summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Modification/NodeAction.php
blob: 369c3a23c83475c784f33a5e96bb764f5cd3ae92 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

namespace Icinga\Module\Businessprocess\Modification;

use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Exception\ModificationError;
use Icinga\Module\Businessprocess\Node;
use Icinga\Exception\ProgrammingError;

/**
 * Abstract NodeAction class
 *
 * Every instance of a NodeAction represents a single applied change. Changes are pushed to
 * a stack and consumed from there. When persisted, NodeActions are serialized with their name,
 * node name and optionally additional properties according preserveProperties. For each property
 * that should be preserved, getter and setter methods have to be defined.
 *
 * @package Icinga\Module\Businessprocess
 */
abstract class NodeAction
{
    /** @var string Name of this action (currently create, modify, remove) */
    protected $actionName;

    /** @var string Name of the node this action applies to */
    protected $nodeName;

    /** @var array Properties which should be preserved when serializing this action */
    protected $preserveProperties = array();

    /**
     * NodeAction constructor.
     *
     * @param Node|string $node
     */
    public function __construct($node = null)
    {
        if ($node !== null) {
            $this->nodeName = (string) $node;
        }
    }

    /**
     * Every NodeAction must be able to apply itself to a BusinessProcess
     *
     * @param BpConfig $config
     * @return mixed
     */
    abstract public function applyTo(BpConfig $config);

    /**
     * Every NodeAction must be able to tell whether it can be applied to a BusinessProcess
     *
     * @param   BpConfig    $config
     *
     * @throws  ModificationError
     *
     * @return  bool
     */
    abstract public function appliesTo(BpConfig $config);

    /**
     * The name of the node this modification applies to
     *
     * @return string
     */
    public function getNodeName()
    {
        return $this->nodeName;
    }

    public function hasNode()
    {
        return $this->nodeName !== null;
    }

    /**
     * Whether this is an instance of a given action name
     *
     * @param string $actionName
     * @return bool
     */
    public function is($actionName)
    {
        return $this->getActionName() === $actionName;
    }

    /**
     * Throw a ModificationError
     *
     * @param   string  $msg
     * @param   mixed   ...
     *
     * @throws  ModificationError
     */
    protected function error($msg)
    {
        $error = ModificationError::create(func_get_args());
        /** @var ModificationError $error */
        throw $error;
    }

    /**
     * Create an instance of a given actionName for a specific Node
     *
     * @param string $actionName
     * @param string $nodeName
     *
     * @return static
     */
    public static function create($actionName, $nodeName)
    {
        $className = __NAMESPACE__ . '\\Node' . ucfirst($actionName) . 'Action';
        $object = new $className($nodeName);
        return $object;
    }

    /**
     * Returns a JSON-encoded serialized NodeAction
     *
     * @return string
     */
    public function serialize()
    {
        $object = (object) array(
            'actionName' => $this->getActionName(),
            'nodeName'   => $this->getNodeName(),
            'properties' => array()
        );

        foreach ($this->preserveProperties as $key) {
            $func = 'get' . ucfirst($key);
            $object->properties[$key] = $this->$func();
        }

        return json_encode($object);
    }

    /**
     * Decodes a JSON-serialized NodeAction and returns an object instance
     *
     * @param $string
     * @return NodeAction
     */
    public static function unSerialize($string)
    {
        $object = json_decode($string, JSON_FORCE_OBJECT);
        $action = self::create($object['actionName'], $object['nodeName']);

        foreach ($object['properties'] as $key => $val) {
            $func = 'set' . ucfirst($key);
            $action->$func($val);
        }

        return $action;
    }

    /**
     * Returns the defined action name or determines such from the class name
     *
     * @return string The action name
     *
     * @throws ProgrammingError when no such class exists
     */
    public function getActionName()
    {
        if ($this->actionName === null) {
            if (! preg_match('/\\\Node(\w+)Action$/', get_class($this), $m)) {
                throw new ProgrammingError(
                    '"%s" is not a NodeAction class',
                    get_class($this)
                );
            }
            $this->actionName = lcfirst($m[1]);
        }

        return $this->actionName;
    }
}