diff --git a/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php b/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php index 48a8c260ff8f9..738aab8387c14 100644 --- a/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php +++ b/src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php @@ -12,19 +12,20 @@ namespace Symfony\Component\Workflow\Exception; use Symfony\Component\Workflow\TransitionBlockerList; +use Symfony\Component\Workflow\WorkflowInterface; /** * Thrown by Workflow when a not enabled transition is applied on a subject. * * @author Grégoire Pineau */ -class NotEnabledTransitionException extends LogicException +class NotEnabledTransitionException extends TransitionException { private $transitionBlockerList; - public function __construct(string $transitionName, string $workflowName, TransitionBlockerList $transitionBlockerList) + public function __construct($subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList) { - parent::__construct(sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflowName)); + parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName())); $this->transitionBlockerList = $transitionBlockerList; } diff --git a/src/Symfony/Component/Workflow/Exception/TransitionException.php b/src/Symfony/Component/Workflow/Exception/TransitionException.php new file mode 100644 index 0000000000000..d09df40d44217 --- /dev/null +++ b/src/Symfony/Component/Workflow/Exception/TransitionException.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Workflow\Exception; + +use Symfony\Component\Workflow\WorkflowInterface; + +/** + * @author Andrew Tch + * @author Grégoire Pineau + */ +class TransitionException extends LogicException +{ + private $subject; + private $transitionName; + private $workflow; + + public function __construct($subject, string $transitionName, WorkflowInterface $workflow, string $message) + { + parent::__construct($message); + + $this->subject = $subject; + $this->transitionName = $transitionName; + $this->workflow = $workflow; + } + + public function getSubject() + { + return $this->subject; + } + + public function getTransitionName(): string + { + return $this->transitionName; + } + + public function getWorkflow(): WorkflowInterface + { + return $this->workflow; + } +} diff --git a/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php b/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php index 04022e90ccbe7..6d13ccfa5e8df 100644 --- a/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php +++ b/src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php @@ -11,15 +11,17 @@ namespace Symfony\Component\Workflow\Exception; +use Symfony\Component\Workflow\WorkflowInterface; + /** * Thrown by Workflow when an undefined transition is applied on a subject. * * @author Grégoire Pineau */ -class UndefinedTransitionException extends LogicException +class UndefinedTransitionException extends TransitionException { - public function __construct(string $transitionName, string $workflowName) + public function __construct($subject, string $transitionName, WorkflowInterface $workflow) { - parent::__construct(sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflowName)); + parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName())); } } diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index 4451357d536cd..8ef966666eeb0 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -271,6 +271,9 @@ public function testApplyWithNotEnabledTransition() $this->assertCount(1, $e->getTransitionBlockerList()); $list = iterator_to_array($e->getTransitionBlockerList()); $this->assertSame('The marking does not enable the transition.', $list[0]->getMessage()); + $this->assertSame($e->getWorkflow(), $workflow); + $this->assertSame($e->getSubject(), $subject); + $this->assertSame($e->getTransitionName(), 't2'); } } diff --git a/src/Symfony/Component/Workflow/Workflow.php b/src/Symfony/Component/Workflow/Workflow.php index 37409c9cd7db1..963f1b8b27180 100644 --- a/src/Symfony/Component/Workflow/Workflow.php +++ b/src/Symfony/Component/Workflow/Workflow.php @@ -124,7 +124,7 @@ public function buildTransitionBlockerList($subject, string $transitionName): Tr } if (!$transitionBlockerList) { - throw new UndefinedTransitionException($transitionName, $this->name); + throw new UndefinedTransitionException($subject, $transitionName, $this); } return $transitionBlockerList; @@ -168,11 +168,11 @@ public function apply($subject, $transitionName) } if (!$transitionBlockerList) { - throw new UndefinedTransitionException($transitionName, $this->name); + throw new UndefinedTransitionException($subject, $transitionName, $this); } if (!$applied) { - throw new NotEnabledTransitionException($transitionName, $this->name, $transitionBlockerList); + throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList); } return $marking;