From 9fa4f794b0f7fc6777a8ed87c3f764e61763de71 Mon Sep 17 00:00:00 2001 From: Andrew Tch Date: Sun, 18 Mar 2018 12:22:42 +0200 Subject: [PATCH 1/2] implemented TransitionException to be thrown instead of Logic exception --- .../Exception/TransitionException.php | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Symfony/Component/Workflow/Exception/TransitionException.php diff --git a/src/Symfony/Component/Workflow/Exception/TransitionException.php b/src/Symfony/Component/Workflow/Exception/TransitionException.php new file mode 100644 index 0000000000000..2abc6cdc12cca --- /dev/null +++ b/src/Symfony/Component/Workflow/Exception/TransitionException.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Workflow\Exception; + +/** + * @author Andrew Tch + */ +class TransitionException extends LogicException +{ + /** + * @var mixed + */ + private $subject; + + /** + * @var string + */ + private $transitionName; + + /** + * @var string + */ + private $workflowName; + + public function __construct($subject, $transitionName, $workflowName) + { + $this->subject = $subject; + $this->transitionName = $transitionName; + $this->workflowName = $workflowName; + + parent::__construct(sprintf('Unable to apply transition "%s" for workflow "%s".', $this->transitionName, $this->workflowName)); + } + + /** + * @return mixed + */ + public function getSubject() + { + return $this->subject; + } + + /** + * @return string + */ + public function getTransitionName() + { + return $this->transitionName; + } + + /** + * @return string + */ + public function getWorkflowName() + { + return $this->workflowName; + } +} From 838548c54f9d237d52b81d2fe9a5df3a2ae174a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 23 Mar 2018 11:28:40 +0100 Subject: [PATCH 2/2] Finished implementation of TransitionException --- .../NotEnabledTransitionException.php | 7 ++-- .../Exception/TransitionException.php | 39 ++++++------------- .../UndefinedTransitionException.php | 8 ++-- .../Component/Workflow/Tests/WorkflowTest.php | 3 ++ src/Symfony/Component/Workflow/Workflow.php | 6 +-- 5 files changed, 26 insertions(+), 37 deletions(-) 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 index 2abc6cdc12cca..d09df40d44217 100644 --- a/src/Symfony/Component/Workflow/Exception/TransitionException.php +++ b/src/Symfony/Component/Workflow/Exception/TransitionException.php @@ -11,56 +11,39 @@ namespace Symfony\Component\Workflow\Exception; +use Symfony\Component\Workflow\WorkflowInterface; + /** * @author Andrew Tch + * @author Grégoire Pineau */ class TransitionException extends LogicException { - /** - * @var mixed - */ private $subject; - - /** - * @var string - */ private $transitionName; + private $workflow; - /** - * @var string - */ - private $workflowName; - - public function __construct($subject, $transitionName, $workflowName) + public function __construct($subject, string $transitionName, WorkflowInterface $workflow, string $message) { + parent::__construct($message); + $this->subject = $subject; $this->transitionName = $transitionName; - $this->workflowName = $workflowName; - - parent::__construct(sprintf('Unable to apply transition "%s" for workflow "%s".', $this->transitionName, $this->workflowName)); + $this->workflow = $workflow; } - /** - * @return mixed - */ public function getSubject() { return $this->subject; } - /** - * @return string - */ - public function getTransitionName() + public function getTransitionName(): string { return $this->transitionName; } - /** - * @return string - */ - public function getWorkflowName() + public function getWorkflow(): WorkflowInterface { - return $this->workflowName; + 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;