22
33namespace Coff \SMF ;
44
5-
65use Coff \SMF \Assertion \Assertion ;
76use Coff \SMF \Assertion \CommonCallbackAssertion ;
87use Coff \SMF \Assertion \DefaultCallbackAssertion ;
@@ -34,20 +33,14 @@ public function allowTransition(StateEnum $stateFrom, StateEnum $stateTo, Assert
3433 {
3534 $ transition = new Transition ($ stateFrom , $ stateTo );
3635
37- /*
38- * Default assertion when no assertions
39- */
40- if (null == $ assertion )
41- {
36+ // Default assertion when no assertions
37+ if (null == $ assertion ) {
4238 $ assertion = new DefaultCallbackAssertion ();
4339 }
4440
4541 $ transition ->addAssertion ($ assertion );
4642
47-
48- /*
49- * Wire it up
50- */
43+ // Wire it up
5144 switch (true ) {
5245 case $ assertion instanceof CommonCallbackAssertion:
5346 // no break
@@ -73,7 +66,8 @@ public function allowTransition(StateEnum $stateFrom, StateEnum $stateTo, Assert
7366 * @return $this
7467 * @throws MachineException
7568 */
76- public function addTransition (TransitionInterface $ transition ) {
69+ public function addTransition (TransitionInterface $ transition )
70+ {
7771
7872 if (!$ transition ->getFromState () instanceof StateEnum) {
7973 throw new MachineException ('Transition is not ready to be set as allowed! ' );
@@ -89,63 +83,120 @@ public function addTransition(TransitionInterface $transition) {
8983 }
9084
9185 /**
92- * @param StateEnum $stateFrom
93- * @param StateEnum $stateTo
94- * @return Transition
95- * @throws MachineException
86+ * Verifies if machine's current state is equal to state given in parameter
87+ *
88+ * @param StateEnum $state
89+ * @return bool
9690 */
97- public function getTransition (StateEnum $ stateFrom , StateEnum $ stateTo ) {
98- if (isset ($ this ->allowedTransitions [(string )$ stateFrom ][(string )$ stateTo ])) {
99- return $ this ->allowedTransitions [(string )$ stateFrom ][(string )$ stateTo ];
100- } else {
101- throw new MachineException ('No transition object for ' . $ stateFrom . ' to ' . $ stateTo );
102- }
91+ public function isMachineState (StateEnum $ state ): bool
92+ {
93+ return (string )$ this ->getMachineState () == (string )$ state ? true : false ;
10394 }
10495
96+ /**
97+ * Returns machine's current state
98+ * @return StateEnum
99+ */
100+ public function getMachineState (): StateEnum
101+ {
102+ return $ this ->machineState ;
103+ }
105104
106- public function getAllowedTransitions (StateEnum $ state = null ) : array {
105+ /**
106+ * Machine-internal method for setting new state. Normally this should only be allowed from within the machine
107+ * assertState() method. To set machine's state externally create dedicated methods like:
108+ *
109+ * public function on() {
110+ * $this->setMachineState(...);
111+ * }
112+ *
113+ * @param $newState
114+ * @return $this
115+ * @throws TransitionException
116+ * @throws MachineException
117+ */
118+ protected function setMachineState (StateEnum $ newState )
119+ {
107120
108- if (null === $ state ) {
109- $ state = $ this ->getMachineState ();
121+ if (false === $ this -> isTransitionAllowed ( $ newState ) ) {
122+ throw new TransitionException ( ' State transition from ' . ( string ) $ this ->getMachineState () . ' to ' . ( string ) $ newState . ' is not allowed. ' );
110123 }
111124
112- return $ this ->allowedTransitions [(string )$ state ];
125+ $ oldState = $ this ->machineState ;
126+
127+ $ this ->machineState = $ newState ;
128+
129+ $ this ->onTransition ($ this ->getTransition ($ oldState , $ newState ));
130+
131+ return $ this ;
113132 }
114133
115- public function isTransitionAllowed (StateEnum $ state )
134+ /**
135+ * Verifies if transition is allowed from current state to the state given in parameter
136+ * @param StateEnum $state
137+ * @return bool
138+ */
139+ public function isTransitionAllowed (StateEnum $ state ): bool
116140 {
117141 return isset ($ this ->allowedTransitions [(string )$ this ->getMachineState ()][$ state ]) ? true : false ;
118142 }
119143
120- public function getMachineState () : StateEnum
144+ /**
145+ * Method called on any state transition occurence
146+ * @param Transition $transition
147+ * @return MachineInterface|void
148+ */
149+ public function onTransition (Transition $ transition )
121150 {
122- return $ this ->machineState ;
151+ // Default implementation does nothing. This can be used to dispatch events in kind-of EventAwareMachine you can
152+ // implement yourself.
123153 }
124154
125- public function isMachineState (StateEnum $ state ) : bool
155+ /**
156+ * @param StateEnum $stateFrom
157+ * @param StateEnum $stateTo
158+ * @return Transition
159+ * @throws MachineException
160+ */
161+ public function getTransition (StateEnum $ stateFrom , StateEnum $ stateTo )
126162 {
127- return (string ) $ this ->getMachineState () == (string ) $ state ? true : false ;
163+ if (isset ($ this ->allowedTransitions [(string )$ stateFrom ][(string )$ stateTo ])) {
164+ return $ this ->allowedTransitions [(string )$ stateFrom ][(string )$ stateTo ];
165+ } else {
166+ throw new MachineException ('No transition object for ' . $ stateFrom . ' to ' . $ stateTo );
167+ }
128168 }
129169
170+ /**
171+ * Sets machine's initial state
172+ * @param StateEnum $state
173+ * @return $this|MachineInterface
174+ */
130175 public function setInitState (StateEnum $ state )
131176 {
132177 $ this ->initState = $ state ;
133178
134179 return $ this ;
135180 }
136181
137- public function assertTransition (Transition $ transition ) : bool
182+ /**
183+ * Default handler method for CommonCallbackAssertion
184+ * @param Transition $transition
185+ * @return bool
186+ */
187+ public function assertTransition (Transition $ transition ): bool
138188 {
139-
140189 // default method just returns true
141190 return true ;
142191 }
143192
144193 /**
194+ * Runs the machine as far as following transitions return true
145195 * @return StateEnum
146196 * @throws TransitionException
147197 */
148- public function run () {
198+ public function run ()
199+ {
149200
150201 do {
151202 $ result = false ;
@@ -172,32 +223,18 @@ public function run() {
172223 return $ this ->machineState ;
173224 }
174225
175-
176226 /**
177- * Machine-internal method for setting new state. Normally this should only be allowed from within the machine
178- * assertState() method. To set machine's state externally create dedicated methods like:
179- *
180- * public function on() {
181- * $this->setMachineState(...);
182- * }
183- *
184- * @param $newState
185- * @return $this
186- * @throws TransitionException
187- * @throws MachineException
227+ * Returns Transitions allowed for current state or state specified in parameter
228+ * @param StateEnum|null $state
229+ * @return array
188230 */
189- protected function setMachineState (StateEnum $ newState ) {
231+ public function getAllowedTransitions (StateEnum $ state = null ): array
232+ {
190233
191- if (false === $ this -> isTransitionAllowed ( $ newState )) {
192- throw new TransitionException ( ' State transition from ' . ( string ) $ this ->getMachineState () . ' to ' . ( string ) $ newState . ' is not allowed. ' );
234+ if (null === $ state ) {
235+ $ state = $ this ->getMachineState ();
193236 }
194237
195- $ oldState = $ this ->machineState ;
196-
197- $ this ->machineState = $ newState ;
198-
199- $ this ->onTransition ($ this ->getTransition ($ oldState , $ newState ));
200-
201- return $ this ;
238+ return $ this ->allowedTransitions [(string )$ state ];
202239 }
203240}
0 commit comments