diff --git a/src/Debug/QueueCollector.php b/src/Debug/QueueCollector.php index 06e9feb4..055cbde0 100644 --- a/src/Debug/QueueCollector.php +++ b/src/Debug/QueueCollector.php @@ -73,7 +73,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac if (!$this->isActive()) { return; } - $this->processingMessages[$queue->getChannelName()][] = $message; + $this->processingMessages[$queue->getChannelName() ?? 'null'][] = $message; } private function reset(): void diff --git a/src/Debug/QueueDecorator.php b/src/Debug/QueueDecorator.php index 3e6f61d9..e6b83f8e 100644 --- a/src/Debug/QueueDecorator.php +++ b/src/Debug/QueueDecorator.php @@ -21,7 +21,7 @@ public function __construct( public function status(string|int $id): JobStatus { $result = $this->queue->status($id); - $this->collector->collectStatus($id, $result); + $this->collector->collectStatus((string) $id, $result); return $result; } diff --git a/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php b/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php index af11386e..bbc4e38b 100644 --- a/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php +++ b/src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php @@ -37,6 +37,7 @@ public function dispatch( FailureHandlingRequest $request, MessageFailureHandlerInterface $finishHandler ): FailureHandlingRequest { + /** @var string $channelName It is always string in this context */ $channelName = $request->getQueue()->getChannelName(); if (!isset($this->middlewareDefinitions[$channelName]) || $this->middlewareDefinitions[$channelName] === []) { $channelName = self::DEFAULT_PIPELINE; diff --git a/src/Provider/AdapterFactoryQueueProvider.php b/src/Provider/AdapterFactoryQueueProvider.php index ffe777f5..7f9cca30 100644 --- a/src/Provider/AdapterFactoryQueueProvider.php +++ b/src/Provider/AdapterFactoryQueueProvider.php @@ -85,7 +85,7 @@ private function getOrTryToCreate(string $channel): QueueInterface|null ), ); } - $this->queues[$channel] = $this->baseQueue->withAdapter($adapter)->withChannelName($channel); + $this->queues[$channel] = $this->baseQueue->withAdapter($adapter->withChannel($channel)); } else { $this->queues[$channel] = null; } diff --git a/src/Provider/PrototypeQueueProvider.php b/src/Provider/PrototypeQueueProvider.php index 53b0d6e2..4b7a2d4b 100644 --- a/src/Provider/PrototypeQueueProvider.php +++ b/src/Provider/PrototypeQueueProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\QueueInterface; /** @@ -17,12 +18,13 @@ final class PrototypeQueueProvider implements QueueProviderInterface */ public function __construct( private readonly QueueInterface $baseQueue, + private readonly AdapterInterface $baseAdapter, ) { } public function get(string $channel): QueueInterface { - return $this->baseQueue->withChannelName($channel); + return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel)); } public function has(string $channel): bool diff --git a/stubs/StubAdapter.php b/stubs/StubAdapter.php index ea423db4..0a8e06b2 100644 --- a/stubs/StubAdapter.php +++ b/stubs/StubAdapter.php @@ -14,6 +14,10 @@ */ final class StubAdapter implements AdapterInterface { + public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME) + { + } + public function runExisting(callable $handlerCallback): void { } @@ -34,11 +38,14 @@ public function subscribe(callable $handlerCallback): void public function withChannel(string $channel): AdapterInterface { - return clone $this; + $new = clone $this; + $new->channelName = $channel; + + return $new; } public function getChannelName(): string { - return QueueInterface::DEFAULT_CHANNEL_NAME; + return $this->channelName; } } diff --git a/stubs/StubQueue.php b/stubs/StubQueue.php index 8436b46a..a275618b 100644 --- a/stubs/StubQueue.php +++ b/stubs/StubQueue.php @@ -15,10 +15,7 @@ */ final class StubQueue implements QueueInterface { - public function __construct( - private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME, - private ?AdapterInterface $adapter = null, - ) { + public function __construct(private ?AdapterInterface $adapter = null) { } public function push( @@ -51,21 +48,12 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface { $new = clone $this; $new->adapter = $adapter; - return $new; - } - public function getChannelName(): string - { - return $this->channelName; + return $new; } - public function withChannelName(string $channel): QueueInterface + public function getChannelName(): ?string { - $new = clone $this; - $new->channelName = $channel; - if ($new->adapter !== null) { - $new->adapter = $new->adapter->withChannel($channel); - } - return $new; + return $this->adapter?->getChannelName(); } } diff --git a/tests/Unit/Provider/CompositeQueueProviderTest.php b/tests/Unit/Provider/CompositeQueueProviderTest.php index 491017ee..7c33de2b 100644 --- a/tests/Unit/Provider/CompositeQueueProviderTest.php +++ b/tests/Unit/Provider/CompositeQueueProviderTest.php @@ -15,7 +15,7 @@ final class CompositeQueueProviderTest extends TestCase { public function testBase(): void { - $queue = new StubQueue('channel'); + $queue = new StubQueue(new StubAdapter()); $provider = new CompositeQueueProvider( new AdapterFactoryQueueProvider( $queue, @@ -38,7 +38,10 @@ public function testBase(): void public function testNotFound(): void { $provider = new CompositeQueueProvider( - new AdapterFactoryQueueProvider(new StubQueue('channel'), ['channel1' => new StubAdapter()]), + new AdapterFactoryQueueProvider( + new StubQueue(new StubAdapter()), + ['channel1' => new StubAdapter()] + ), ); $this->expectException(ChannelNotFoundException::class); diff --git a/tests/Unit/Provider/PrototypeQueueProviderTest.php b/tests/Unit/Provider/PrototypeQueueProviderTest.php index 601a0f24..f8fa4c42 100644 --- a/tests/Unit/Provider/PrototypeQueueProviderTest.php +++ b/tests/Unit/Provider/PrototypeQueueProviderTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Queue\Provider\PrototypeQueueProvider; +use Yiisoft\Queue\Stubs\StubAdapter; use Yiisoft\Queue\Stubs\StubQueue; final class PrototypeQueueProviderTest extends TestCase @@ -14,6 +15,7 @@ public function testBase(): void { $provider = new PrototypeQueueProvider( new StubQueue(), + new StubAdapter(), ); $queue = $provider->get('test-channel'); diff --git a/tests/Unit/Stubs/StubQueueTest.php b/tests/Unit/Stubs/StubQueueTest.php index 29efe050..5692ae8e 100644 --- a/tests/Unit/Stubs/StubQueueTest.php +++ b/tests/Unit/Stubs/StubQueueTest.php @@ -20,7 +20,7 @@ public function testBase(): void $this->assertSame($message, $queue->push($message)); $this->assertSame(0, $queue->run()); $this->assertTrue($queue->status('test')->isDone()); - $this->assertSame(QueueInterface::DEFAULT_CHANNEL_NAME, $queue->getChannelName()); + $this->assertNull($queue->getChannelName()); $this->assertNull($queue->getAdapter()); $queue->listen(); } @@ -34,15 +34,4 @@ public function testWithAdapter(): void $this->assertNotSame($queue, $sourceQueue); $this->assertInstanceOf(StubAdapter::class, $queue->getAdapter()); } - - public function testWithChannelName(): void - { - $sourceQueue = new StubQueue(); - - $queue = $sourceQueue->withChannelName('test'); - - $this->assertNotSame($queue, $sourceQueue); - $this->assertSame(QueueInterface::DEFAULT_CHANNEL_NAME, $sourceQueue->getChannelName()); - $this->assertSame('test', $queue->getChannelName()); - } }