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\Handler; |
16
|
|
|
|
17
|
|
|
use Netzmacht\Workflow\Flow\Context; |
18
|
|
|
use Netzmacht\Workflow\Flow\Exception\WorkflowException; |
19
|
|
|
use Netzmacht\Workflow\Flow\Item; |
20
|
|
|
use Netzmacht\Workflow\Flow\State; |
21
|
|
|
use Netzmacht\Workflow\Flow\Step; |
22
|
|
|
use Netzmacht\Workflow\Flow\Transition; |
23
|
|
|
use Netzmacht\Workflow\Flow\Workflow; |
24
|
|
|
use Netzmacht\Workflow\Transaction\TransactionHandler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AbstractTransitionHandler can be used as base class for transition handler implementations. |
28
|
|
|
* |
29
|
|
|
* @package Netzmacht\Workflow\Handler |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractTransitionHandler implements TransitionHandler |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The given entity. |
35
|
|
|
* |
36
|
|
|
* @var Item |
37
|
|
|
*/ |
38
|
|
|
private $item; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The current workflow. |
42
|
|
|
* |
43
|
|
|
* @var Workflow |
44
|
|
|
*/ |
45
|
|
|
private $workflow; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The transition name which will be handled. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $transitionName; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Validation state. |
56
|
|
|
* |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
private $validated; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The transaction handler. |
63
|
|
|
* |
64
|
|
|
* @var TransactionHandler |
65
|
|
|
*/ |
66
|
|
|
protected $transactionHandler; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The transition context. |
70
|
|
|
* |
71
|
|
|
* @var Context |
72
|
|
|
*/ |
73
|
|
|
private $context; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Construct. |
77
|
|
|
* |
78
|
|
|
* @param Item $item The item. |
79
|
|
|
* @param Workflow $workflow The current workflow. |
80
|
|
|
* @param string $transitionName The transition to be handled. |
81
|
|
|
* @param TransactionHandler $transactionHandler TransactionHandler take care of transactions. |
82
|
|
|
* |
83
|
|
|
* @throws WorkflowException If invalid transition name is given. |
84
|
|
|
*/ |
85
|
|
|
public function __construct( |
86
|
|
|
Item $item, |
87
|
|
|
Workflow $workflow, |
88
|
|
|
$transitionName, |
89
|
|
|
TransactionHandler $transactionHandler |
90
|
|
|
) { |
91
|
|
|
$this->item = $item; |
92
|
|
|
$this->workflow = $workflow; |
93
|
|
|
$this->transitionName = $transitionName; |
94
|
|
|
$this->transactionHandler = $transactionHandler; |
95
|
|
|
$this->context = new Context(); |
96
|
|
|
|
97
|
|
|
$this->guardAllowedTransition($transitionName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getTransition(): Transition |
104
|
|
|
{ |
105
|
|
|
if ($this->isWorkflowStarted()) { |
106
|
|
|
return $this->workflow->getTransition($this->transitionName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->workflow->getStartTransition(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getWorkflow(): Workflow |
116
|
|
|
{ |
117
|
|
|
return $this->workflow; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getItem(): Item |
124
|
|
|
{ |
125
|
|
|
return $this->item; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function getContext(): Context |
132
|
|
|
{ |
133
|
|
|
return $this->context; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function isWorkflowStarted(): bool |
140
|
|
|
{ |
141
|
|
|
return $this->item->isWorkflowStarted(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function getRequiredPayloadProperties(): array |
148
|
|
|
{ |
149
|
|
|
return $this->getTransition()->getRequiredPayloadProperties($this->item); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Consider if transition is available. |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isAvailable(): bool |
158
|
|
|
{ |
159
|
|
|
return $this->getTransition()->isAvailable($this->item, $this->context); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getCurrentStep():? Step |
166
|
|
|
{ |
167
|
|
|
if ($this->isWorkflowStarted()) { |
168
|
|
|
$stepName = $this->item->getCurrentStepName(); |
169
|
|
|
|
170
|
|
|
return $this->workflow->getStep($stepName); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function validate(array $payload): bool |
180
|
|
|
{ |
181
|
|
|
// first build the form |
182
|
|
|
$this->context = $this->context->withEmptyErrorCollection(); |
183
|
|
|
$this->validated = false; |
184
|
|
|
|
185
|
|
|
// check pre conditions first |
186
|
|
|
if ($this->getTransition()->checkPreCondition($this->item, $this->context)) { |
187
|
|
|
$this->validated = true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if ($this->validated |
191
|
|
|
&& !$this->getTransition()->checkCondition($this->item, $this->context) |
192
|
|
|
) { |
193
|
|
|
$this->validated = false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->validated; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Execute the transition. |
201
|
|
|
* |
202
|
|
|
* @return State |
203
|
|
|
*/ |
204
|
|
|
protected function executeTransition(): State |
205
|
|
|
{ |
206
|
|
|
$transition = $this->getTransition(); |
207
|
|
|
$success = $transition->executeActions($this->item, $this->context); |
208
|
|
|
|
209
|
|
|
if ($this->isWorkflowStarted()) { |
210
|
|
|
$state = $this->getItem()->transit($transition, $this->context, $success); |
211
|
|
|
} else { |
212
|
|
|
$state = $this->getItem()->start($transition, $this->context, $success); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$transition->executePostActions($this->item, $this->context); |
216
|
|
|
|
217
|
|
|
return $state; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Guard that transition was validated before. |
222
|
|
|
* |
223
|
|
|
* @throws WorkflowException If transition. |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
protected function guardValidated(): void |
228
|
|
|
{ |
229
|
|
|
if ($this->validated === null) { |
230
|
|
|
throw new WorkflowException('Transition was not validated so far.'); |
231
|
|
|
} elseif (!$this->validated) { |
232
|
|
|
throw new WorkflowException('Transition is in a invalid state and can\'t be processed.'); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Guard that requested transition is allowed. |
238
|
|
|
* |
239
|
|
|
* @param string|null $transitionName Transition to be processed. |
240
|
|
|
* |
241
|
|
|
* @throws WorkflowException If Transition is not allowed. |
242
|
|
|
* |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
private function guardAllowedTransition(?string $transitionName): void |
246
|
|
|
{ |
247
|
|
|
if (!$this->isWorkflowStarted()) { |
248
|
|
|
if (!$transitionName || $transitionName === $this->getWorkflow()->getStartTransition()->getName()) { |
|
|
|
|
249
|
|
|
return; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
throw new WorkflowException( |
253
|
|
|
sprintf( |
254
|
|
|
'Not allowed to process transition "%s". Workflow "%s" not started for item "%s"', |
255
|
|
|
$transitionName, |
256
|
|
|
$this->workflow->getName(), |
257
|
|
|
$this->item->getEntityId() |
258
|
|
|
) |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$step = $this->getCurrentStep(); |
263
|
|
|
|
264
|
|
|
if (!$step->isTransitionAllowed($transitionName)) { |
265
|
|
|
throw new WorkflowException( |
266
|
|
|
sprintf( |
267
|
|
|
'Not allowed to process transition "%s". Transition is not allowed in step "%s"', |
268
|
|
|
$transitionName, |
269
|
|
|
$step->getName() |
270
|
|
|
) |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: