summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Modification/NodeAddChildrenAction.php
blob: 5d5ab2908b100722edb6bf261ff0769d2acdec14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace Icinga\Module\Businessprocess\Modification;

use Icinga\Module\Businessprocess\BpConfig;

class NodeAddChildrenAction extends NodeAction
{
    protected $children = array();

    protected $preserveProperties = array('children');

    /**
     * @inheritdoc
     */
    public function appliesTo(BpConfig $config)
    {
        $name = $this->getNodeName();

        if (! $config->hasBpNode($name)) {
            $this->error('Process "%s" not found', $name);
        }

        return true;
    }

    /**
     * @inheritdoc
     */
    public function applyTo(BpConfig $config)
    {
        $node = $config->getBpNode($this->getNodeName());

        foreach ($this->children as $name) {
            if (! $config->hasNode($name) || $config->getNode($name)->getBpConfig()->getName() !== $config->getName()) {
                if (strpos($name, ';') !== false) {
                    list($host, $service) = preg_split('/;/', $name, 2);

                    if ($service === 'Hoststatus') {
                        $config->createHost($host);
                    } else {
                        $config->createService($host, $service);
                    }
                } elseif ($name[0] === '@' && strpos($name, ':') !== false) {
                    list($configName, $nodeName) = preg_split('~:\s*~', substr($name, 1), 2);
                    $config->createImportedNode($configName, $nodeName);
                }
            }
            $node->addChild($config->getNode($name));
        }

        return $this;
    }

    /**
     * @param array|string $children
     * @return $this
     */
    public function setChildren($children)
    {
        if (is_string($children)) {
            $children = array($children);
        }
        $this->children = $children;
        return $this;
    }

    /**
     * @return array
     */
    public function getChildren()
    {
        return $this->children;
    }
}