Skip to content

Commit f92a103

Browse files
committed
[Workflow] Made the code more robbust and ease on-boarding
1 parent e93497b commit f92a103

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

Tests/WorkflowTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public function testCanWithGuard()
113113
$subject = new \stdClass();
114114
$subject->marking = null;
115115
$eventDispatcher = new EventDispatcher();
116-
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) { $event->setBlocked(true); });
116+
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
117+
$event->setBlocked(true);
118+
});
117119
$workflow = new Workflow($definition, new PropertyAccessorMarkingStore(), $eventDispatcher, 'workflow_name');
118120

119121
$this->assertFalse($workflow->can($subject, 't1'));
@@ -188,7 +190,9 @@ public function testGetEnabledTransitions()
188190
$subject = new \stdClass();
189191
$subject->marking = null;
190192
$eventDispatcher = new EventDispatcher();
191-
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) { $event->setBlocked(true); });
193+
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
194+
$event->setBlocked(true);
195+
});
192196
$workflow = new Workflow($definition, new PropertyAccessorMarkingStore(), $eventDispatcher, 'workflow_name');
193197

194198
$this->assertEmpty($workflow->getEnabledTransitions($subject));

Validator/StateMachineValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function validate(Definition $definition, $name)
5353
if (isset($transitionFromNames[$from][$transition->getName()])) {
5454
throw new InvalidDefinitionException(
5555
sprintf(
56-
'A transition from a place/state must have an unique name. Multiple transition named "%s" from place/state "%s" where found on StateMachine "%s". ',
56+
'A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ',
5757
$transition->getName(),
5858
$from,
5959
$name

Workflow.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Workflow\Event\GuardEvent;
1717
use Symfony\Component\Workflow\Exception\LogicException;
1818
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
19+
use Symfony\Component\Workflow\MarkingStore\PropertyAccessorMarkingStore;
1920

2021
/**
2122
* @author Fabien Potencier <fabien@symfony.com>
@@ -29,10 +30,10 @@ class Workflow
2930
private $dispatcher;
3031
private $name;
3132

32-
public function __construct(Definition $definition, MarkingStoreInterface $markingStore, EventDispatcherInterface $dispatcher = null, $name = 'unnamed')
33+
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, $name = 'unnamed')
3334
{
3435
$this->definition = $definition;
35-
$this->markingStore = $markingStore;
36+
$this->markingStore = $markingStore ?: new PropertyAccessorMarkingStore();
3637
$this->dispatcher = $dispatcher;
3738
$this->name = $name;
3839
}
@@ -163,7 +164,7 @@ public function getName()
163164
* @param Marking $marking
164165
* @param Transition $transition
165166
*
166-
* @return bool|void boolean true if this transition is guarded, ie you cannot use it.
167+
* @return bool|void boolean true if this transition is guarded, ie you cannot use it
167168
*/
168169
private function guardTransition($subject, Marking $marking, Transition $transition)
169170
{
@@ -253,25 +254,21 @@ private function getTransitions($transitionName)
253254
{
254255
$transitions = $this->definition->getTransitions();
255256

256-
$namedTransitions = array_filter(
257-
$transitions,
258-
function (Transition $transition) use ($transitionName) {
259-
return $transitionName === $transition->getName();
260-
}
261-
);
257+
$transitions = array_filter($transitions, function (Transition $transition) use ($transitionName) {
258+
return $transitionName === $transition->getName();
259+
});
262260

263-
if (empty($namedTransitions)) {
264-
throw new LogicException(
265-
sprintf('Transition "%s" does not exist for workflow "%s".', $transitionName, $this->name)
266-
);
261+
if (!$transitions) {
262+
throw new LogicException(sprintf('Transition "%s" does not exist for workflow "%s".', $transitionName, $this->name));
267263
}
268264

269-
return $namedTransitions;
265+
return $transitions;
270266
}
271267

272268
/**
273-
* Return the first Transition in $transitions that is valid for the $subject and $marking. null is returned when
274-
* you cannot do any Transition in $transitions on the $subject.
269+
* Return the first Transition in $transitions that is valid for the
270+
* $subject and $marking. null is returned when you cannot do any Transition
271+
* in $transitions on the $subject.
275272
*
276273
* @param object $subject
277274
* @param Marking $marking
@@ -292,7 +289,5 @@ private function getTransitionForSubject($subject, Marking $marking, array $tran
292289
return $transition;
293290
}
294291
}
295-
296-
return;
297292
}
298293
}

0 commit comments

Comments
 (0)