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 Assert\Assertion; |
18
|
|
|
use Netzmacht\Workflow\Data\EntityId; |
19
|
|
|
use Netzmacht\Workflow\Exception\WorkflowException; |
20
|
|
|
use Netzmacht\Workflow\Flow\Exception\FlowException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Item stores workflow related data of an entity. It knows the state history and the current state. |
24
|
|
|
* |
25
|
|
|
* @package Netzmacht\Workflow |
26
|
|
|
*/ |
27
|
|
|
class Item |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Workflow name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $workflowName; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Current step name. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $currentStepName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* State the items already had. |
45
|
|
|
* |
46
|
|
|
* @var State[] |
47
|
|
|
*/ |
48
|
|
|
private $stateHistory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Workflow entity. |
52
|
|
|
* |
53
|
|
|
* @var mixed |
54
|
|
|
*/ |
55
|
|
|
private $entity; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Entity id. |
59
|
|
|
* |
60
|
|
|
* @var EntityId |
61
|
|
|
*/ |
62
|
|
|
private $entityId; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Construct. Do not used constructor. Use named constructor static methods. |
66
|
|
|
* |
67
|
|
|
* @param EntityId $entityId The entity id. |
68
|
|
|
* @param mixed $entity The entity for which the workflow is started. |
69
|
|
|
*/ |
70
|
|
|
protected function __construct(EntityId $entityId, $entity) |
71
|
|
|
{ |
72
|
|
|
$this->entityId = $entityId; |
73
|
|
|
$this->entity = $entity; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Initialize a new workflow item. |
78
|
|
|
* |
79
|
|
|
* It is called before the workflow is started. |
80
|
|
|
* |
81
|
|
|
* @param EntityId $entityId The entity id for the containing entity. |
82
|
|
|
* @param mixed $entity The entity for which the workflow is started. |
83
|
|
|
* |
84
|
|
|
* @return Item |
85
|
|
|
*/ |
86
|
|
|
public static function initialize(EntityId $entityId, $entity): self |
87
|
|
|
{ |
88
|
|
|
return new Item($entityId, $entity); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Restore an existing item. |
93
|
|
|
* |
94
|
|
|
* @param EntityId $entityId The entity id. |
95
|
|
|
* @param mixed $entity The entity. |
96
|
|
|
* @param State[]|iterable $stateHistory Set or already passed states. |
97
|
|
|
* |
98
|
|
|
* @return Item |
99
|
|
|
*/ |
100
|
|
|
public static function reconstitute(EntityId $entityId, $entity, iterable $stateHistory): Item |
101
|
|
|
{ |
102
|
|
|
Assertion::allIsInstanceOf($stateHistory, State::class); |
103
|
|
|
|
104
|
|
|
$item = self::initialize($entityId, $entity); |
105
|
|
|
|
106
|
|
|
// replay states |
107
|
|
|
foreach ($stateHistory as $state) { |
108
|
|
|
$item->apply($state); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $item; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Start an item and return current state. |
116
|
|
|
* |
117
|
|
|
* @param Transition $transition The transition being executed. |
118
|
|
|
* @param Context $context The transition context. |
119
|
|
|
* @param bool $success The transition success. |
120
|
|
|
* |
121
|
|
|
* @return State |
122
|
|
|
* |
123
|
|
|
* @throws WorkflowException If workflow is already started. |
124
|
|
|
*/ |
125
|
|
|
public function start( |
126
|
|
|
Transition $transition, |
127
|
|
|
Context $context, |
128
|
|
|
bool $success |
129
|
|
|
): State { |
130
|
|
|
$this->guardNotStarted(); |
131
|
|
|
|
132
|
|
|
$state = State::start($this->entityId, $transition, $context, $success); |
133
|
|
|
$this->apply($state); |
134
|
|
|
|
135
|
|
|
return $state; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Transits to a new state and return it. |
140
|
|
|
* |
141
|
|
|
* @param Transition $transition The transition being executed. |
142
|
|
|
* @param Context $context The transition context. |
143
|
|
|
* @param bool $success The transition success. |
144
|
|
|
* |
145
|
|
|
* @throws WorkflowException If workflow is not started. |
146
|
|
|
* |
147
|
|
|
* @return State |
148
|
|
|
*/ |
149
|
|
|
public function transit( |
150
|
|
|
Transition $transition, |
151
|
|
|
Context $context, |
152
|
|
|
bool $success |
153
|
|
|
): State { |
154
|
|
|
$this->guardStarted(); |
155
|
|
|
|
156
|
|
|
$state = $this->getLatestState(); |
157
|
|
|
$state = $state->transit($transition, $context, $success); |
158
|
|
|
|
159
|
|
|
$this->apply($state); |
160
|
|
|
|
161
|
|
|
return $state; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the name of the current step. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getCurrentStepName(): ?string |
170
|
|
|
{ |
171
|
|
|
return $this->currentStepName; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the entity id. |
176
|
|
|
* |
177
|
|
|
* @return EntityId |
178
|
|
|
*/ |
179
|
|
|
public function getEntityId(): EntityId |
180
|
|
|
{ |
181
|
|
|
return $this->entityId; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the entity. |
186
|
|
|
* |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function getEntity() |
190
|
|
|
{ |
191
|
|
|
return $this->entity; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the state history of the workflow item. |
196
|
|
|
* |
197
|
|
|
* @return State[]|iterable |
198
|
|
|
*/ |
199
|
|
|
public function getStateHistory(): iterable |
200
|
|
|
{ |
201
|
|
|
return $this->stateHistory; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get latest successful state. |
206
|
|
|
* |
207
|
|
|
* @param bool $successfulOnly Return only success ful steps. |
208
|
|
|
* |
209
|
|
|
* @return State|false |
210
|
|
|
*/ |
211
|
|
|
public function getLatestState(bool $successfulOnly = true) |
212
|
|
|
{ |
213
|
|
|
if (!$successfulOnly) { |
214
|
|
|
return end($this->stateHistory); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
for ($index = (count($this->stateHistory) - 1); $index >= 0; $index--) { |
218
|
|
|
if ($this->stateHistory[$index]->isSuccessful()) { |
219
|
|
|
return $this->stateHistory[$index]; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get name of the workflow. |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function getWorkflowName(): ?string |
232
|
|
|
{ |
233
|
|
|
return $this->workflowName; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Consider if workflow has started. |
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function isWorkflowStarted(): bool |
242
|
|
|
{ |
243
|
|
|
return !empty($this->currentStepName); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Detach item from current workflow. |
248
|
|
|
* |
249
|
|
|
* You should only use it with care if the workflow has changed and there is no way to finish it. |
250
|
|
|
* |
251
|
|
|
* @return void |
252
|
|
|
*/ |
253
|
|
|
public function detach(): void |
254
|
|
|
{ |
255
|
|
|
$this->currentStepName = null; |
256
|
|
|
$this->workflowName = null; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Guard that workflow of item was not already started. |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
* |
264
|
|
|
* @throws FlowException If item workflow process was already started. |
265
|
|
|
*/ |
266
|
|
|
private function guardNotStarted(): void |
267
|
|
|
{ |
268
|
|
|
if ($this->isWorkflowStarted()) { |
269
|
|
|
throw new FlowException('Item is already started.'); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Guard that workflow of item is started. |
275
|
|
|
* |
276
|
|
|
* @return void |
277
|
|
|
* |
278
|
|
|
* @throws FlowException If item workflow process was not started. |
279
|
|
|
*/ |
280
|
|
|
private function guardStarted(): void |
281
|
|
|
{ |
282
|
|
|
if (!$this->isWorkflowStarted()) { |
283
|
|
|
throw new FlowException('Item has not started yet.'); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Apply a new state. |
289
|
|
|
* |
290
|
|
|
* @param State $state The state being assigned. |
291
|
|
|
* |
292
|
|
|
* @return void |
293
|
|
|
*/ |
294
|
|
|
private function apply(State $state): void |
295
|
|
|
{ |
296
|
|
|
// only change current step if transition was successful |
297
|
|
|
if ($state->isSuccessful()) { |
298
|
|
|
$this->currentStepName = $state->getStepName(); |
299
|
|
|
$this->workflowName = $state->getWorkflowName(); |
300
|
|
|
} elseif (!$this->isWorkflowStarted()) { |
301
|
|
|
$this->workflowName = $state->getWorkflowName(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$this->stateHistory[] = $state; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|