From 6effbf27e5b3d3c89ea5c91566960c0c43b9930d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 12 Dec 2024 22:37:57 +0300 Subject: [PATCH] providers --- composer.json | 2 +- src/Debug/QueueProviderInterfaceProxy.php | 5 ++-- src/Provider/AdapterFactoryQueueProvider.php | 10 +++++-- src/Provider/ChannelNotFoundException.php | 6 ++-- src/Provider/CompositeQueueProvider.php | 5 ++-- src/Provider/PrototypeQueueProvider.php | 5 ++-- src/Provider/QueueProviderInterface.php | 9 +++--- .../AdapterFactoryQueueProviderTest.php | 17 +++++++++++ .../Provider/ChannelNotFoundExceptionTest.php | 30 +++++++++++++++++++ 9 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 tests/Unit/Provider/ChannelNotFoundExceptionTest.php diff --git a/composer.json b/composer.json index a6fe193a..9812ea48 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "psr/container": "^1.0|^2.0", "psr/log": "^2.0|^3.0", "symfony/console": "^5.4|^6.0", - "yiisoft/definitions": "dev-fix-union as 3.0.1", + "yiisoft/definitions": "^1.0|^2.0|^3.0", "yiisoft/factory": "^1.3", "yiisoft/friendly-exception": "^1.0", "yiisoft/injector": "^1.0" diff --git a/src/Debug/QueueProviderInterfaceProxy.php b/src/Debug/QueueProviderInterfaceProxy.php index 5f00096e..22ce1307 100644 --- a/src/Debug/QueueProviderInterfaceProxy.php +++ b/src/Debug/QueueProviderInterfaceProxy.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Debug; +use BackedEnum; use Yiisoft\Queue\Provider\QueueProviderInterface; use Yiisoft\Queue\QueueInterface; @@ -15,13 +16,13 @@ public function __construct( ) { } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { $queue = $this->queueProvider->get($channel); return new QueueDecorator($queue, $this->collector); } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { return $this->queueProvider->has($channel); } diff --git a/src/Provider/AdapterFactoryQueueProvider.php b/src/Provider/AdapterFactoryQueueProvider.php index 7f9cca30..36c2a193 100644 --- a/src/Provider/AdapterFactoryQueueProvider.php +++ b/src/Provider/AdapterFactoryQueueProvider.php @@ -4,10 +4,12 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Psr\Container\ContainerInterface; use Yiisoft\Definitions\Exception\InvalidConfigException; use Yiisoft\Factory\StrictFactory; use Yiisoft\Queue\Adapter\AdapterInterface; +use Yiisoft\Queue\ChannelNormalizer; use Yiisoft\Queue\QueueInterface; use function array_key_exists; @@ -50,17 +52,21 @@ public function __construct( } } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { + $channel = ChannelNormalizer::normalize($channel); + $queue = $this->getOrTryToCreate($channel); if ($queue === null) { throw new ChannelNotFoundException($channel); } + return $queue; } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { + $channel = ChannelNormalizer::normalize($channel); return $this->factory->has($channel); } diff --git a/src/Provider/ChannelNotFoundException.php b/src/Provider/ChannelNotFoundException.php index 2538cda7..ef9b3ace 100644 --- a/src/Provider/ChannelNotFoundException.php +++ b/src/Provider/ChannelNotFoundException.php @@ -4,8 +4,10 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use LogicException; use Throwable; +use Yiisoft\Queue\ChannelNormalizer; use function sprintf; @@ -14,10 +16,10 @@ */ final class ChannelNotFoundException extends LogicException implements QueueProviderException { - public function __construct(string $channel, int $code = 0, ?Throwable $previous = null) + public function __construct(string|BackedEnum $channel, int $code = 0, ?Throwable $previous = null) { parent::__construct( - sprintf('Channel "%s" not found.', $channel), + sprintf('Channel "%s" not found.', ChannelNormalizer::normalize($channel)), $code, $previous, ); diff --git a/src/Provider/CompositeQueueProvider.php b/src/Provider/CompositeQueueProvider.php index cd7f36ca..f699d256 100644 --- a/src/Provider/CompositeQueueProvider.php +++ b/src/Provider/CompositeQueueProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Yiisoft\Queue\QueueInterface; /** @@ -25,7 +26,7 @@ public function __construct( $this->providers = $providers; } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { foreach ($this->providers as $provider) { if ($provider->has($channel)) { @@ -35,7 +36,7 @@ public function get(string $channel): QueueInterface throw new ChannelNotFoundException($channel); } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { foreach ($this->providers as $provider) { if ($provider->has($channel)) { diff --git a/src/Provider/PrototypeQueueProvider.php b/src/Provider/PrototypeQueueProvider.php index 4b7a2d4b..929d124a 100644 --- a/src/Provider/PrototypeQueueProvider.php +++ b/src/Provider/PrototypeQueueProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\QueueInterface; @@ -22,12 +23,12 @@ public function __construct( ) { } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel)); } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { return true; } diff --git a/src/Provider/QueueProviderInterface.php b/src/Provider/QueueProviderInterface.php index 7d3501b7..2e67a2f1 100644 --- a/src/Provider/QueueProviderInterface.php +++ b/src/Provider/QueueProviderInterface.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Yiisoft\Queue\QueueInterface; /** @@ -14,21 +15,21 @@ interface QueueProviderInterface /** * Find a queue by channel name and returns it. * - * @param string $channel Channel name. + * @param string|BackedEnum $channel Channel name. * * @throws InvalidQueueConfigException If the queue configuration is invalid. * @throws ChannelNotFoundException If the channel is not found. * @throws QueueProviderException If the queue provider fails to provide a queue. * @return QueueInterface Queue instance. */ - public function get(string $channel): QueueInterface; + public function get(string|BackedEnum $channel): QueueInterface; /** * Check if a queue with the specified channel name exists. * - * @param string $channel Channel name. + * @param string|BackedEnum $channel Channel name. * * @return bool Whether the queue exists. */ - public function has(string $channel): bool; + public function has(string|BackedEnum $channel): bool; } diff --git a/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php b/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php index 24ca9096..94900a66 100644 --- a/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php +++ b/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php @@ -12,6 +12,7 @@ use Yiisoft\Queue\Provider\InvalidQueueConfigException; use Yiisoft\Queue\Stubs\StubQueue; use Yiisoft\Queue\Stubs\StubAdapter; +use Yiisoft\Queue\Tests\Unit\Support\StringEnum; use function sprintf; @@ -100,4 +101,20 @@ public function testInvalidQueueConfigOnGet(): void ); $provider->get('channel1'); } + + public function testGetHasByStringEnum(): void + { + $provider = new AdapterFactoryQueueProvider( + new StubQueue(), + [ + 'red' => StubAdapter::class, + ], + ); + + $queue = $provider->get(StringEnum::RED); + + $this->assertSame('red', $queue->getChannelName()); + $this->assertTrue($provider->has(StringEnum::RED)); + $this->assertFalse($provider->has(StringEnum::GREEN)); + } } diff --git a/tests/Unit/Provider/ChannelNotFoundExceptionTest.php b/tests/Unit/Provider/ChannelNotFoundExceptionTest.php new file mode 100644 index 00000000..699a94b2 --- /dev/null +++ b/tests/Unit/Provider/ChannelNotFoundExceptionTest.php @@ -0,0 +1,30 @@ + ['channel1', 'channel1']; + yield 'string-enum' => ['red', StringEnum::RED]; + } + + #[DataProvider('dataBase')] + public function testBase(string $expectedChannelName, mixed $channel): void + { + $exception = new ChannelNotFoundException($channel); + + $this->assertSame( + 'Channel "' . $expectedChannelName . '" not found.', + $exception->getMessage(), + ); + } +}