Skip to content

Commit

Permalink
Allow usage of message handler classes as message handler names
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger committed Sep 20, 2024
1 parent 9fc8dee commit c86ce07
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Message/MessageHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Message;

interface MessageHandlerInterface
{
public function handle(MessageInterface $message): void;
}
13 changes: 13 additions & 0 deletions src/Worker/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Throwable;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Exception\JobFailureException;
use Yiisoft\Queue\Message\MessageHandlerInterface;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\Consume\ConsumeFinalHandler;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
Expand Down Expand Up @@ -77,6 +78,18 @@ public function process(MessageInterface $message, QueueInterface $queue): Messa
private function getHandler(string $name): ?callable
{
if (!array_key_exists($name, $this->handlersCached)) {
$definition = $this->handlers[$name] ?? null;
if ($definition === null && $this->container->has($name)) {
$handler = $this->container->get($name);
if ($handler instanceof MessageHandlerInterface) {
$this->handlersCached[$name] = [$handler, 'handle'];

return $this->handlersCached[$name];
}

return null;
}

$this->handlersCached[$name] = $this->prepare($this->handlers[$name] ?? null);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/MessageConsumingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Queue\Middleware\Consume\MiddlewareFactoryConsumeInterface;
use Yiisoft\Queue\Middleware\FailureHandling\FailureMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\FailureHandling\MiddlewareFactoryFailureInterface;
use Yiisoft\Queue\Tests\Integration\Support\TestHandler;
use Yiisoft\Queue\Tests\TestCase;
use Yiisoft\Queue\Worker\Worker;

Expand Down Expand Up @@ -41,4 +42,27 @@ public function testMessagesConsumed(): void

$this->assertEquals($messages, $this->messagesProcessed);
}

public function testMessagesConsumedByHandlerClass(): void
{
$handler = new TestHandler();
$container = $this->createMock(ContainerInterface::class);
$container->method('get')->with(TestHandler::class)->willReturn($handler);
$container->method('has')->with(TestHandler::class)->willReturn(true);
$worker = new Worker(
[],
new NullLogger(),
new Injector($container),
$container,
new ConsumeMiddlewareDispatcher($this->createMock(MiddlewareFactoryConsumeInterface::class)),
new FailureMiddlewareDispatcher($this->createMock(MiddlewareFactoryFailureInterface::class), [])
);

$messages = [1, 'foo', 'bar-baz'];
foreach ($messages as $message) {
$worker->process(new Message(TestHandler::class, $message), $this->getQueue());
}

$this->assertEquals($messages, $handler->messagesProcessed);
}
}
20 changes: 20 additions & 0 deletions tests/Integration/Support/TestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Integration\Support;

use Yiisoft\Queue\Message\MessageHandlerInterface;
use Yiisoft\Queue\Message\MessageInterface;

final class TestHandler implements MessageHandlerInterface
{
public function __construct(public array $messagesProcessed = [])
{
}

public function handle(MessageInterface $message): void
{
$this->messagesProcessed[] = $message->getData();
}
}

0 comments on commit c86ce07

Please sign in to comment.