Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5a55a5

Browse files
committed
feature #26651 [Workflow] Added a TransitionException (andrewtch, lyrixx)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Workflow] Added a TransitionException | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26587 #26581 | License | MIT | Doc PR | --- Note for reviewer: `NotEnabledTransitionException` and `UndefinedTransitionException` were not released. So this is not a BC Break. Commits ------- 838548c Finished implementation of TransitionException 9fa4f79 implemented TransitionException to be thrown instead of Logic exception
2 parents 4bbdf06 + 838548c commit d5a55a5

5 files changed

Lines changed: 64 additions & 9 deletions

File tree

src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
namespace Symfony\Component\Workflow\Exception;
1313

1414
use Symfony\Component\Workflow\TransitionBlockerList;
15+
use Symfony\Component\Workflow\WorkflowInterface;
1516

1617
/**
1718
* Thrown by Workflow when a not enabled transition is applied on a subject.
1819
*
1920
* @author Grégoire Pineau <[email protected]>
2021
*/
21-
class NotEnabledTransitionException extends LogicException
22+
class NotEnabledTransitionException extends TransitionException
2223
{
2324
private $transitionBlockerList;
2425

25-
public function __construct(string $transitionName, string $workflowName, TransitionBlockerList $transitionBlockerList)
26+
public function __construct($subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList)
2627
{
27-
parent::__construct(sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflowName));
28+
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName()));
2829

2930
$this->transitionBlockerList = $transitionBlockerList;
3031
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\Exception;
13+
14+
use Symfony\Component\Workflow\WorkflowInterface;
15+
16+
/**
17+
* @author Andrew Tch <[email protected]>
18+
* @author Grégoire Pineau <[email protected]>
19+
*/
20+
class TransitionException extends LogicException
21+
{
22+
private $subject;
23+
private $transitionName;
24+
private $workflow;
25+
26+
public function __construct($subject, string $transitionName, WorkflowInterface $workflow, string $message)
27+
{
28+
parent::__construct($message);
29+
30+
$this->subject = $subject;
31+
$this->transitionName = $transitionName;
32+
$this->workflow = $workflow;
33+
}
34+
35+
public function getSubject()
36+
{
37+
return $this->subject;
38+
}
39+
40+
public function getTransitionName(): string
41+
{
42+
return $this->transitionName;
43+
}
44+
45+
public function getWorkflow(): WorkflowInterface
46+
{
47+
return $this->workflow;
48+
}
49+
}

src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111

1212
namespace Symfony\Component\Workflow\Exception;
1313

14+
use Symfony\Component\Workflow\WorkflowInterface;
15+
1416
/**
1517
* Thrown by Workflow when an undefined transition is applied on a subject.
1618
*
1719
* @author Grégoire Pineau <[email protected]>
1820
*/
19-
class UndefinedTransitionException extends LogicException
21+
class UndefinedTransitionException extends TransitionException
2022
{
21-
public function __construct(string $transitionName, string $workflowName)
23+
public function __construct($subject, string $transitionName, WorkflowInterface $workflow)
2224
{
23-
parent::__construct(sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflowName));
25+
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName()));
2426
}
2527
}

src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ public function testApplyWithNotEnabledTransition()
271271
$this->assertCount(1, $e->getTransitionBlockerList());
272272
$list = iterator_to_array($e->getTransitionBlockerList());
273273
$this->assertSame('The marking does not enable the transition.', $list[0]->getMessage());
274+
$this->assertSame($e->getWorkflow(), $workflow);
275+
$this->assertSame($e->getSubject(), $subject);
276+
$this->assertSame($e->getTransitionName(), 't2');
274277
}
275278
}
276279

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function buildTransitionBlockerList($subject, string $transitionName): Tr
124124
}
125125

126126
if (!$transitionBlockerList) {
127-
throw new UndefinedTransitionException($transitionName, $this->name);
127+
throw new UndefinedTransitionException($subject, $transitionName, $this);
128128
}
129129

130130
return $transitionBlockerList;
@@ -168,11 +168,11 @@ public function apply($subject, $transitionName)
168168
}
169169

170170
if (!$transitionBlockerList) {
171-
throw new UndefinedTransitionException($transitionName, $this->name);
171+
throw new UndefinedTransitionException($subject, $transitionName, $this);
172172
}
173173

174174
if (!$applied) {
175-
throw new NotEnabledTransitionException($transitionName, $this->name, $transitionBlockerList);
175+
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList);
176176
}
177177

178178
return $marking;

0 commit comments

Comments
 (0)