Skip to content

Commit 6ab893c

Browse files
authored
Merge pull request #65 from OpenClassrooms/add-event-delay
feat(event): add delay attr
2 parents e9b94cd + f7c85b4 commit 6ab893c

7 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎src/Attribute/Event.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
public readonly ?string $queue = null,
2424
public readonly array $dispatch = [On::POST],
2525
public readonly ?string $messageClass = null,
26+
public readonly ?int $delay = null,
2627
) {
2728
parent::__construct();
2829
Assert::allIsInstanceOf($dispatch, On::class);

‎src/Handler/Contract/EventHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
interface EventHandler extends AnnotationHandler
1111
{
12-
public function dispatch(object $event, ?string $queue = null): void;
12+
public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void;
1313

1414
public function listen(Instance $instance, string $name, Transport $transport = null, int $priority = 0): void;
1515
}

‎src/Handler/Impl/Event/HttpEventHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
4040
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
4141
*/
42-
public function dispatch(object $event, ?string $queue = null): void
42+
public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void
4343
{
4444
if (!$event instanceof Event) {
4545
throw new \InvalidArgumentException('Event must be an instance of ' . Event::class);

‎src/Handler/Impl/Event/SymfonyEventDispatcherEventHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
) {
2323
}
2424

25-
public function dispatch(object $event, ?string $queue = null): void
25+
public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void
2626
{
2727
if (!$event instanceof Event) {
2828
throw new \InvalidArgumentException('Event must be an instance of ' . Event::class);

‎src/Handler/Impl/Event/SymfonyMessengerEventHandler.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psr\Log\NullLogger;
1414
use Symfony\Component\HttpFoundation\RequestStack;
1515
use Symfony\Component\Messenger\MessageBusInterface;
16+
use Symfony\Component\Messenger\Stamp\DelayStamp;
1617

1718
final class SymfonyMessengerEventHandler implements EventHandler
1819
{
@@ -29,7 +30,7 @@ public function __construct(
2930
$this->logger = $logger ?? new NullLogger();
3031
}
3132

32-
public function dispatch(object $event, ?string $queue = null): void
33+
public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void
3334
{
3435
if ($event instanceof Event) {
3536
$message = $this->createMessage($event, $queue);
@@ -38,7 +39,11 @@ public function dispatch(object $event, ?string $queue = null): void
3839
}
3940

4041
try {
41-
$this->bus->dispatch($message);
42+
if ($delay !== null) {
43+
$this->bus->dispatch($message, [new DelayStamp($delay)]);
44+
} else {
45+
$this->bus->dispatch($message);
46+
}
4247
} catch (\Throwable $exception) {
4348
$this->logger->error($exception->getMessage(), compact('message', 'exception'));
4449
}

‎src/Interceptor/Impl/EventInterceptor.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function prefix(Instance $instance): Response
5656
);
5757
foreach ($handlers as $handler) {
5858
if ($attribute->isPre()) {
59-
$handler->dispatch($event, $attribute->queue);
59+
$handler->dispatch($event, $attribute->queue, $attribute->delay);
6060
}
6161
}
6262
}
@@ -87,7 +87,7 @@ public function suffix(Instance $instance): Response
8787
$this->config->eventInstanceClassName,
8888
);
8989
}
90-
$handler->dispatch($event, $attribute->queue);
90+
$handler->dispatch($event, $attribute->queue, $attribute->delay);
9191
}
9292

9393
if ($attribute->isOnException() && $instance->getMethod()->threwException()) {
@@ -97,7 +97,7 @@ public function suffix(Instance $instance): Response
9797
$attribute->name,
9898
$this->config->eventInstanceClassName,
9999
);
100-
$handler->dispatch($event, $attribute->queue);
100+
$handler->dispatch($event, $attribute->queue, $attribute->delay);
101101
}
102102
}
103103
}

‎tests/Double/Mock/Event/EventHandlerMock.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getName(): string
2525
return 'array';
2626
}
2727

28-
public function dispatch(object $event, ?string $queue = null): void
28+
public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void
2929
{
3030
$this->events[] = $event;
3131
}

0 commit comments

Comments
 (0)