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\Flow\Condition\Transition\AndCondition; |
18
|
|
|
use Netzmacht\Workflow\Flow\Condition\Transition\Condition; |
19
|
|
|
use Netzmacht\Workflow\Flow\Exception\ActionFailedException; |
20
|
|
|
use Netzmacht\Workflow\Flow\Security\Permission; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Transition handles the transition from a step to another. |
24
|
|
|
* |
25
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
26
|
|
|
*/ |
27
|
|
|
class Transition extends Base |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Actions which will be executed during the transition. |
31
|
|
|
* |
32
|
|
|
* @var Action[] |
33
|
|
|
*/ |
34
|
|
|
private $actions = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Post actions which will be executed when new step is reached. |
38
|
|
|
* |
39
|
|
|
* @var Action[] |
40
|
|
|
*/ |
41
|
|
|
private $postActions = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The step the transition is moving to. |
45
|
|
|
* |
46
|
|
|
* @var Step |
47
|
|
|
*/ |
48
|
|
|
private $stepTo; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* A pre condition which has to be passed to execute transition. |
52
|
|
|
* |
53
|
|
|
* @var AndCondition |
54
|
|
|
*/ |
55
|
|
|
private $preCondition; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* A condition which has to be passed to execute the transition. |
59
|
|
|
* |
60
|
|
|
* @var AndCondition |
61
|
|
|
*/ |
62
|
|
|
private $condition; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* A set of permission being assigned to the transition. |
66
|
|
|
* |
67
|
|
|
* @var Permission|null |
68
|
|
|
*/ |
69
|
|
|
private $permission; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The corresponding workflow. |
73
|
|
|
* |
74
|
|
|
* @var Workflow |
75
|
|
|
*/ |
76
|
|
|
private $workflow; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Transition constructor. |
80
|
|
|
* |
81
|
|
|
* @param string $name Name of the element. |
82
|
|
|
* @param Workflow $workflow The workflow to which the transition belongs. |
83
|
|
|
* @param Step $stepTo The target step. |
84
|
|
|
* @param string $label Label of the element. |
85
|
|
|
* @param array $config Configuration values. |
86
|
|
|
*/ |
87
|
|
|
public function __construct(string $name, Workflow $workflow, Step $stepTo, string $label = '', array $config = []) |
88
|
|
|
{ |
89
|
|
|
parent::__construct($name, $label, $config); |
90
|
|
|
|
91
|
|
|
$workflow->addTransition($this); |
92
|
|
|
|
93
|
|
|
$this->workflow = $workflow; |
94
|
|
|
$this->stepTo = $stepTo; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the workflow. |
99
|
|
|
* |
100
|
|
|
* @return Workflow |
101
|
|
|
*/ |
102
|
|
|
public function getWorkflow(): Workflow |
103
|
|
|
{ |
104
|
|
|
return $this->workflow; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add an action to the transition. |
109
|
|
|
* |
110
|
|
|
* @param Action $action The added action. |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function addAction(Action $action): self |
115
|
|
|
{ |
116
|
|
|
$this->actions[] = $action; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get all actions. |
123
|
|
|
* |
124
|
|
|
* @return Action[]|iterable |
125
|
|
|
*/ |
126
|
|
|
public function getActions(): iterable |
127
|
|
|
{ |
128
|
|
|
return $this->actions; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add an post action to the transition. |
133
|
|
|
* |
134
|
|
|
* @param Action $action The added action. |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function addPostAction(Action $action): self |
139
|
|
|
{ |
140
|
|
|
$this->postActions[] = $action; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get all post actions. |
147
|
|
|
* |
148
|
|
|
* @return Action[]|iterable |
149
|
|
|
*/ |
150
|
|
|
public function getPostActions(): iterable |
151
|
|
|
{ |
152
|
|
|
return $this->postActions; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the target step. |
157
|
|
|
* |
158
|
|
|
* @return Step |
159
|
|
|
*/ |
160
|
|
|
public function getStepTo():? Step |
161
|
|
|
{ |
162
|
|
|
return $this->stepTo; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the condition. |
167
|
|
|
* |
168
|
|
|
* @return Condition|null |
169
|
|
|
*/ |
170
|
|
|
public function getCondition():? Condition |
171
|
|
|
{ |
172
|
|
|
return $this->condition; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Add a condition. |
177
|
|
|
* |
178
|
|
|
* @param Condition $condition The new condition. |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function addCondition(Condition $condition): self |
183
|
|
|
{ |
184
|
|
|
if (!$this->condition) { |
185
|
|
|
$this->condition = new AndCondition(); |
186
|
|
|
} |
187
|
|
|
$this->condition->addCondition($condition); |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the precondition. |
194
|
|
|
* |
195
|
|
|
* @return Condition |
196
|
|
|
*/ |
197
|
|
|
public function getPreCondition():? Condition |
198
|
|
|
{ |
199
|
|
|
return $this->preCondition; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Add a precondition precondition. |
204
|
|
|
* |
205
|
|
|
* @param Condition $preCondition The new precondition. |
206
|
|
|
* |
207
|
|
|
* @return $this |
208
|
|
|
*/ |
209
|
|
|
public function addPreCondition(Condition $preCondition): self |
210
|
|
|
{ |
211
|
|
|
if (!$this->preCondition) { |
212
|
|
|
$this->preCondition = new AndCondition(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->preCondition->addCondition($preCondition); |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Consider if user input is required. |
222
|
|
|
* |
223
|
|
|
* @param Item $item Workflow item. |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public function getRequiredPayloadProperties(Item $item): array |
228
|
|
|
{ |
229
|
|
|
if (empty($this->actions)) { |
230
|
|
|
return []; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return array_merge( |
234
|
|
|
... array_map( |
235
|
|
|
function (Action $action) use ($item) { |
236
|
|
|
return $action->getRequiredPayloadProperties($item); |
237
|
|
|
}, |
238
|
|
|
$this->actions |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Validate the given item and context (payload properties). |
245
|
|
|
* |
246
|
|
|
* @param Item $item Workflow item. |
247
|
|
|
* @param Context $context Transition context. |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
public function validate(Item $item, Context $context): bool |
252
|
|
|
{ |
253
|
|
|
$validated = true; |
254
|
|
|
|
255
|
|
|
foreach ($this->actions as $action) { |
256
|
|
|
$validated = $validated && $action->validate($item, $context); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $validated; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Consider if transition is allowed. |
264
|
|
|
* |
265
|
|
|
* @param Item $item The Item. |
266
|
|
|
* @param Context $context The transition context. |
267
|
|
|
* |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
|
|
public function isAllowed(Item $item, Context $context): bool |
271
|
|
|
{ |
272
|
|
|
if ($this->checkPreCondition($item, $context)) { |
273
|
|
|
return $this->checkCondition($item, $context); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return false; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Consider if transition is available. |
281
|
|
|
* |
282
|
|
|
* If a transition can be available but it is not allowed depending on the user input. |
283
|
|
|
* |
284
|
|
|
* @param Item $item The Item. |
285
|
|
|
* @param Context $context The transition context. |
286
|
|
|
* |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
|
|
public function isAvailable(Item $item, Context $context): bool |
290
|
|
|
{ |
291
|
|
|
if ($this->getRequiredPayloadProperties($item)) { |
292
|
|
|
return $this->checkPreCondition($item, $context); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $this->isAllowed($item, $context); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Check the precondition. |
300
|
|
|
* |
301
|
|
|
* @param Item $item The Item. |
302
|
|
|
* @param Context $context The transition context. |
303
|
|
|
* |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
public function checkPreCondition(Item $item, Context $context): bool |
307
|
|
|
{ |
308
|
|
|
return $this->performConditionCheck($this->preCondition, $item, $context); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Check the condition. |
313
|
|
|
* |
314
|
|
|
* @param Item $item The Item. |
315
|
|
|
* @param Context $context The transition context. |
316
|
|
|
* |
317
|
|
|
* @return bool |
318
|
|
|
*/ |
319
|
|
|
public function checkCondition(Item $item, Context $context): bool |
320
|
|
|
{ |
321
|
|
|
return $this->performConditionCheck($this->condition, $item, $context); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Set a permission to the transition. |
326
|
|
|
* |
327
|
|
|
* @param Permission $permission Permission being assigned. |
328
|
|
|
* |
329
|
|
|
* @return $this |
330
|
|
|
*/ |
331
|
|
|
public function setPermission(Permission $permission): self |
332
|
|
|
{ |
333
|
|
|
$this->permission = $permission; |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Consider if permission is assigned to transition. |
340
|
|
|
* |
341
|
|
|
* @param Permission $permission Permission being check. |
342
|
|
|
* |
343
|
|
|
* @return bool |
344
|
|
|
*/ |
345
|
|
|
public function hasPermission(Permission $permission): bool |
346
|
|
|
{ |
347
|
|
|
if ($this->permission) { |
348
|
|
|
return $this->permission->equals($permission); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return false; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Get assigned permission. Returns null if no transition is set. |
356
|
|
|
* |
357
|
|
|
* @return Permission|null |
358
|
|
|
*/ |
359
|
|
|
public function getPermission():? Permission |
360
|
|
|
{ |
361
|
|
|
return $this->permission; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Execute all actions. |
366
|
|
|
* |
367
|
|
|
* @param Item $item The workflow item. |
368
|
|
|
* @param Context $context The transition context. |
369
|
|
|
* |
370
|
|
|
* @return bool |
371
|
|
|
*/ |
372
|
|
|
public function executeActions(Item $item, Context $context): bool |
373
|
|
|
{ |
374
|
|
|
return $this->doExecuteActions($item, $context, $this->actions); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Execute all actions. |
379
|
|
|
* |
380
|
|
|
* @param Item $item The workflow item. |
381
|
|
|
* @param Context $context The transition context. |
382
|
|
|
* |
383
|
|
|
* @return bool |
384
|
|
|
*/ |
385
|
|
|
public function executePostActions(Item $item, Context $context): bool |
386
|
|
|
{ |
387
|
|
|
return $this->doExecuteActions($item, $context, $this->postActions); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Perform condition check. |
392
|
|
|
* |
393
|
|
|
* @param Condition|null $condition Condition to be checked. |
394
|
|
|
* @param Item $item Workflow item. |
395
|
|
|
* @param Context $context Condition context. |
396
|
|
|
* |
397
|
|
|
* @return bool |
398
|
|
|
*/ |
399
|
|
|
private function performConditionCheck($condition, $item, $context): bool |
400
|
|
|
{ |
401
|
|
|
if (!$condition) { |
402
|
|
|
return true; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
return $condition->match($this, $item, $context); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Execute the actions. |
410
|
|
|
* |
411
|
|
|
* @param Item $item Workflow item. |
412
|
|
|
* @param Context $context Condition context. |
413
|
|
|
* @param Action[] $actions Action to execute. |
414
|
|
|
* |
415
|
|
|
* @return bool |
416
|
|
|
*/ |
417
|
|
|
private function doExecuteActions(Item $item, Context $context, $actions): bool |
418
|
|
|
{ |
419
|
|
|
$success = $this->isAllowed($item, $context); |
420
|
|
|
|
421
|
|
|
if ($success) { |
422
|
|
|
try { |
423
|
|
|
foreach ($actions as $action) { |
424
|
|
|
$action->transit($this, $item, $context); |
425
|
|
|
} |
426
|
|
|
} catch (ActionFailedException $e) { |
427
|
|
|
$params = ['exception' => $e->getMessage()]; |
428
|
|
|
$context->addError('transition.action.failed', $params); |
429
|
|
|
|
430
|
|
|
return false; |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
return $success; |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|