1 | <?php |
||
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( |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function handle(Item $item, string $transitionName = null, bool $changeWorkflow = false): ?TransitionHandler |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function addWorkflow(Workflow $workflow): Manager |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | * |
||
118 | * @throws WorkflowNotFound When no supporting workflow is found. |
||
119 | */ |
||
120 | public function getWorkflow(EntityId $entityId, $entity): Workflow |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | * |
||
134 | * @throws WorkflowNotFound When no workflow with name is found. |
||
135 | */ |
||
136 | public function getWorkflowByName(string $name): Workflow |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function getWorkflowByItem(Item $item): Workflow |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function hasWorkflow(EntityId $entityId, $entity): bool |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function getWorkflows(): iterable |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function createItem(EntityId $entityId, $entity): Item |
||
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 |
||
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.