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