Completed
Pull Request — master (#1)
by Fabrice
09:39 queued 01:54
created

FlowAbstract::getFlowStatus()   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\Flows;
11
12
use fab2s\NodalFlow\Nodes\NodeInterface;
13
14
/**
15
 * Abstract Class FlowAbstract
16
 */
17
abstract class FlowAbstract implements FlowInterface
18
{
19
    use FlowIdTrait;
20
21
    /**
22
     * The parent Flow, only set when branched
23
     *
24
     * @var FlowInterface
25
     */
26
    public $parent;
27
28
    /**
29
     * Current Flow Status
30
     *
31
     * @var FlowStatusInterface
32
     */
33
    protected $flowStatus;
34
35
    /**
36
     * The underlying node structure
37
     *
38
     * @var NodeInterface[]
39
     */
40
    protected $nodes = [];
41
42
    /**
43
     * @var FlowMapInterface
44
     */
45
    protected $flowMap;
46
47
    /**
48
     * Progress modulo to apply
49
     * Set to x if you want to trigger
50
     * progress every x iterations in flow
51
     *
52
     * @var int
53
     */
54
    protected $progressMod = 1024;
55
56
    /**
57
     * @var string|bool
58
     */
59
    protected $interruptNodeId;
60
61
    /**
62
     * Get the stats array with latest Node stats
63
     *
64
     * @return array
65
     */
66
    public function getStats()
67
    {
68
        return $this->flowMap->getStats();
69
    }
70
71
    /**
72
     * Get the stats array with latest Node stats
73
     *
74
     * @return FlowMapInterface
75
     */
76
    public function getFlowMap()
77
    {
78
        return $this->flowMap;
79
    }
80
81
    /**
82
     * Get the Node array
83
     *
84
     * @return NodeInterface[]
85
     */
86
    public function getNodes()
87
    {
88
        return $this->nodes;
89
    }
90
91
    /**
92
     * Get/Generate Node Map
93
     *
94
     * @return array
95
     */
96
    public function getNodeMap()
97
    {
98
        return $this->flowMap->getNodeMap();
99
    }
100
101
    /**
102
     * Get current $progressMod
103
     *
104
     * @return int
105
     */
106
    public function getProgressMod()
107
    {
108
        return $this->progressMod;
109
    }
110
111
    /**
112
     * The Flow status can either indicate be:
113
     *      - clean (isClean()): everything went well
114
     *      - dirty (isDirty()): one Node broke the flow
115
     *      - exception (isException()): an exception was raised during the flow
116
     *
117
     * @return FlowStatusInterface
118
     */
119
    public function getFlowStatus()
120
    {
121
        return $this->flowStatus;
122
    }
123
124
    /**
125
     * getId() alias for backward compatibility
126
     *
127
     * @deprecated use `getId` instead
128
     *
129
     * @return string
130
     */
131
    public function getFlowId()
132
    {
133
        return $this->getId();
134
    }
135
136
    /**
137
     * Set parent Flow, happens only when branched
138
     *
139
     * @param FlowInterface $flow
140
     *
141
     * @return $this
142
     */
143
    public function setParent(FlowInterface $flow)
144
    {
145
        $this->parent = $flow;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Get eventual parent Flow
152
     *
153
     * @return FlowInterface
154
     */
155
    public function getParent()
156
    {
157
        return $this->parent;
158
    }
159
160
    /**
161
     * Tells if this flow has a parent
162
     *
163
     * @return bool
164
     */
165
    public function hasParent()
166
    {
167
        return !empty($this->parent);
168
    }
169
170
    /**
171
     * Define the progress modulo, Progress Callback will be
172
     * triggered upon each iteration in the flow modulo $progressMod
173
     *
174
     * @param int $progressMod
175
     *
176
     * @return $this
177
     */
178
    public function setProgressMod($progressMod)
179
    {
180
        $this->progressMod = max(1, (int) $progressMod);
181
182
        return $this;
183
    }
184
185
    /**
186
     * @param string                    $interruptType
187
     * @param InterrupterInterface|null $flowInterrupt
188
     *
189
     * @throws NodalFlowException
190
     *
191
     * @return $this
192
     */
193
    public function interruptFlow($interruptType, InterrupterInterface $flowInterrupt = null)
194
    {
195
        switch ($interruptType) {
196
            case InterrupterInterface::TYPE_CONTINUE:
197
                $this->continue = true;
0 ignored issues
show
Bug introduced by
The property continue does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
198
                $this->flowMap->incrementFlow('num_continue');
199
                break;
200
            case InterrupterInterface::TYPE_BREAK:
201
                $this->flowStatus = new FlowStatus(FlowStatus::FLOW_DIRTY);
202
                $this->break      = true;
0 ignored issues
show
Bug introduced by
The property break does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
203
                $this->flowMap->incrementFlow('num_break');
204
                break;
205
            default:
206
                throw new NodalFlowException('FlowInterrupt Type missing');
207
        }
208
209
        if ($flowInterrupt) {
210
            $flowInterrupt->setType($interruptType)->propagate($this);
211
        }
212
213
        return $this;
214
    }
215
216
    /**
217
     * Used to set the eventual Node Target of an Interrupt signal
218
     * set to :
219
     * - A node hash to target
220
     * - true to interrupt every upstream nodes
221
     *     in this Flow
222
     * - false to only interrupt up to the first
223
     *     upstream Traversable in this Flow
224
     *
225
     * @param string|bool $interruptNodeId
226
     *
227
     * @return $this
228
     */
229
    public function setInterruptNodeId($interruptNodeId)
230
    {
231
        $this->interruptNodeId = $interruptNodeId;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @param NodeInterface $node
238
     *
239
     * @return bool
240
     */
241
    protected function interruptNode(NodeInterface $node)
242
    {
243
        // if we have an interruptNodeId, bubble up until we match a node
244
        // else stop propagation
245
        return $this->interruptNodeId ? $this->interruptNodeId !== $node->getId() : false;
246
    }
247
}
248