[Workflow] Made code simpler
This commit is contained in:
parent
db69ccc185
commit
732f343572
@ -87,7 +87,8 @@ class StateMachineTest extends TestCase
|
|||||||
// that are not enabled by the marking are evaluated.
|
// that are not enabled by the marking are evaluated.
|
||||||
// see https://github.com/symfony/symfony/issues/28432
|
// see https://github.com/symfony/symfony/issues/28432
|
||||||
|
|
||||||
// Test if when you are in place "a" applying transition "t1" then returned blocker list contains guard blocker instead blockedByMarking
|
// Test if when you are in place "a"trying transition "t1" then returned
|
||||||
|
// blocker list contains guard blocker instead blockedByMarking
|
||||||
$subject->marking = 'a';
|
$subject->marking = 'a';
|
||||||
$transitionBlockerList = $net->buildTransitionBlockerList($subject, 't1');
|
$transitionBlockerList = $net->buildTransitionBlockerList($subject, 't1');
|
||||||
$this->assertCount(1, $transitionBlockerList);
|
$this->assertCount(1, $transitionBlockerList);
|
||||||
@ -96,7 +97,8 @@ class StateMachineTest extends TestCase
|
|||||||
$this->assertSame('Transition blocker of place a', $blockers[0]->getMessage());
|
$this->assertSame('Transition blocker of place a', $blockers[0]->getMessage());
|
||||||
$this->assertSame('blocker', $blockers[0]->getCode());
|
$this->assertSame('blocker', $blockers[0]->getCode());
|
||||||
|
|
||||||
// Test if when you are in place "d" applying transition "t1" then returned blocker list contains guard blocker instead blockedByMarking
|
// Test if when you are in place "d" trying transition "t1" then
|
||||||
|
// returned blocker list contains guard blocker instead blockedByMarking
|
||||||
$subject->marking = 'd';
|
$subject->marking = 'd';
|
||||||
$transitionBlockerList = $net->buildTransitionBlockerList($subject, 't1');
|
$transitionBlockerList = $net->buildTransitionBlockerList($subject, 't1');
|
||||||
$this->assertCount(1, $transitionBlockerList);
|
$this->assertCount(1, $transitionBlockerList);
|
||||||
|
@ -37,6 +37,17 @@ final class TransitionBlockerList implements \IteratorAggregate, \Countable
|
|||||||
$this->blockers[] = $blocker;
|
$this->blockers[] = $blocker;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function has(string $code): bool
|
||||||
|
{
|
||||||
|
foreach ($this->blockers as $blocker) {
|
||||||
|
if ($code === $blocker->getCode()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function clear(): void
|
public function clear(): void
|
||||||
{
|
{
|
||||||
$this->blockers = array();
|
$this->blockers = array();
|
||||||
|
@ -92,11 +92,7 @@ class Workflow implements WorkflowInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
||||||
} catch (NotEnabledTransitionException $e) {
|
|
||||||
$transitionBlockerList = $e->getTransitionBlockerList();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($transitionBlockerList->isEmpty()) {
|
if ($transitionBlockerList->isEmpty()) {
|
||||||
return true;
|
return true;
|
||||||
@ -120,13 +116,18 @@ class Workflow implements WorkflowInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
||||||
|
|
||||||
|
if ($transitionBlockerList->isEmpty()) {
|
||||||
|
return $transitionBlockerList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We prefer to return transitions blocker by something else than
|
||||||
|
// marking. Because it means the marking was OK. Transitions are
|
||||||
|
// deterministic: it's not possible to have many transitions enabled
|
||||||
|
// at the same time that match the same marking with the same name
|
||||||
|
if (!$transitionBlockerList->has(TransitionBlocker::BLOCKED_BY_MARKING)) {
|
||||||
return $transitionBlockerList;
|
return $transitionBlockerList;
|
||||||
} catch (NotEnabledTransitionException $e) {
|
|
||||||
// a transition with the same name is defined for other places too
|
|
||||||
$transitionBlockerList = $e->getTransitionBlockerList();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,16 +154,12 @@ class Workflow implements WorkflowInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
||||||
} catch (NotEnabledTransitionException $e) {
|
if (!$transitionBlockerList->isEmpty()) {
|
||||||
$transitionBlockerList = $e->getTransitionBlockerList();
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($transitionBlockerList->isEmpty()) {
|
|
||||||
$approvedTransitionQueue[] = $transition;
|
$approvedTransitionQueue[] = $transition;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($approvedTransitionQueue as $transition) {
|
foreach ($approvedTransitionQueue as $transition) {
|
||||||
$applied = true;
|
$applied = true;
|
||||||
@ -202,12 +199,7 @@ class Workflow implements WorkflowInterface
|
|||||||
$marking = $this->getMarking($subject);
|
$marking = $this->getMarking($subject);
|
||||||
|
|
||||||
foreach ($this->definition->getTransitions() as $transition) {
|
foreach ($this->definition->getTransitions() as $transition) {
|
||||||
try {
|
|
||||||
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
|
||||||
} catch (NotEnabledTransitionException $e) {
|
|
||||||
$transitionBlockerList = $e->getTransitionBlockerList();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($transitionBlockerList->isEmpty()) {
|
if ($transitionBlockerList->isEmpty()) {
|
||||||
$enabledTransitions[] = $transition;
|
$enabledTransitions[] = $transition;
|
||||||
}
|
}
|
||||||
@ -252,7 +244,9 @@ class Workflow implements WorkflowInterface
|
|||||||
{
|
{
|
||||||
foreach ($transition->getFroms() as $place) {
|
foreach ($transition->getFroms() as $place) {
|
||||||
if (!$marking->has($place)) {
|
if (!$marking->has($place)) {
|
||||||
throw new NotEnabledTransitionException($subject, $transition->getName(), $this, new TransitionBlockerList(array(TransitionBlocker::createBlockedByMarking($marking))));
|
return new TransitionBlockerList(array(
|
||||||
|
TransitionBlocker::createBlockedByMarking($marking),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user