1818namespace Elcodi \Component \StateTransitionMachine \Machine ;
1919
2020use stdClass ;
21- use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
2221
2322use Elcodi \Component \StateTransitionMachine \Definition \Transition ;
24- use Elcodi \Component \StateTransitionMachine \ElcodiStateTransitionMachineEvents ;
2523use Elcodi \Component \StateTransitionMachine \Entity \Interfaces \StateLineInterface ;
2624use Elcodi \Component \StateTransitionMachine \Entity \StateLineStack ;
27- use Elcodi \Component \StateTransitionMachine \Event \InitializationEvent ;
28- use Elcodi \Component \StateTransitionMachine \Event \TransitionEvent ;
25+ use Elcodi \Component \StateTransitionMachine \EventDispatcher \MachineEventDispatcher ;
2926use Elcodi \Component \StateTransitionMachine \Exception \ObjectAlreadyInitializedException ;
3027use Elcodi \Component \StateTransitionMachine \Exception \ObjectNotInitializedException ;
3128use Elcodi \Component \StateTransitionMachine \Exception \StateNotReachableException ;
@@ -47,11 +44,11 @@ class MachineManager
4744 private $ machine ;
4845
4946 /**
50- * @var EventDispatcherInterface
47+ * @var MachineEventDispatcher
5148 *
52- * Event Dispatcher
49+ * Machine Event Dispatcher
5350 */
54- private $ eventDispatcher ;
51+ private $ machineEventDispatcher ;
5552
5653 /**
5754 * @var StateLineFactory
@@ -63,17 +60,17 @@ class MachineManager
6360 /**
6461 * Construct
6562 *
66- * @param MachineInterface $machine Machine
67- * @param EventDispatcherInterface $eventDispatcher Event Dispatcher
68- * @param StateLineFactory $stateLineFactory StateLine Factory
63+ * @param MachineInterface $machine Machine
64+ * @param MachineEventDispatcher $machineEventDispatcher Event Dispatcher
65+ * @param StateLineFactory $stateLineFactory StateLine Factory
6966 */
7067 public function __construct (
7168 MachineInterface $ machine ,
72- EventDispatcherInterface $ eventDispatcher ,
69+ MachineEventDispatcher $ machineEventDispatcher ,
7370 StateLineFactory $ stateLineFactory
7471 ) {
7572 $ this ->machine = $ machine ;
76- $ this ->eventDispatcher = $ eventDispatcher ;
73+ $ this ->machineEventDispatcher = $ machineEventDispatcher ;
7774 $ this ->stateLineFactory = $ stateLineFactory ;
7875 }
7976
@@ -108,11 +105,13 @@ public function initialize(
108105
109106 $ stateLineStack ->addStateLine ($ stateLine );
110107
111- $ this ->dispatchInitializationEvents (
112- $ this ->machine ,
113- $ object ,
114- $ stateLineStack
115- );
108+ $ this
109+ ->machineEventDispatcher
110+ ->dispatchInitializationEvents (
111+ $ this ->machine ,
112+ $ object ,
113+ $ stateLineStack
114+ );
116115
117116 return $ stateLineStack ;
118117 }
@@ -240,133 +239,15 @@ private function applyTransitionAction(
240239
241240 $ stateLineStack ->addStateLine ($ stateLine );
242241
243- $ this ->dispatchTransitionEvents (
244- $ this ->machine ,
245- $ object ,
246- $ stateLineStack ,
247- $ transition
248- );
249-
250- return $ stateLineStack ;
251- }
252-
253- /**
254- * Throw initialization events
255- *
256- * @param MachineInterface $machine Machine
257- * @param stdClass $object Object
258- * @param StateLineStack $stateLineStack StateLine Stack
259- *
260- * @return $this Self object
261- */
262- private function dispatchInitializationEvents (
263- MachineInterface $ machine ,
264- $ object ,
265- StateLineStack $ stateLineStack
266- ) {
267242 $ this
268- ->eventDispatcher
269- ->dispatch (
270- str_replace (
271- '{machine_id} ' ,
272- $ machine ->getId (),
273- ElcodiStateTransitionMachineEvents::INITIALIZATION
274- ),
275- InitializationEvent::create (
276- $ object ,
277- $ stateLineStack
278- )
279- );
280- }
281-
282- /**
283- * Throw transition events
284- *
285- * @param MachineInterface $machine Machine
286- * @param stdClass $object Object
287- * @param StateLineStack $stateLineStack StateLine Stack
288- * @param Transition $transition Transition
289- *
290- * @return $this Self object
291- */
292- private function dispatchTransitionEvents (
293- MachineInterface $ machine ,
294- $ object ,
295- StateLineStack $ stateLineStack ,
296- Transition $ transition
297- ) {
298- $ this
299- ->eventDispatcher
300- ->dispatch (
301- ElcodiStateTransitionMachineEvents::ALL_TRANSITIONS ,
302- TransitionEvent::create (
303- $ object ,
304- $ stateLineStack ,
305- $ transition
306- )
307- );
308-
309- $ this
310- ->eventDispatcher
311- ->dispatch (
312- str_replace (
313- [
314- '{machine_id} ' ,
315- '{state_name} ' ,
316- ],
317- [
318- $ machine ->getId (),
319- $ transition ->getStart ()->getName (),
320- ],
321- ElcodiStateTransitionMachineEvents::TRANSITION_FROM_STATE
322- ),
323- TransitionEvent::create (
324- $ object ,
325- $ stateLineStack ,
326- $ transition
327- )
328- );
329-
330- $ this
331- ->eventDispatcher
332- ->dispatch (
333- str_replace (
334- [
335- '{machine_id} ' ,
336- '{state_name} ' ,
337- ],
338- [
339- $ machine ->getId (),
340- $ transition ->getFinal ()->getName (),
341- ],
342- ElcodiStateTransitionMachineEvents::TRANSITION_TO_STATE
343- ),
344- TransitionEvent::create (
345- $ object ,
346- $ stateLineStack ,
347- $ transition
348- )
243+ ->machineEventDispatcher
244+ ->dispatchTransitionEvents (
245+ $ this ->machine ,
246+ $ object ,
247+ $ stateLineStack ,
248+ $ transition
349249 );
350250
351- $ this
352- ->eventDispatcher
353- ->dispatch (
354- str_replace (
355- [
356- '{machine_id} ' ,
357- '{transition_name} ' ,
358- ],
359- [
360- $ machine ->getId (),
361- $ transition ->getName (),
362- ],
363- ElcodiStateTransitionMachineEvents::TRANSITION
364- ),
365- TransitionEvent::create (
366- $ object ,
367- $ stateLineStack ,
368- $ transition
369- )
370- );
251+ return $ stateLineStack ;
371252 }
372253}
0 commit comments