@@ -159,25 +159,47 @@ public function apply($subject, $transitionName/*, array $context = []*/)
159
159
160
160
$ marking = $ this ->getMarking ($ subject );
161
161
162
- $ transitionBlockerList = null ;
163
- $ applied = false ;
164
- $ approvedTransitionQueue = [] ;
162
+ $ transitionExist = false ;
163
+ $ approvedTransitions = [] ;
164
+ $ bestTransitionBlockerList = null ;
165
165
166
166
foreach ($ this ->definition ->getTransitions () as $ transition ) {
167
167
if ($ transition ->getName () !== $ transitionName ) {
168
168
continue ;
169
169
}
170
170
171
- $ transitionBlockerList = $ this ->buildTransitionBlockerListForTransition ($ subject , $ marking , $ transition );
172
- if (!$ transitionBlockerList ->isEmpty ()) {
171
+ $ transitionExist = true ;
172
+
173
+ $ tmpTransitionBlockerList = $ this ->buildTransitionBlockerListForTransition ($ subject , $ marking , $ transition );
174
+
175
+ if ($ tmpTransitionBlockerList ->isEmpty ()) {
176
+ $ approvedTransitions [] = $ transition ;
177
+ continue ;
178
+ }
179
+
180
+ if (!$ bestTransitionBlockerList ) {
181
+ $ bestTransitionBlockerList = $ tmpTransitionBlockerList ;
173
182
continue ;
174
183
}
175
- $ approvedTransitionQueue [] = $ transition ;
184
+
185
+ // We prefer to return transitions blocker by something else than
186
+ // marking. Because it means the marking was OK. Transitions are
187
+ // deterministic: it's not possible to have many transitions enabled
188
+ // at the same time that match the same marking with the same name
189
+ if (!$ tmpTransitionBlockerList ->has (TransitionBlocker::BLOCKED_BY_MARKING )) {
190
+ $ bestTransitionBlockerList = $ tmpTransitionBlockerList ;
191
+ }
192
+ }
193
+
194
+ if (!$ transitionExist ) {
195
+ throw new UndefinedTransitionException ($ subject , $ transitionName , $ this );
176
196
}
177
197
178
- foreach ($ approvedTransitionQueue as $ transition ) {
179
- $ applied = true ;
198
+ if (!$ approvedTransitions ) {
199
+ throw new NotEnabledTransitionException ($ subject , $ transitionName , $ this , $ bestTransitionBlockerList );
200
+ }
180
201
202
+ foreach ($ approvedTransitions as $ transition ) {
181
203
$ this ->leave ($ subject , $ transition , $ marking );
182
204
183
205
$ context = $ this ->transition ($ subject , $ transition , $ marking , $ context );
@@ -193,14 +215,6 @@ public function apply($subject, $transitionName/*, array $context = []*/)
193
215
$ this ->announce ($ subject , $ transition , $ marking );
194
216
}
195
217
196
- if (!$ transitionBlockerList ) {
197
- throw new UndefinedTransitionException ($ subject , $ transitionName , $ this );
198
- }
199
-
200
- if (!$ applied ) {
201
- throw new NotEnabledTransitionException ($ subject , $ transitionName , $ this , $ transitionBlockerList );
202
- }
203
-
204
218
return $ marking ;
205
219
}
206
220
0 commit comments