|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Workflow library. |
|
5
|
|
|
* |
|
6
|
|
|
* @package workflow |
|
7
|
|
|
* @author David Molineus <[email protected]> |
|
8
|
|
|
* @copyright 2014-2017 netzmacht David Molineus |
|
9
|
|
|
* @license LGPL 3.0 https://github.com/netzmacht/workflow |
|
10
|
|
|
* @filesource |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Netzmacht\Workflow\Flow; |
|
16
|
|
|
|
|
17
|
|
|
use Netzmacht\Workflow\Data\EntityId; |
|
18
|
|
|
use Netzmacht\Workflow\Flow\Condition\Workflow\AndCondition; |
|
19
|
|
|
use Netzmacht\Workflow\Flow\Condition\Workflow\Condition; |
|
20
|
|
|
use Netzmacht\Workflow\Flow\Exception\StepNotFoundException; |
|
21
|
|
|
use Netzmacht\Workflow\Flow\Exception\TransitionNotFound; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Workflow stores all information of a step processing workflow. |
|
25
|
|
|
* |
|
26
|
|
|
* @package Netzmacht\Workflow\Flow |
|
27
|
|
|
*/ |
|
28
|
|
|
class Workflow extends Base |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Transitions being available in the workflow. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Transition[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $transitions = array(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Steps being available in the workflow. |
|
39
|
|
|
* |
|
40
|
|
|
* @var Step[] |
|
41
|
|
|
*/ |
|
42
|
|
|
private $steps = array(); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The start transition. |
|
46
|
|
|
* |
|
47
|
|
|
* @var Transition |
|
48
|
|
|
*/ |
|
49
|
|
|
private $startTransition; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Condition to supports if workflow can handle an entity. |
|
53
|
|
|
* |
|
54
|
|
|
* @var AndCondition |
|
55
|
|
|
*/ |
|
56
|
|
|
private $condition; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Name of the provider. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $providerName; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Construct. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name The name of the workflow. |
|
69
|
|
|
* @param string $providerName Name of the provider. |
|
70
|
|
|
* @param string $label The label of the workflow. |
|
71
|
|
|
* @param array $config Extra config. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct(string $name, string $providerName, string $label = '', array $config = array()) |
|
74
|
|
|
{ |
|
75
|
|
|
parent::__construct($name, $label, $config); |
|
76
|
|
|
|
|
77
|
|
|
$this->providerName = $providerName; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Add a transition to the workflow. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Transition $transition Transition to be added. |
|
84
|
|
|
* @param bool $startTransition True if transition will be the start transition. |
|
85
|
|
|
* |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
|
|
public function addTransition(Transition $transition, $startTransition = false): self |
|
89
|
|
|
{ |
|
90
|
|
|
if (in_array($transition, $this->transitions)) { |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->transitions[] = $transition; |
|
95
|
|
|
|
|
96
|
|
|
if ($startTransition) { |
|
97
|
|
|
$this->startTransition = $transition; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get a transition by name. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $transitionName The name of the transition. |
|
107
|
|
|
* |
|
108
|
|
|
* @throws TransitionNotFound If transition is not found. |
|
109
|
|
|
* |
|
110
|
|
|
* @return \Netzmacht\Workflow\Flow\Transition If transition is not found. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getTransition(string $transitionName): Transition |
|
113
|
|
|
{ |
|
114
|
|
|
foreach ($this->transitions as $transition) { |
|
115
|
|
|
if ($transition->getName() == $transitionName) { |
|
116
|
|
|
return $transition; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
throw TransitionNotFound::withName($transitionName, $this->getName()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get allowed transitions for a workflow item. |
|
125
|
|
|
* |
|
126
|
|
|
* @param Item $item Workflow item. |
|
127
|
|
|
* @param Context $context Transition context. |
|
128
|
|
|
* |
|
129
|
|
|
* @throws StepNotFoundException If Step does not exists. |
|
130
|
|
|
* @throws TransitionNotFound If transition does not exists. |
|
131
|
|
|
* |
|
132
|
|
|
* @return Transition[]|iterable |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getAvailableTransitions(Item $item, Context $context = null): iterable |
|
135
|
|
|
{ |
|
136
|
|
|
if ($context) { |
|
137
|
|
|
$context = $context->createCleanCopy(); |
|
138
|
|
|
} else { |
|
139
|
|
|
$context = new Context(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (!$item->isWorkflowStarted()) { |
|
143
|
|
|
$transitions = array($this->getStartTransition()); |
|
144
|
|
|
} else { |
|
145
|
|
|
$step = $this->getStep($item->getCurrentStepName()); |
|
146
|
|
|
$transitions = array_map( |
|
147
|
|
|
function ($transitionName) { |
|
148
|
|
|
return $this->getTransition($transitionName); |
|
149
|
|
|
}, |
|
150
|
|
|
$step->getAllowedTransitions() |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return array_filter( |
|
155
|
|
|
$transitions, |
|
156
|
|
|
function (Transition $transition) use ($item, $context) { |
|
157
|
|
|
return $transition->isAvailable($item, $context); |
|
158
|
|
|
} |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get all transitions. |
|
164
|
|
|
* |
|
165
|
|
|
* @return Transition[]|iterable |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getTransitions(): iterable |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->transitions; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Check if transition is part of the workflow. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $transitionName Transition name. |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
|
|
public function hasTransition(string $transitionName): bool |
|
180
|
|
|
{ |
|
181
|
|
|
foreach ($this->transitions as $transition) { |
|
182
|
|
|
if ($transition->getName() === $transitionName) { |
|
183
|
|
|
return true; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Check if a specific transition is available. |
|
192
|
|
|
* |
|
193
|
|
|
* @param Item $item The workflow item. |
|
194
|
|
|
* @param Context $context Transition context. |
|
195
|
|
|
* @param string $transitionName The transition name. |
|
196
|
|
|
* |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
public function isTransitionAvailable( |
|
200
|
|
|
Item $item, |
|
201
|
|
|
Context $context, |
|
202
|
|
|
string $transitionName |
|
203
|
|
|
): bool { |
|
204
|
|
|
if (!$item->isWorkflowStarted()) { |
|
205
|
|
|
return $this->getStartTransition()->getName() === $transitionName; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$step = $this->getStep($item->getCurrentStepName()); |
|
209
|
|
|
if (!$step->isTransitionAllowed($transitionName)) { |
|
210
|
|
|
return false; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$transition = $this->getTransition($transitionName); |
|
214
|
|
|
|
|
215
|
|
|
return $transition->isAvailable($item, $context); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Add a new step to the workflow. |
|
220
|
|
|
* |
|
221
|
|
|
* @param Step $step Step to be added. |
|
222
|
|
|
* |
|
223
|
|
|
* @return $this |
|
224
|
|
|
*/ |
|
225
|
|
|
public function addStep(Step $step): self |
|
226
|
|
|
{ |
|
227
|
|
|
$this->steps[] = $step; |
|
228
|
|
|
|
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get a step by step name. |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $stepName The step name. |
|
236
|
|
|
* |
|
237
|
|
|
* @return Step |
|
238
|
|
|
* |
|
239
|
|
|
* @throws StepNotFoundException If step is not found. |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getStep(string $stepName): Step |
|
242
|
|
|
{ |
|
243
|
|
|
foreach ($this->steps as $step) { |
|
244
|
|
|
if ($step->getName() == $stepName) { |
|
245
|
|
|
return $step; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
throw new StepNotFoundException($stepName, $this->getName()); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Check if step with a name exist. |
|
254
|
|
|
* |
|
255
|
|
|
* @param string $stepName The step name. |
|
256
|
|
|
* |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
|
|
public function hasStep(string $stepName): bool |
|
260
|
|
|
{ |
|
261
|
|
|
foreach ($this->steps as $step) { |
|
262
|
|
|
if ($step->getName() == $stepName) { |
|
263
|
|
|
return true; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return false; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Set transition as start transition. |
|
272
|
|
|
* |
|
273
|
|
|
* @param string $transitionName Name of start transition. |
|
274
|
|
|
* |
|
275
|
|
|
* @throws TransitionNotFound If transition is not part of the workflow. |
|
276
|
|
|
* |
|
277
|
|
|
* @return $this |
|
278
|
|
|
*/ |
|
279
|
|
|
public function setStartTransition(string $transitionName): self |
|
280
|
|
|
{ |
|
281
|
|
|
$this->startTransition = $this->getTransition($transitionName); |
|
282
|
|
|
|
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Get the start transition. |
|
288
|
|
|
* |
|
289
|
|
|
* @return Transition |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getStartTransition(): Transition |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->startTransition; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Get the current condition. |
|
298
|
|
|
* |
|
299
|
|
|
* @return AndCondition |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getCondition():? AndCondition |
|
302
|
|
|
{ |
|
303
|
|
|
return $this->condition; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Shortcut to add a condition to the condition collection. |
|
308
|
|
|
* |
|
309
|
|
|
* @param Condition $condition Condition to be added. |
|
310
|
|
|
* |
|
311
|
|
|
* @return $this |
|
312
|
|
|
*/ |
|
313
|
|
|
public function addCondition(Condition $condition) |
|
314
|
|
|
{ |
|
315
|
|
|
if (!$this->condition) { |
|
316
|
|
|
$this->condition = new AndCondition(); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
$this->condition->addCondition($condition); |
|
320
|
|
|
|
|
321
|
|
|
return $this; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Get provider name. |
|
326
|
|
|
* |
|
327
|
|
|
* @return string |
|
328
|
|
|
*/ |
|
329
|
|
|
public function getProviderName(): string |
|
330
|
|
|
{ |
|
331
|
|
|
return $this->providerName; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Consider if workflow is supports an entity. |
|
336
|
|
|
* |
|
337
|
|
|
* @param EntityId $entityId The entity id. |
|
338
|
|
|
* @param mixed $entity The entity. |
|
339
|
|
|
* |
|
340
|
|
|
* @return bool |
|
341
|
|
|
*/ |
|
342
|
|
|
public function supports(EntityId $entityId, $entity): bool |
|
343
|
|
|
{ |
|
344
|
|
|
if (!$this->condition) { |
|
345
|
|
|
return true; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
return $this->condition->match($this, $entityId, $entity); |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|