1 | <?php |
||
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) |
||
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 |
||
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 |
||
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( |
||
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( |
||
163 | |||
164 | /** |
||
165 | * Get the name of the current step. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getCurrentStepName(): ?string |
||
173 | |||
174 | /** |
||
175 | * Get the entity id. |
||
176 | * |
||
177 | * @return EntityId |
||
178 | */ |
||
179 | public function getEntityId(): EntityId |
||
183 | |||
184 | /** |
||
185 | * Get the entity. |
||
186 | * |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function getEntity() |
||
193 | |||
194 | /** |
||
195 | * Get the state history of the workflow item. |
||
196 | * |
||
197 | * @return State[]|iterable |
||
198 | */ |
||
199 | public function getStateHistory(): iterable |
||
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) |
||
225 | |||
226 | /** |
||
227 | * Get name of the workflow. |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getWorkflowName(): ?string |
||
235 | |||
236 | /** |
||
237 | * Consider if workflow has started. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isWorkflowStarted(): bool |
||
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 |
||
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 |
||
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 |
||
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 |
||
306 | } |
||
307 |