Complex classes like StateMachine often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StateMachine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class StateMachine implements StateMachineInterface |
||
|
|||
24 | { |
||
25 | /** |
||
26 | * The stateful object |
||
27 | * |
||
28 | * @var object |
||
29 | */ |
||
30 | protected $object; |
||
31 | |||
32 | /** |
||
33 | * The available states |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $states = array(); |
||
38 | |||
39 | /** |
||
40 | * The available transitions |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $transitions = array(); |
||
45 | |||
46 | /** |
||
47 | * The current state |
||
48 | * |
||
49 | * @var StateInterface |
||
50 | */ |
||
51 | protected $currentState; |
||
52 | |||
53 | /** |
||
54 | * @var EventDispatcherInterface |
||
55 | */ |
||
56 | protected $dispatcher; |
||
57 | |||
58 | /** |
||
59 | * @var StateAccessorInterface |
||
60 | */ |
||
61 | protected $stateAccessor; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $graph; |
||
67 | |||
68 | /** |
||
69 | * @param object $object |
||
70 | * @param EventDispatcherInterface $dispatcher |
||
71 | * @param StateAccessorInterface $stateAccessor |
||
72 | */ |
||
73 | 200 | public function __construct( |
|
83 | |||
84 | /** |
||
85 | * @{inheritDoc} |
||
86 | */ |
||
87 | 145 | public function initialize() |
|
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | * |
||
117 | * @throws Exception\StateException |
||
118 | */ |
||
119 | 20 | public function apply($transitionName, array $parameters = array()) |
|
120 | { |
||
121 | 20 | $transition = $this->getTransition($transitionName); |
|
122 | 20 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this, $parameters); |
|
123 | 20 | if (!$this->can($transition, $parameters)) { |
|
124 | 5 | throw new Exception\StateException(sprintf( |
|
125 | 5 | 'The "%s" transition can not be applied to the "%s" state of object "%s" with graph "%s".', |
|
126 | 5 | $transition->getName(), |
|
127 | 5 | $this->currentState->getName(), |
|
128 | 5 | get_class($this->getObject()), |
|
129 | 5 | $this->getGraph() |
|
130 | 4 | )); |
|
131 | } |
||
132 | |||
133 | 20 | $this->dispatcher->dispatch(FiniteEvents::PRE_TRANSITION, $event); |
|
134 | 20 | $this->dispatcher->dispatch(FiniteEvents::PRE_TRANSITION . '.' . $transitionName, $event); |
|
135 | 20 | if (null !== $this->getGraph()) { |
|
136 | 10 | $this->dispatcher->dispatch(FiniteEvents::PRE_TRANSITION . '.' . $this->getGraph() . '.' . $transition->getName(), $event); |
|
137 | 8 | } |
|
138 | |||
139 | 20 | $returnValue = $transition->process($this); |
|
140 | 20 | $this->stateAccessor->setState($this->object, $transition->getState()); |
|
141 | 20 | $this->currentState = $this->getState($transition->getState()); |
|
142 | |||
143 | 20 | $this->dispatcher->dispatch(FiniteEvents::POST_TRANSITION, $event); |
|
144 | 20 | $this->dispatcher->dispatch(FiniteEvents::POST_TRANSITION . '.' . $transitionName, $event); |
|
145 | 20 | if (null !== $this->getGraph()) { |
|
146 | 10 | $this->dispatcher->dispatch(FiniteEvents::POST_TRANSITION . '.' . $this->getGraph() . '.' . $transition->getName(), $event); |
|
147 | 8 | } |
|
148 | |||
149 | 20 | return $returnValue; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * {@inheritDoc} |
||
154 | */ |
||
155 | 55 | public function can($transition, array $parameters = array()) |
|
156 | { |
||
157 | 55 | $transition = $transition instanceof TransitionInterface ? $transition : $this->getTransition($transition); |
|
158 | |||
159 | 55 | if (null !== $transition->getGuard() && !call_user_func($transition->getGuard(), $this)) { |
|
160 | 5 | return false; |
|
161 | } |
||
162 | |||
163 | 50 | if (!in_array($transition->getName(), $this->getCurrentState()->getTransitions())) { |
|
164 | 30 | return false; |
|
165 | } |
||
166 | |||
167 | 50 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this, $parameters); |
|
168 | 50 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION, $event); |
|
169 | 50 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION . '.' . $transition->getName(), $event); |
|
170 | 50 | if (null !== $this->getGraph()) { |
|
171 | 10 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION . '.' . $this->getGraph() . '.' . $transition->getName(), $event); |
|
172 | 8 | } |
|
173 | |||
174 | 50 | return !$event->isRejected(); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @{inheritDoc} |
||
179 | */ |
||
180 | 165 | public function addState($state) |
|
188 | |||
189 | /** |
||
190 | * @{inheritDoc} |
||
191 | */ |
||
192 | 160 | public function addTransition($transition, $initialState = null, $finalState = null) |
|
229 | |||
230 | /** |
||
231 | * @{inheritDoc} |
||
232 | */ |
||
233 | 145 | public function getTransition($name) |
|
246 | |||
247 | /** |
||
248 | * @{inheritDoc} |
||
249 | */ |
||
250 | 165 | public function getState($name) |
|
265 | |||
266 | /** |
||
267 | * @{inheritDoc} |
||
268 | */ |
||
269 | 5 | public function getTransitions() |
|
273 | |||
274 | /** |
||
275 | * @{inheritDoc} |
||
276 | */ |
||
277 | 5 | public function getStates() |
|
281 | |||
282 | /** |
||
283 | * {@inheritDoc} |
||
284 | */ |
||
285 | 135 | public function setObject($object) |
|
289 | |||
290 | /** |
||
291 | * @{inheritDoc} |
||
292 | */ |
||
293 | 160 | public function getObject() |
|
297 | |||
298 | /** |
||
299 | * @{inheritDoc} |
||
300 | */ |
||
301 | 110 | public function getCurrentState() |
|
305 | |||
306 | /** |
||
307 | * Find and return the Initial state if exists |
||
308 | * |
||
309 | * @return string |
||
310 | * |
||
311 | * @throws Exception\StateException |
||
312 | */ |
||
313 | 20 | protected function findInitialState() |
|
327 | |||
328 | /** |
||
329 | * @param EventDispatcherInterface $dispatcher |
||
330 | */ |
||
331 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
335 | |||
336 | /** |
||
337 | * @return EventDispatcherInterface |
||
338 | */ |
||
339 | 5 | public function getDispatcher() |
|
340 | { |
||
341 | 5 | return $this->dispatcher; |
|
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param StateAccessorInterface $stateAccessor |
||
346 | */ |
||
347 | 10 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
|
348 | { |
||
349 | 10 | $this->stateAccessor = $stateAccessor; |
|
350 | 10 | } |
|
351 | |||
352 | /** |
||
353 | * @{inheritDoc} |
||
354 | */ |
||
355 | 15 | public function setGraph($graph) |
|
359 | |||
360 | /** |
||
361 | * @{inheritDoc} |
||
362 | */ |
||
363 | 160 | public function getGraph() |
|
367 | } |
||
368 |