Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Attribute/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Contract/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Handler/Impl/Event/HttpEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/Handler/Impl/Event/SymfonyMessengerEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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'));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Interceptor/Impl/EventInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Double/Mock/Event/EventHandlerMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down