Completed
Pull Request — master (#7)
by Fabrice
02:29
created

FlowAbstract::getRootFlow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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\Callbacks\CallbackInterface;
13
use fab2s\NodalFlow\Nodes\NodeInterface;
14
15
/**
16
 * Abstract Class FlowAbstract
17
 */
18
abstract class FlowAbstract extends FlowAncestryAbstract
19
{
20
    use FlowIdTrait;
21
22
    /**
23
     * The underlying node structure
24
     *
25
     * @var NodeInterface[]
26
     */
27
    protected $nodes = [];
28
29
    /**
30
     * @var FlowRegistryInterface
31
     */
32
    protected $registry;
33
34
    /**
35
     * The current registered Callback class if any
36
     *
37
     * @var CallbackInterface|null
38
     */
39
    protected $callBack;
40
41
    /**
42
     * Progress modulo to apply
43
     * Set to x if you want to trigger
44
     * progress every x iterations in flow
45
     *
46
     * @var int
47
     */
48
    protected $progressMod = 1024;
49
50
    /**
51
     * Get the stats array with latest Node stats
52
     *
53
     * @return array
54
     */
55
    public function getStats()
56
    {
57
        return $this->flowMap->getStats();
58
    }
59
60
    /**
61
     * Get the stats array with latest Node stats
62
     *
63
     * @return FlowMapInterface
64
     */
65
    public function getFlowMap()
66
    {
67
        return $this->flowMap;
68
    }
69
70
    /**
71
     * Get the Node array
72
     *
73
     * @return NodeInterface[]
74
     */
75
    public function getNodes()
76
    {
77
        return $this->nodes;
78
    }
79
80
    /**
81
     * Get/Generate Node Map
82
     *
83
     * @return array
84
     */
85
    public function getNodeMap()
86
    {
87
        return $this->flowMap->getNodeMap();
88
    }
89
90
    /**
91
     * Get current $progressMod
92
     *
93
     * @return int
94
     */
95
    public function getProgressMod()
96
    {
97
        return $this->progressMod;
98
    }
99
100
    /**
101
     * The Flow status can either indicate be:
102
     *      - clean (isClean()): everything went well
103
     *      - dirty (isDirty()): one Node broke the flow
104
     *      - exception (isException()): an exception was raised during the flow
105
     *
106
     * @return FlowStatusInterface
107
     */
108
    public function getFlowStatus()
109
    {
110
        return $this->flowStatus;
111
    }
112
113
    /**
114
     * getId() alias for backward compatibility
115
     *
116
     * @deprecated use `getId` instead
117
     *
118
     * @return string
119
     */
120
    public function getFlowId()
121
    {
122
        return $this->getId();
123
    }
124
125
    /**
126
     * Define the progress modulo, Progress Callback will be
127
     * triggered upon each iteration in the flow modulo $progressMod
128
     *
129
     * @param int $progressMod
130
     *
131
     * @return $this
132
     */
133
    public function setProgressMod($progressMod)
134
    {
135
        $this->progressMod = max(1, (int) $progressMod);
136
137
        return $this;
138
    }
139
140
    /**
141
     * Register callback class
142
     *
143
     * @param CallbackInterface $callBack
144
     *
145
     * @return $this
146
     */
147
    public function setCallBack(CallbackInterface $callBack)
148
    {
149
        $this->callBack = $callBack;
150
151
        return $this;
152
    }
153
154
    /**
155
     * KISS helper to trigger Callback slots
156
     *
157
     * @param string             $which
158
     * @param null|NodeInterface $node
159
     *
160
     * @return $this
161
     */
162
    protected function triggerCallback($which, NodeInterface $node = null)
163
    {
164
        if (null !== $this->callBack) {
165
            $this->callBack->$which($this, $node);
166
        }
167
168
        return $this;
169
    }
170
}
171