Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/Provider/QueueFactoryProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Provider;

use BackedEnum;
use Psr\Container\ContainerInterface;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Factory\StrictFactory;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\StringNormalizer;

use function array_key_exists;
use function sprintf;

/**
* This queue provider creates queue objects directly from definitions.
*
* @see https://github.com/yiisoft/definitions/
* @see https://github.com/yiisoft/factory/
*/
final class QueueFactoryProvider implements QueueProviderInterface
{
/**
* @psalm-var array<string, QueueInterface|null>
*/
private array $queues = [];

private readonly StrictFactory $factory;

/**
* @param array $definitions Queue definitions indexed by queue names.
* @param ContainerInterface|null $container Container to use for dependencies resolving.
* @param bool $validate If definitions should be validated when set.
*
* @psalm-param array<string, mixed> $definitions
*
* @throws InvalidQueueConfigException
*/
public function __construct(
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|BackedEnum $name): QueueInterface
{
$name = StringNormalizer::normalize($name);

$queue = $this->getOrTryToCreate($name);
if ($queue === null) {
throw new QueueNotFoundException($name);
}

return $queue;
}

public function has(string|BackedEnum $name): bool
{
$name = StringNormalizer::normalize($name);
return $this->factory->has($name);
}

/**
* @throws InvalidQueueConfigException
*/
private function getOrTryToCreate(string $name): ?QueueInterface
{
if (array_key_exists($name, $this->queues)) {
return $this->queues[$name];
}

if (!$this->factory->has($name)) {
$this->queues[$name] = null;
return null;
}

try {
$queue = $this->factory->create($name);
} catch (InvalidConfigException $exception) {
throw new InvalidQueueConfigException($exception->getMessage(), previous: $exception);
}

if (!$queue instanceof QueueInterface) {
throw new InvalidQueueConfigException(
sprintf(
'Queue must implement "%s". For queue "%s" got "%s" instead.',
QueueInterface::class,
$name,
get_debug_type($queue),
),
);
}

$this->queues[$name] = $queue;
return $queue;
}
}
156 changes: 156 additions & 0 deletions tests/Unit/Provider/QueueFactoryProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Provider;

use PHPUnit\Framework\TestCase;
use Yiisoft\Definitions\Reference;
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Provider\InvalidQueueConfigException;
use Yiisoft\Queue\Provider\QueueFactoryProvider;
use Yiisoft\Queue\Provider\QueueNotFoundException;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\Stubs\StubAdapter;
use Yiisoft\Queue\Stubs\StubLoop;
use Yiisoft\Queue\Stubs\StubQueue;
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;
use Yiisoft\Test\Support\Container\SimpleContainer;

use function sprintf;

final class QueueFactoryProviderTest extends TestCase
{
public function testBase(): void
{
$provider = new QueueFactoryProvider(
[
'queue1' => StubQueue::class,
],
);

$queue = $provider->get('queue1');

$this->assertInstanceOf(StubQueue::class, $queue);
$this->assertTrue($provider->has('queue1'));
$this->assertFalse($provider->has('not-exist-queue'));
}

public function testGetTwice(): void
{
$provider = new QueueFactoryProvider(
[
'queue1' => StubQueue::class,
],
);

$queue1 = $provider->get('queue1');
$queue2 = $provider->get('queue1');

$this->assertSame($queue1, $queue2);
}

public function testGetNotExistQueue(): void
{
$provider = new QueueFactoryProvider(
[
'queue1' => StubQueue::class,
],
);

$this->expectException(QueueNotFoundException::class);
$this->expectExceptionMessage('Queue with name "not-exist-queue" not found.');
$provider->get('not-exist-queue');
}

public function testInvalidQueueConfig(): void
{
$definitions = [
'queue1' => [
'class' => StubQueue::class,
'__construct()' => 'hello',
],
];

$this->expectException(InvalidQueueConfigException::class);
$this->expectExceptionMessage(
'Invalid definition: incorrect constructor arguments. Expected array, got string.',
);
new QueueFactoryProvider($definitions);
}

public function testInvalidQueueConfigOnGet(): void
{
$provider = new QueueFactoryProvider(
[
'queue1' => StubLoop::class,
],
);

$this->expectException(InvalidQueueConfigException::class);
$this->expectExceptionMessage(
sprintf(
'Queue must implement "%s". For queue "%s" got "%s" instead.',
QueueInterface::class,
'queue1',
StubLoop::class,
),
);
$provider->get('queue1');
}

public function testGetHasByStringEnum(): void
{
$provider = new QueueFactoryProvider(
[
'red' => StubQueue::class,
],
);

$queue = $provider->get(StringEnum::RED);

$this->assertInstanceOf(StubQueue::class, $queue);
$this->assertTrue($provider->has(StringEnum::RED));
$this->assertFalse($provider->has(StringEnum::GREEN));
}

public function testWithContainer(): void
{
$adapter = new StubAdapter();
$container = new SimpleContainer([
AdapterInterface::class => $adapter,
]);

$provider = new QueueFactoryProvider(
[
'queue1' => [
'class' => StubQueue::class,
'__construct()' => [
'adapter' => Reference::to(AdapterInterface::class),
],
],
],
$container,
);

$queue = $provider->get('queue1');

$this->assertInstanceOf(StubQueue::class, $queue);
$this->assertSame($adapter, $queue->getAdapter());
}

public function testValidateFalse(): void
{
$provider = new QueueFactoryProvider(
[
'queue1' => [
'class' => StubQueue::class,
'__construct()' => 'hello',
],
],
validate: false,
);

$this->assertTrue($provider->has('queue1'));
}
}
Loading