-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Yiisoft\Definitions\Exception\InvalidConfigException; | ||
use Yiisoft\Factory\StrictFactory; | ||
use Yiisoft\Queue\Adapter\AdapterInterface; | ||
use Yiisoft\Queue\QueueInterface; | ||
|
||
use function array_key_exists; | ||
use function sprintf; | ||
|
||
final class AdapterFactoryQueueProvider implements QueueProviderInterface | ||
{ | ||
/** | ||
* @psalm-var array<string, QueueInterface|null> | ||
*/ | ||
private array $queues = []; | ||
|
||
private readonly StrictFactory $factory; | ||
|
||
/** | ||
* @psalm-param array<string, mixed> $definitions | ||
* @throws InvalidQueueConfigException | ||
*/ | ||
public function __construct( | ||
private readonly QueueInterface $baseQueue, | ||
array $definitions, | ||
?ContainerInterface $container = null, | ||
bool $validate = true, | ||
) { | ||
try { | ||
$this->factory = new StrictFactory($definitions, $container, $validate); | ||
} catch (InvalidConfigException $exception) { | ||
throw new InvalidQueueConfigException($exception->getMessage(), previous: $exception); | ||
} | ||
} | ||
|
||
public function get(string $channel): QueueInterface | ||
{ | ||
$queue = $this->getOrTryCreate($channel); | ||
if ($queue === null) { | ||
throw new ChannelNotFoundException($channel); | ||
} | ||
return $queue; | ||
} | ||
|
||
public function has(string $channel): bool | ||
{ | ||
return $this->factory->has($channel); | ||
} | ||
|
||
/** | ||
* @throws InvalidQueueConfigException | ||
*/ | ||
private function getOrTryCreate(string $channel): QueueInterface|null | ||
{ | ||
if (array_key_exists($channel, $this->queues)) { | ||
return $this->queues[$channel]; | ||
} | ||
|
||
if ($this->factory->has($channel)) { | ||
$adapter = $this->factory->create($channel); | ||
if (!$adapter instanceof AdapterInterface) { | ||
throw new InvalidQueueConfigException( | ||
sprintf( | ||
'Adapter must implement "%s". For channel "%s" got "%s" instead.', | ||
AdapterInterface::class, | ||
$channel, | ||
get_debug_type($adapter), | ||
), | ||
); | ||
} | ||
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter)->withChannelName($channel); | ||
} else { | ||
$this->queues[$channel] = null; | ||
} | ||
|
||
return $this->queues[$channel]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/Unit/Provider/AdapterFactoryQueueProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Provider; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Queue\Adapter\AdapterInterface; | ||
use Yiisoft\Queue\Adapter\StubAdapter; | ||
use Yiisoft\Queue\Cli\StubLoop; | ||
use Yiisoft\Queue\Provider\AdapterFactoryQueueProvider; | ||
use Yiisoft\Queue\Provider\ChannelNotFoundException; | ||
use Yiisoft\Queue\Provider\InvalidQueueConfigException; | ||
use Yiisoft\Queue\StubQueue; | ||
|
||
use function sprintf; | ||
|
||
final class AdapterFactoryQueueProviderTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$provider = new AdapterFactoryQueueProvider( | ||
new StubQueue(), | ||
[ | ||
'channel1' => StubAdapter::class, | ||
], | ||
); | ||
|
||
$queue = $provider->get('channel1'); | ||
|
||
$this->assertInstanceOf(StubQueue::class, $queue); | ||
$this->assertSame('channel1', $queue->getChannelName()); | ||
$this->assertInstanceOf(StubAdapter::class, $queue->getAdapter()); | ||
$this->assertTrue($provider->has('channel1')); | ||
$this->assertFalse($provider->has('not-exist-channel')); | ||
} | ||
|
||
public function testGetTwice(): void | ||
{ | ||
$provider = new AdapterFactoryQueueProvider( | ||
new StubQueue(), | ||
[ | ||
'channel1' => StubAdapter::class, | ||
], | ||
); | ||
|
||
$queue1 = $provider->get('channel1'); | ||
$queue2 = $provider->get('channel1'); | ||
|
||
$this->assertSame($queue1, $queue2); | ||
} | ||
|
||
public function testGetNotExistChannel(): void | ||
{ | ||
$provider = new AdapterFactoryQueueProvider( | ||
new StubQueue(), | ||
[ | ||
'channel1' => StubAdapter::class, | ||
], | ||
); | ||
|
||
$this->expectException(ChannelNotFoundException::class); | ||
$this->expectExceptionMessage('Channel "not-exist-channel" not found.'); | ||
$provider->get('not-exist-channel'); | ||
} | ||
|
||
public function testInvalidQueueConfig(): void | ||
{ | ||
$baseQueue = new StubQueue(); | ||
$definitions = [ | ||
'channel1' => [ | ||
'class' => StubAdapter::class, | ||
'__construct()' => 'hello', | ||
], | ||
]; | ||
|
||
$this->expectException(InvalidQueueConfigException::class); | ||
$this->expectExceptionMessage( | ||
'Invalid definition: incorrect constructor arguments. Expected array, got string.' | ||
); | ||
new AdapterFactoryQueueProvider($baseQueue, $definitions); | ||
} | ||
|
||
public function testInvalidQueueConfigOnGet(): void | ||
{ | ||
$provider = new AdapterFactoryQueueProvider( | ||
new StubQueue(), | ||
[ | ||
'channel1' => StubLoop::class, | ||
] | ||
); | ||
|
||
$this->expectException(InvalidQueueConfigException::class); | ||
$this->expectExceptionMessage( | ||
sprintf( | ||
'Adapter must implement "%s". For channel "channel1" got "%s" instead.', | ||
AdapterInterface::class, | ||
StubLoop::class, | ||
) | ||
); | ||
$provider->get('channel1'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters