@@ -143,75 +143,31 @@ public function assertTransition(Transition $transition) : bool
143143
144144 public function run () {
145145
146- $ allowedTrans = $ this -> getAllowedTransitions ();
146+ do {
147147
148- /**
149- * @var StateEnum $nextState
150- * @var Transition $transition
151- */
152- foreach ($ allowedTrans as $ nextState => $ transition ) {
153-
154-
155- if (true === $ transition ->assert ()) {
156- $ this ->machineState = $ transition ->getToState ();
157-
158- return $ nextState ;
159- }
160- }
161-
162- return false ;
163- }
164-
165- public function validate () {
166-
167- $ allowedTrans = $ this ->getAllowedTransitions ($ this ->getInitState ());
168-
169- /**
170- * @var StateEnum $nextState
171- * @var Transition $transition
172- */
173- foreach ($ allowedTrans as $ nextState => $ transition ) {
174-
175-
176- if (true === $ transition ->assert ()) {
177- $ this ->machineState = $ transition ->getToState ();
178-
179- return $ nextState ;
180- }
181- }
182-
183- return false ;
184- }
185-
186- /**
187- * @param StateEnum $state
188- */
189- private function doValidate (StateEnum $ state ) {
148+ $ allowedTrans = $ this ->getAllowedTransitions ();
190149
191- $ allowedTrans = $ this -> getAllowedTransitions ( $ state );
192-
193- if ( 0 === count ( $ allowedTrans )) {
194- return false ;
195- }
150+ /**
151+ * @var StateEnum $nextState
152+ * @var Transition $transition
153+ */
154+ foreach ( $ allowedTrans as $ nextState => $ transition ) {
196155
197- /**
198- * @var StateEnum $nextState
199- * @var Transition $transition
200- */
201- foreach ($ allowedTrans as $ nextState => $ transition ) {
156+ $ result = $ transition ->assert ();
202157
203- if ($ transition ->getToState () === $ this ->getInitState ()) {
204- // transition path ends on initial state
205- } else {
206- if (false === $ this ->doValidate ($ transition ->getToState ())) {
158+ if (true === $ result ) {
159+ $ this ->machineState = $ transition ->getToState ();
207160
161+ $ this ->onTransition ($ transition );
208162 }
209163 }
210- }
211164
212- return true ;
165+ } while (true == $ result );
166+
167+ return $ this ->machineState ;
213168 }
214169
170+
215171 /**
216172 * Machine-internal method for setting new state. Normally this should only be allowed from within the machine
217173 * assertState() method. To set machine's state externally create dedicated methods like:
@@ -230,8 +186,12 @@ protected function setMachineState(StateEnum $newState) {
230186 throw new TransitionException ('State transition from ' . (string ) $ this ->getMachineState () . ' to ' . (string ) $ newState . 'is not allowed. ' );
231187 }
232188
189+ $ oldState = $ this ->machineState ;
190+
233191 $ this ->machineState = $ newState ;
234192
193+ $ this ->onTransition ($ this ->getTransition ($ oldState , $ newState ));
194+
235195 return $ this ;
236196 }
237197}
0 commit comments