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\FlowException; |
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 FlowException 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->createCleanCopy($payload); |
183
|
|
|
$this->validated = false; |
184
|
|
|
$transition = $this->getTransition(); |
185
|
|
|
|
186
|
|
|
// check pre conditions first |
187
|
|
|
if ($transition->checkPreCondition($this->item, $this->context)) { |
188
|
|
|
$this->validated = true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Validate the actions |
192
|
|
|
if (!$transition->validate($this->item, $this->context)) { |
193
|
|
|
$this->validated = false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if ($this->validated && !$transition->checkCondition($this->item, $this->context)) { |
197
|
|
|
$this->validated = false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->validated; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Execute the transition. |
205
|
|
|
* |
206
|
|
|
* @return State |
207
|
|
|
*/ |
208
|
|
|
protected function executeTransition(): State |
209
|
|
|
{ |
210
|
|
|
$transition = $this->getTransition(); |
211
|
|
|
$success = $transition->executeActions($this->item, $this->context); |
212
|
|
|
|
213
|
|
|
if ($this->isWorkflowStarted()) { |
214
|
|
|
$state = $this->getItem()->transit($transition, $this->context, $success); |
215
|
|
|
} else { |
216
|
|
|
$state = $this->getItem()->start($transition, $this->context, $success); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$transition->executePostActions($this->item, $this->context); |
220
|
|
|
|
221
|
|
|
return $state; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Guard that transition was validated before. |
226
|
|
|
* |
227
|
|
|
* @throws FlowException If transition. |
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
protected function guardValidated(): void |
232
|
|
|
{ |
233
|
|
|
if ($this->validated === null) { |
234
|
|
|
throw new FlowException('Transition was not validated so far.'); |
235
|
|
|
} elseif (!$this->validated) { |
236
|
|
|
throw new FlowException('Transition is in a invalid state and can\'t be processed.'); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Guard that requested transition is allowed. |
242
|
|
|
* |
243
|
|
|
* @param string|null $transitionName Transition to be processed. |
244
|
|
|
* |
245
|
|
|
* @throws FlowException If Transition is not allowed. |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
private function guardAllowedTransition(?string $transitionName): void |
250
|
|
|
{ |
251
|
|
|
if (!$this->isWorkflowStarted()) { |
252
|
|
|
if ($transitionName === null || $transitionName === $this->getWorkflow()->getStartTransition()->getName()) { |
253
|
|
|
return; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
throw new FlowException( |
257
|
|
|
sprintf( |
258
|
|
|
'Not allowed to process transition "%s". Workflow "%s" not started for item "%s"', |
259
|
|
|
$transitionName, |
260
|
|
|
$this->workflow->getName(), |
261
|
|
|
$this->item->getEntityId() |
262
|
|
|
) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$step = $this->getCurrentStep(); |
267
|
|
|
|
268
|
|
|
if (!$step->isTransitionAllowed($transitionName)) { |
269
|
|
|
throw new FlowException( |
270
|
|
|
sprintf( |
271
|
|
|
'Not allowed to process transition "%s". Transition is not allowed in step "%s"', |
272
|
|
|
$transitionName, |
273
|
|
|
$step->getName() |
274
|
|
|
) |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|