Passed
Push — master ( 9ec99d...6dd623 )
by Fabrice
01:42
created

NodeAbstract::getNodeIncrements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of NodalFlow.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\NodalFlow\Nodes;
11
12
use fab2s\NodalFlow\Flows\FlowInterface;
13
use fab2s\NodalFlow\NodalFlowException;
14
15
/**
16
 * abstract Class NodeAbstract
17
 */
18
abstract class NodeAbstract implements NodeInterface
19
{
20
    /**
21
     * The carrying Flow
22
     *
23
     * @var FlowInterface
24
     */
25
    public $carrier;
26
27
    /**
28
     * Indicate if this Node is traversable
29
     *
30
     * @var bool
31
     */
32
    protected $isATraversable;
33
34
    /**
35
     * Indicate if this Node is returning a value
36
     *
37
     * @var bool
38
     */
39
    protected $isAReturningVal;
40
41
    /**
42
     * Indicate if this Node is a Flow (Branch)
43
     *
44
     * @var bool
45
     */
46
    protected $isAFlow;
47
48
    /**
49
     * @var array
50
     */
51
    protected $nodeIncrements = [];
52
53
    /**
54
     * Instantiate a Node
55
     */
56
    public function __construct()
57
    {
58
        $this->enforceIsATraversable();
59
    }
60
61
    /**
62
     * Indicate if this Node is Traversable
63
     *
64
     * @return bool
65
     */
66
    public function isTraversable()
67
    {
68
        return (bool) $this->isATraversable;
69
    }
70
71
    /**
72
     * Indicate if this Node is a Flow (Branch)
73
     *
74
     * @return bool true if this node instanceof FlowInterface
75
     */
76
    public function isFlow()
77
    {
78
        return (bool) $this->isAFlow;
79
    }
80
81
    /**
82
     * Indicate if this Node is returning a value
83
     *
84
     * @return bool true if this node is expected to return
85
     *              something to pass on next node as param.
86
     *              If nothing is returned, the previously
87
     *              returned value will be use as param
88
     *              for next nodes.
89
     */
90
    public function isReturningVal()
91
    {
92
        return (bool) $this->isAReturningVal;
93
    }
94
95
    /**
96
     * Set carrying Flow
97
     *
98
     * @param FlowInterface $flow
99
     *
100
     * @return $this
101
     */
102
    public function setCarrier(FlowInterface $flow)
103
    {
104
        $this->carrier = $flow;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Get carrying Flow
111
     *
112
     * @return FlowInterface
113
     */
114
    public function getCarrier()
115
    {
116
        return $this->carrier;
117
    }
118
119
    /**
120
     * Get this Node's hash, must be deterministic and unique
121
     *
122
     * Caching the result would save a bit of resources
123
     * but would require to implement `__clone()` to
124
     * make sure we reset the hash should a node be cloned.
125
     * Doing this would add the burden to actual Node
126
     * implementation to trigger `parent::_clone()`
127
     * which is not ideal and impossible to guaranty.
128
     * Now since this method is not used in the actual
129
     * flow execution loop, but only when an interruption
130
     * is raised, it's not a performance issue.
131
     *
132
     * @return string
133
     */
134
    public function getNodeHash()
135
    {
136
        return \sha1(\spl_object_hash($this));
137
    }
138
139
    /**
140
     * Get the custom Node increments to be considered during
141
     * Flow execution
142
     * To set additional increment keys, use :
143
     *      'keyName' => int
144
     * to add keyName as increment, starting at int
145
     * or :
146
     *      'keyName' => 'existingIncrement'
147
     * to assign keyName as a reference to an existingIncrement
148
     *
149
     * @return array
150
     */
151
    public function getNodeIncrements()
152
    {
153
        return $this->nodeIncrements;
154
    }
155
156
    /**
157
     * Make sure this Node is consistent
158
     *
159
     * @throws NodalFlowException
160
     *
161
     * @return $this
162
     */
163
    protected function enforceIsATraversable()
164
    {
165
        if ($this->isFlow()) {
166
            if ($this->isATraversable) {
167
                throw new NodalFlowException('Cannot Traverse a Branch');
168
            }
169
170
            return $this;
171
        }
172
173
        if ($this->isATraversable) {
174
            if (!($this instanceof TraversableNodeInterface)) {
175
                throw new NodalFlowException('Cannot Traverse a Node that does not implement TraversableNodeInterface');
176
            }
177
178
            return $this;
179
        }
180
181
        if (!($this instanceof ExecNodeInterface)) {
182
            throw new NodalFlowException('Cannot Exec a Node that does not implement ExecNodeInterface');
183
        }
184
185
        return $this;
186
    }
187
}
188