summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Modification/NodeModifyAction.php
blob: 1b330940b78dc78fa966156cb99dcddbd725a19e (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

namespace Icinga\Module\Businessprocess\Modification;

use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Node;

class NodeModifyAction extends NodeAction
{
    protected $properties = array();

    protected $formerProperties = array();

    protected $preserveProperties = array('formerProperties', 'properties');

    /**
     * Set properties for a specific node
     *
     * Can be called multiple times
     *
     * @param Node $node
     * @param array $properties
     *
     * @return $this
     */
    public function setNodeProperties(Node $node, array $properties)
    {
        foreach (array_keys($properties) as $key) {
            $this->properties[$key] = $properties[$key];

            if (array_key_exists($key, $this->formerProperties)) {
                continue;
            }

            $func = 'get' . ucfirst($key);
            $this->formerProperties[$key] = $node->$func();
        }

        return $this;
    }

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

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

        $node = $config->getNode($name);

        foreach ($this->properties as $key => $val) {
            $currentVal = $node->{'get' . ucfirst($key)}();
            if ($this->formerProperties[$key] !== $currentVal) {
                $this->error(
                    'Property %s of node "%s" changed its value from "%s" to "%s"',
                    $key,
                    $name,
                    $this->formerProperties[$key],
                    $currentVal
                );
            }
        }

        return true;
    }

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

        foreach ($this->properties as $key => $val) {
            $func = 'set' . ucfirst($key);
            $node->$func($val);
        }

        return $this;
    }

    /**
     * @param $properties
     * @return $this
     */
    public function setProperties($properties)
    {
        $this->properties = $properties;
        return $this;
    }

    /**
     * @param $properties
     * @return $this
     */
    public function setFormerProperties($properties)
    {
        $this->formerProperties = $properties;
        return $this;
    }

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

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