diff --git a/src/Attribute/Event.php b/src/Attribute/Event.php index 307acbb..5db165a 100644 --- a/src/Attribute/Event.php +++ b/src/Attribute/Event.php @@ -23,6 +23,7 @@ public function __construct( public readonly ?string $queue = null, public readonly array $dispatch = [On::POST], public readonly ?string $messageClass = null, + public readonly ?int $delay = null, ) { parent::__construct(); Assert::allIsInstanceOf($dispatch, On::class); diff --git a/src/Handler/Contract/EventHandler.php b/src/Handler/Contract/EventHandler.php index 71f6403..d175cc2 100644 --- a/src/Handler/Contract/EventHandler.php +++ b/src/Handler/Contract/EventHandler.php @@ -9,7 +9,7 @@ interface EventHandler extends AnnotationHandler { - public function dispatch(object $event, ?string $queue = null): void; + public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void; public function listen(Instance $instance, string $name, Transport $transport = null, int $priority = 0): void; } diff --git a/src/Handler/Impl/Event/HttpEventHandler.php b/src/Handler/Impl/Event/HttpEventHandler.php index 69f4a05..b518fab 100644 --- a/src/Handler/Impl/Event/HttpEventHandler.php +++ b/src/Handler/Impl/Event/HttpEventHandler.php @@ -39,7 +39,7 @@ public function __construct( * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface */ - public function dispatch(object $event, ?string $queue = null): void + public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void { if (!$event instanceof Event) { throw new \InvalidArgumentException('Event must be an instance of ' . Event::class); diff --git a/src/Handler/Impl/Event/SymfonyEventDispatcherEventHandler.php b/src/Handler/Impl/Event/SymfonyEventDispatcherEventHandler.php index 85f7c5d..a92367c 100644 --- a/src/Handler/Impl/Event/SymfonyEventDispatcherEventHandler.php +++ b/src/Handler/Impl/Event/SymfonyEventDispatcherEventHandler.php @@ -22,7 +22,7 @@ public function __construct( ) { } - public function dispatch(object $event, ?string $queue = null): void + public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void { if (!$event instanceof Event) { throw new \InvalidArgumentException('Event must be an instance of ' . Event::class); diff --git a/src/Handler/Impl/Event/SymfonyMessengerEventHandler.php b/src/Handler/Impl/Event/SymfonyMessengerEventHandler.php index 6db01fd..a6554a4 100644 --- a/src/Handler/Impl/Event/SymfonyMessengerEventHandler.php +++ b/src/Handler/Impl/Event/SymfonyMessengerEventHandler.php @@ -13,6 +13,7 @@ use Psr\Log\NullLogger; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Messenger\MessageBusInterface; +use Symfony\Component\Messenger\Stamp\DelayStamp; final class SymfonyMessengerEventHandler implements EventHandler { @@ -29,7 +30,7 @@ public function __construct( $this->logger = $logger ?? new NullLogger(); } - public function dispatch(object $event, ?string $queue = null): void + public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void { if ($event instanceof Event) { $message = $this->createMessage($event, $queue); @@ -38,7 +39,11 @@ public function dispatch(object $event, ?string $queue = null): void } try { - $this->bus->dispatch($message); + if ($delay !== null) { + $this->bus->dispatch($message, [new DelayStamp($delay)]); + } else { + $this->bus->dispatch($message); + } } catch (\Throwable $exception) { $this->logger->error($exception->getMessage(), compact('message', 'exception')); } diff --git a/src/Interceptor/Impl/EventInterceptor.php b/src/Interceptor/Impl/EventInterceptor.php index 5dcc248..ec77686 100644 --- a/src/Interceptor/Impl/EventInterceptor.php +++ b/src/Interceptor/Impl/EventInterceptor.php @@ -56,7 +56,7 @@ public function prefix(Instance $instance): Response ); foreach ($handlers as $handler) { if ($attribute->isPre()) { - $handler->dispatch($event, $attribute->queue); + $handler->dispatch($event, $attribute->queue, $attribute->delay); } } } @@ -87,7 +87,7 @@ public function suffix(Instance $instance): Response $this->config->eventInstanceClassName, ); } - $handler->dispatch($event, $attribute->queue); + $handler->dispatch($event, $attribute->queue, $attribute->delay); } if ($attribute->isOnException() && $instance->getMethod()->threwException()) { @@ -97,7 +97,7 @@ public function suffix(Instance $instance): Response $attribute->name, $this->config->eventInstanceClassName, ); - $handler->dispatch($event, $attribute->queue); + $handler->dispatch($event, $attribute->queue, $attribute->delay); } } } diff --git a/tests/Double/Mock/Event/EventHandlerMock.php b/tests/Double/Mock/Event/EventHandlerMock.php index 99795f8..8d94239 100644 --- a/tests/Double/Mock/Event/EventHandlerMock.php +++ b/tests/Double/Mock/Event/EventHandlerMock.php @@ -25,7 +25,7 @@ public function getName(): string return 'array'; } - public function dispatch(object $event, ?string $queue = null): void + public function dispatch(object $event, ?string $queue = null, ?int $delay = null): void { $this->events[] = $event; }