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

Skip to content

Commit 62740c8

Browse files
committed
[Workflow] Add EventNameTrait to compute event name strings
1 parent e49f84b commit 62740c8

File tree

9 files changed

+139
-0
lines changed

9 files changed

+139
-0
lines changed

src/Symfony/Component/Workflow/Event/AnnounceEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
final class AnnounceEvent extends Event
1919
{
2020
use HasContextTrait;
21+
use EventNameTrait {
22+
getNameForTransition as public get;
23+
}
2124

2225
public function __construct(object $subject, Marking $marking, ?Transition $transition = null, ?WorkflowInterface $workflow = null, array $context = [])
2326
{

src/Symfony/Component/Workflow/Event/CompletedEvent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
final class CompletedEvent extends Event
1919
{
2020
use HasContextTrait;
21+
use EventNameTrait {
22+
getNameForTransition as public get;
23+
}
24+
2125

2226
public function __construct(object $subject, Marking $marking, ?Transition $transition = null, ?WorkflowInterface $workflow = null, array $context = [])
2327
{

src/Symfony/Component/Workflow/Event/EnterEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
final class EnterEvent extends Event
1919
{
2020
use HasContextTrait;
21+
use EventNameTrait {
22+
getNameForPlace as public get;
23+
}
2124

2225
public function __construct(object $subject, Marking $marking, ?Transition $transition = null, ?WorkflowInterface $workflow = null, array $context = [])
2326
{

src/Symfony/Component/Workflow/Event/EnteredEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
final class EnteredEvent extends Event
1919
{
2020
use HasContextTrait;
21+
use EventNameTrait {
22+
getNameForPlace as public get;
23+
}
2124

2225
public function __construct(object $subject, Marking $marking, ?Transition $transition = null, ?WorkflowInterface $workflow = null, array $context = [])
2326
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Event;
4+
5+
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
6+
7+
/**
8+
* @author Nicolas Rigaud <[email protected]>
9+
*/
10+
trait EventNameTrait
11+
{
12+
/**
13+
* Get the event name as lowercase string.
14+
*
15+
* Example: EnterEvent => enter
16+
*
17+
* @return string
18+
*/
19+
public static function getName(): string
20+
{
21+
return \strtolower(\substr(\substr(static::class, \strrpos(static::class, '\\') + 1), 0, -5));
22+
}
23+
24+
/**
25+
* Get event name for workflow and transition.
26+
*
27+
* @throws InvalidArgumentException If $transitionName is provided without $workflowName
28+
*/
29+
private static function getNameForTransition(string|null $workflowName, string|null $transitionName): string
30+
{
31+
return self::computeName($workflowName, $transitionName);
32+
}
33+
34+
/**
35+
* Get event name for workflow and place.
36+
*
37+
* @throws InvalidArgumentException If $placeName is provided without $workflowName
38+
*/
39+
private static function getNameForPlace(string|null $workflowName, string|null $placeName): string
40+
{
41+
return self::computeName($workflowName, $placeName);
42+
}
43+
44+
private static function computeName(string|null $workflowName, string|null $transitionOrPlaceName): string
45+
{
46+
if (null !== $transitionOrPlaceName && null === $workflowName) {
47+
throw new \InvalidArgumentException('Missing workflow name.');
48+
}
49+
50+
return implode('.', \array_filter(['workflow', $workflowName, self::getName(), $transitionOrPlaceName], \is_string(...)));
51+
}
52+
}

src/Symfony/Component/Workflow/Event/GuardEvent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
*/
2424
final class GuardEvent extends Event
2525
{
26+
use EventNameTrait {
27+
getNameForTransition as public get;
28+
}
29+
2630
private TransitionBlockerList $transitionBlockerList;
2731

2832
public function __construct(object $subject, Marking $marking, Transition $transition, ?WorkflowInterface $workflow = null)

src/Symfony/Component/Workflow/Event/LeaveEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
final class LeaveEvent extends Event
1919
{
2020
use HasContextTrait;
21+
use EventNameTrait {
22+
getNameForPlace as public get;
23+
}
2124

2225
public function __construct(object $subject, Marking $marking, ?Transition $transition = null, ?WorkflowInterface $workflow = null, array $context = [])
2326
{

src/Symfony/Component/Workflow/Event/TransitionEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
final class TransitionEvent extends Event
1919
{
2020
use HasContextTrait;
21+
use EventNameTrait {
22+
getNameForTransition as public get;
23+
}
2124

2225
public function __construct(object $subject, Marking $marking, ?Transition $transition = null, ?WorkflowInterface $workflow = null, array $context = [])
2326
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests\Event;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Workflow\Event\AnnounceEvent;
7+
use Symfony\Component\Workflow\Event\CompletedEvent;
8+
use Symfony\Component\Workflow\Event\EnteredEvent;
9+
use Symfony\Component\Workflow\Event\EnterEvent;
10+
use Symfony\Component\Workflow\Event\GuardEvent;
11+
use Symfony\Component\Workflow\Event\LeaveEvent;
12+
use Symfony\Component\Workflow\Event\TransitionEvent;
13+
14+
class EventNameTraitTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider getEvents
18+
*
19+
* @param class-string $class
20+
*/
21+
public function testEventNames(string $class, string|null $workflowName, string|null $transitionOrPlaceName, string $expected)
22+
{
23+
$name = $class::get($workflowName, $transitionOrPlaceName);
24+
$this->assertEquals($expected, $name);
25+
}
26+
27+
public static function getEvents(): iterable
28+
{
29+
yield [AnnounceEvent::class, null, null, 'workflow.announce'];
30+
yield [AnnounceEvent::class, 'post', null, 'workflow.post.announce'];
31+
yield [AnnounceEvent::class, 'post', 'publish', 'workflow.post.announce.publish'];
32+
33+
yield [CompletedEvent::class, null, null, 'workflow.completed'];
34+
yield [CompletedEvent::class, 'post', null, 'workflow.post.completed'];
35+
yield [CompletedEvent::class, 'post', 'publish', 'workflow.post.completed.publish'];
36+
37+
yield [EnteredEvent::class, null, null, 'workflow.entered'];
38+
yield [EnteredEvent::class, 'post', null, 'workflow.post.entered'];
39+
yield [EnteredEvent::class, 'post', 'published', 'workflow.post.entered.published'];
40+
41+
yield [EnterEvent::class, null, null, 'workflow.enter'];
42+
yield [EnterEvent::class, 'post', null, 'workflow.post.enter'];
43+
yield [EnterEvent::class, 'post', 'published', 'workflow.post.enter.published'];
44+
45+
yield [GuardEvent::class, null, null, 'workflow.guard'];
46+
yield [GuardEvent::class, 'post', null, 'workflow.post.guard'];
47+
yield [GuardEvent::class, 'post', 'publish', 'workflow.post.guard.publish'];
48+
49+
yield [LeaveEvent::class, null, null, 'workflow.leave'];
50+
yield [LeaveEvent::class, 'post', null, 'workflow.post.leave'];
51+
yield [LeaveEvent::class, 'post', 'published', 'workflow.post.leave.published'];
52+
53+
yield [TransitionEvent::class, null, null, 'workflow.transition'];
54+
yield [TransitionEvent::class, 'post', null, 'workflow.post.transition'];
55+
yield [TransitionEvent::class, 'post', 'publish', 'workflow.post.transition.publish'];
56+
}
57+
58+
public function testInvalidArgumentExceptionIsThrownIfWorkflowNameIsMissing()
59+
{
60+
$this->expectException(\InvalidArgumentException::class);
61+
62+
EnterEvent::get(null, 'place');
63+
}
64+
}

0 commit comments

Comments
 (0)