Skip to content

Commit

Permalink
providers
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 12, 2024
1 parent c10a09f commit 6effbf2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions src/Debug/QueueProviderInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Debug;

use BackedEnum;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\QueueInterface;

Expand All @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Provider/AdapterFactoryQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Provider/ChannelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Yiisoft\Queue\Provider;

use BackedEnum;
use LogicException;
use Throwable;
use Yiisoft\Queue\ChannelNormalizer;

use function sprintf;

Expand All @@ -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,
);
Expand Down
5 changes: 3 additions & 2 deletions src/Provider/CompositeQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Provider;

use BackedEnum;
use Yiisoft\Queue\QueueInterface;

/**
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Provider/PrototypeQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Provider;

use BackedEnum;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\QueueInterface;

Expand All @@ -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;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Provider/QueueProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Provider;

use BackedEnum;
use Yiisoft\Queue\QueueInterface;

/**
Expand All @@ -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;
}
17 changes: 17 additions & 0 deletions tests/Unit/Provider/AdapterFactoryQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
30 changes: 30 additions & 0 deletions tests/Unit/Provider/ChannelNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Provider;

use PHPUnit\Framework\Attributes\DataProvider;
use Yiisoft\Queue\Provider\ChannelNotFoundException;
use Yiisoft\Queue\Tests\TestCase;
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;

final class ChannelNotFoundExceptionTest extends TestCase
{
public static function dataBase(): iterable
{
yield 'string' => ['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(),
);
}
}

0 comments on commit 6effbf2

Please sign in to comment.