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 DateTimeImmutable; |
18
|
|
|
use Netzmacht\Workflow\Data\EntityId; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class State stores information of a current state of an entity. |
22
|
|
|
* |
23
|
|
|
* @package Netzmacht\Workflow\Flow |
24
|
|
|
*/ |
25
|
|
|
class State |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The state id. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $stateId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The entity id. |
36
|
|
|
* |
37
|
|
|
* @var EntityId |
38
|
|
|
*/ |
39
|
|
|
private $entityId; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Store if transition was successful. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $successful; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The last transition. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $transitionName; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The current step. |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $stepName; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Date being stored. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private $data = array(); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Date when state was reached. |
71
|
|
|
* |
72
|
|
|
* @var DateTimeImmutable |
73
|
|
|
*/ |
74
|
|
|
private $reachedAt; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* List of errors. |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
private $errors; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Name of the workflow. |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
private $workflowName; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Construct. |
92
|
|
|
* |
93
|
|
|
* @param EntityId $entityId The entity id. |
94
|
|
|
* @param string $workflowName Workflow name. |
95
|
|
|
* @param string $transitionName The transition executed to reach the step. |
96
|
|
|
* @param string $stepToName The step reached after transition. |
97
|
|
|
* @param bool $successful Consider if transition was successful. |
98
|
|
|
* @param array $data Stored data. |
99
|
|
|
* @param DateTimeImmutable $reachedAt Time when state was reached. |
100
|
|
|
* @param array $errors List of errors. |
101
|
|
|
* @param int $stateId The state id of a persisted state. |
102
|
|
|
* |
103
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList) |
104
|
|
|
*/ |
105
|
|
|
public function __construct( |
106
|
|
|
EntityId $entityId, |
107
|
|
|
string $workflowName, |
108
|
|
|
string $transitionName, |
109
|
|
|
string $stepToName, |
110
|
|
|
bool $successful, |
111
|
|
|
array $data, |
112
|
|
|
DateTimeImmutable $reachedAt, |
113
|
|
|
array $errors = array(), |
114
|
|
|
int $stateId = null |
115
|
|
|
) { |
116
|
|
|
$this->entityId = $entityId; |
117
|
|
|
$this->workflowName = $workflowName; |
118
|
|
|
$this->transitionName = $transitionName; |
119
|
|
|
$this->stepName = $stepToName; |
120
|
|
|
$this->successful = $successful; |
121
|
|
|
$this->data = $data; |
122
|
|
|
$this->reachedAt = $reachedAt; |
123
|
|
|
$this->errors = $errors; |
124
|
|
|
$this->stateId = $stateId; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create an initial state. |
129
|
|
|
* |
130
|
|
|
* @param EntityId $entityId The entity id. |
131
|
|
|
* @param Transition $transition The current executed transition. |
132
|
|
|
* @param Context $context The context. |
133
|
|
|
* @param bool $success Success state. |
134
|
|
|
* |
135
|
|
|
* @return State |
136
|
|
|
*/ |
137
|
|
|
public static function start( |
138
|
|
|
EntityId $entityId, |
139
|
|
|
Transition $transition, |
140
|
|
|
Context $context, |
141
|
|
|
$success |
142
|
|
|
) { |
143
|
|
|
$state = new State( |
144
|
|
|
$entityId, |
145
|
|
|
$transition->getWorkflow()->getName(), |
146
|
|
|
$transition->getName(), |
147
|
|
|
$transition->getStepTo()->getName(), |
148
|
|
|
$success, |
149
|
|
|
$context->getProperties()->toArray(), |
150
|
|
|
new \DateTimeImmutable(), |
151
|
|
|
$context->getErrorCollection()->toArray() |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return $state; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get step name. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getStepName(): string |
163
|
|
|
{ |
164
|
|
|
return $this->stepName; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get transition name. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getTransitionName(): string |
173
|
|
|
{ |
174
|
|
|
return $this->transitionName; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the workflow name. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getWorkflowName(): string |
183
|
|
|
{ |
184
|
|
|
return $this->workflowName; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get state data. |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function getData(): array |
193
|
|
|
{ |
194
|
|
|
return $this->data; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get reached at time. |
199
|
|
|
* |
200
|
|
|
* @return DateTimeImmutable |
201
|
|
|
*/ |
202
|
|
|
public function getReachedAt(): \DateTimeImmutable |
203
|
|
|
{ |
204
|
|
|
return $this->reachedAt; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Consider if state is successful. |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function isSuccessful(): bool |
213
|
|
|
{ |
214
|
|
|
return $this->successful; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the entity id. |
219
|
|
|
* |
220
|
|
|
* @return EntityId |
221
|
|
|
*/ |
222
|
|
|
public function getEntityId(): EntityId |
223
|
|
|
{ |
224
|
|
|
return $this->entityId; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get error messages. |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public function getErrors(): array |
233
|
|
|
{ |
234
|
|
|
return $this->errors; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get state id. |
239
|
|
|
* |
240
|
|
|
* @return int|null |
241
|
|
|
*/ |
242
|
|
|
public function getStateId():? int |
243
|
|
|
{ |
244
|
|
|
return $this->stateId; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Transit to a new state. |
249
|
|
|
* |
250
|
|
|
* @param Transition $transition The transition being performed. |
251
|
|
|
* @param Context $context The transition context. |
252
|
|
|
* @param bool $success The success state. |
253
|
|
|
* |
254
|
|
|
* @return State |
255
|
|
|
*/ |
256
|
|
|
public function transit( |
257
|
|
|
Transition $transition, |
258
|
|
|
Context $context, |
259
|
|
|
bool $success = true |
260
|
|
|
): State { |
261
|
|
|
$dateTime = new DateTimeImmutable(); |
262
|
|
|
$stepName = $success ? $transition->getStepTo()->getName() : $this->stepName; |
263
|
|
|
|
264
|
|
|
return new static( |
265
|
|
|
$this->entityId, |
266
|
|
|
$this->workflowName, |
267
|
|
|
$transition->getName(), |
268
|
|
|
$stepName, |
269
|
|
|
$success, |
270
|
|
|
$context->getProperties()->toArray(), |
271
|
|
|
$dateTime, |
272
|
|
|
$context->getErrorCollection()->getErrors() |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|