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\Manager; |
16
|
|
|
|
17
|
|
|
use Assert\Assertion; |
18
|
|
|
use Netzmacht\Workflow\Data\EntityId; |
19
|
|
|
use Netzmacht\Workflow\Data\StateRepository; |
20
|
|
|
use Netzmacht\Workflow\Exception\WorkflowNotFound; |
21
|
|
|
use Netzmacht\Workflow\Flow\Exception\FlowException; |
22
|
|
|
use Netzmacht\Workflow\Flow\Item; |
23
|
|
|
use Netzmacht\Workflow\Flow\Workflow; |
24
|
|
|
use Netzmacht\Workflow\Handler\TransitionHandler; |
25
|
|
|
use Netzmacht\Workflow\Handler\TransitionHandlerFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Manager handles a set of workflows. |
29
|
|
|
* |
30
|
|
|
* Usually there will a different workflow manager for different workflow types. The manager is the API entry point |
31
|
|
|
* when using the workflow API. |
32
|
|
|
* |
33
|
|
|
* @package Netzmacht\Workflow |
34
|
|
|
*/ |
35
|
|
|
class WorkflowManager implements Manager |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* The state repository. |
39
|
|
|
* |
40
|
|
|
* @var StateRepository |
41
|
|
|
*/ |
42
|
|
|
private $stateRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* A set of workflows. |
46
|
|
|
* |
47
|
|
|
* @var Workflow[] |
48
|
|
|
*/ |
49
|
|
|
private $workflows; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* A Transition handler factory. |
53
|
|
|
* |
54
|
|
|
* @var TransitionHandlerFactory |
55
|
|
|
*/ |
56
|
|
|
private $handlerFactory; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Construct. |
60
|
|
|
* |
61
|
|
|
* @param TransitionHandlerFactory $handlerFactory The transition handler factory. |
62
|
|
|
* @param StateRepository $stateRepository The state repository. |
63
|
|
|
* @param Workflow[] $workflows The set of managed workflows. |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
TransitionHandlerFactory $handlerFactory, |
67
|
|
|
StateRepository $stateRepository, |
68
|
|
|
$workflows = [] |
69
|
|
|
) { |
70
|
|
|
Assertion::allIsInstanceOf($workflows, Workflow::class); |
71
|
|
|
|
72
|
|
|
$this->workflows = $workflows; |
73
|
|
|
$this->handlerFactory = $handlerFactory; |
74
|
|
|
$this->stateRepository = $stateRepository; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function handle(Item $item, string $transitionName = null, bool $changeWorkflow = false): ?TransitionHandler |
81
|
|
|
{ |
82
|
|
|
$entity = $item->getEntity(); |
83
|
|
|
|
84
|
|
|
if (!$this->hasWorkflow($item->getEntityId(), $entity)) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$workflow = $this->getWorkflow($item->getEntityId(), $entity); |
89
|
|
|
|
90
|
|
|
if ($this->hasWorkflowChanged($item, $workflow, !$changeWorkflow) && $changeWorkflow) { |
91
|
|
|
$item->detach(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$handler = $this->handlerFactory->createTransitionHandler( |
95
|
|
|
$item, |
96
|
|
|
$workflow, |
97
|
|
|
$transitionName, |
98
|
|
|
$item->getEntityId()->getProviderName(), |
99
|
|
|
$this->stateRepository |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return $handler; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function addWorkflow(Workflow $workflow): Manager |
109
|
|
|
{ |
110
|
|
|
$this->workflows[] = $workflow; |
111
|
|
|
|
112
|
|
|
return $this; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
* |
118
|
|
|
* @throws WorkflowNotFound When no supporting workflow is found. |
119
|
|
|
*/ |
120
|
|
|
public function getWorkflow(EntityId $entityId, $entity): Workflow |
121
|
|
|
{ |
122
|
|
|
foreach ($this->workflows as $workflow) { |
123
|
|
|
if ($workflow->supports($entityId, $entity)) { |
124
|
|
|
return $workflow; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw WorkflowNotFound::forEntity($entityId); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
* |
134
|
|
|
* @throws WorkflowNotFound When no workflow with name is found. |
135
|
|
|
*/ |
136
|
|
|
public function getWorkflowByName(string $name): Workflow |
137
|
|
|
{ |
138
|
|
|
foreach ($this->workflows as $workflow) { |
139
|
|
|
if ($workflow->getName() == $name) { |
140
|
|
|
return $workflow; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
throw WorkflowNotFound::withName($name); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function getWorkflowByItem(Item $item): Workflow |
151
|
|
|
{ |
152
|
|
|
return $this->getWorkflow($item->getEntityId(), $item->getEntity()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function hasWorkflow(EntityId $entityId, $entity): bool |
159
|
|
|
{ |
160
|
|
|
foreach ($this->workflows as $workflow) { |
161
|
|
|
if ($workflow->supports($entityId, $entity)) { |
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function getWorkflows(): iterable |
173
|
|
|
{ |
174
|
|
|
return $this->workflows; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function createItem(EntityId $entityId, $entity): Item |
181
|
|
|
{ |
182
|
|
|
$stateHistory = $this->stateRepository->find($entityId); |
183
|
|
|
|
184
|
|
|
return Item::reconstitute($entityId, $entity, $stateHistory); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Guard that already started workflow is the same which is tried to be ran now. |
189
|
|
|
* |
190
|
|
|
* @param Item $item Current workflow item. |
191
|
|
|
* @param Workflow $workflow Selected workflow. |
192
|
|
|
* @param bool $throw If true an error is thrown. |
193
|
|
|
* |
194
|
|
|
* @throws FlowException If item workflow is not the same as current workflow. |
195
|
|
|
* |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
private function hasWorkflowChanged(Item $item, Workflow $workflow, bool $throw = true): bool |
199
|
|
|
{ |
200
|
|
|
if ($item->isWorkflowStarted() && $item->getWorkflowName() != $workflow->getName()) { |
201
|
|
|
$message = sprintf( |
202
|
|
|
'Item "%s" already process workflow "%s" and cannot be handled by "%s"', |
203
|
|
|
$item->getEntityId(), |
204
|
|
|
$item->getWorkflowName(), |
205
|
|
|
$workflow->getName() |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
if ($throw) { |
209
|
|
|
throw new FlowException($message); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.