Skip to content

Commit e2af401

Browse files
authored
Allow to use backed enumerations as channel name (#229)
1 parent 8af373f commit e2af401

19 files changed

+197
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"psr/container": "^1.0|^2.0",
4141
"psr/log": "^2.0|^3.0",
4242
"symfony/console": "^5.4|^6.0",
43-
"yiisoft/definitions": "^1.0|^2.0|^3.0",
43+
"yiisoft/definitions": "^3.3.1",
4444
"yiisoft/factory": "^1.3",
4545
"yiisoft/friendly-exception": "^1.0",
4646
"yiisoft/injector": "^1.0"

src/Adapter/AdapterInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Adapter;
66

7+
use BackedEnum;
78
use InvalidArgumentException;
89
use Yiisoft\Queue\Enum\JobStatus;
910
use Yiisoft\Queue\Message\MessageInterface;
@@ -40,7 +41,7 @@ public function push(MessageInterface $message): MessageInterface;
4041
*/
4142
public function subscribe(callable $handlerCallback): void;
4243

43-
public function withChannel(string $channel): self;
44+
public function withChannel(string|BackedEnum $channel): self;
4445

4546
public function getChannelName(): string;
4647
}

src/Adapter/SynchronousAdapter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Yiisoft\Queue\Adapter;
66

7+
use BackedEnum;
78
use InvalidArgumentException;
9+
use Yiisoft\Queue\ChannelNormalizer;
810
use Yiisoft\Queue\Enum\JobStatus;
911
use Yiisoft\Queue\Message\MessageInterface;
1012
use Yiisoft\Queue\QueueInterface;
@@ -15,12 +17,14 @@ final class SynchronousAdapter implements AdapterInterface
1517
{
1618
private array $messages = [];
1719
private int $current = 0;
20+
private string $channel;
1821

1922
public function __construct(
2023
private WorkerInterface $worker,
2124
private QueueInterface $queue,
22-
private string $channel = QueueInterface::DEFAULT_CHANNEL_NAME,
25+
string|BackedEnum $channel = QueueInterface::DEFAULT_CHANNEL_NAME,
2326
) {
27+
$this->channel = ChannelNormalizer::normalize($channel);
2428
}
2529

2630
public function __destruct()
@@ -74,8 +78,10 @@ public function subscribe(callable $handlerCallback): void
7478
$this->runExisting($handlerCallback);
7579
}
7680

77-
public function withChannel(string $channel): self
81+
public function withChannel(string|BackedEnum $channel): self
7882
{
83+
$channel = ChannelNormalizer::normalize($channel);
84+
7985
if ($channel === $this->channel) {
8086
return $this;
8187
}

src/ChannelNormalizer.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Queue;
6+
7+
use BackedEnum;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class ChannelNormalizer
13+
{
14+
public static function normalize(string|BackedEnum $channel): string
15+
{
16+
return $channel instanceof BackedEnum ? (string) $channel->value : $channel;
17+
}
18+
}

src/Debug/QueueProviderInterfaceProxy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Debug;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\Provider\QueueProviderInterface;
89
use Yiisoft\Queue\QueueInterface;
910

@@ -15,13 +16,13 @@ public function __construct(
1516
) {
1617
}
1718

18-
public function get(string $channel): QueueInterface
19+
public function get(string|BackedEnum $channel): QueueInterface
1920
{
2021
$queue = $this->queueProvider->get($channel);
2122
return new QueueDecorator($queue, $this->collector);
2223
}
2324

24-
public function has(string $channel): bool
25+
public function has(string|BackedEnum $channel): bool
2526
{
2627
return $this->queueProvider->has($channel);
2728
}

src/Provider/AdapterFactoryQueueProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace Yiisoft\Queue\Provider;
66

7+
use BackedEnum;
78
use Psr\Container\ContainerInterface;
89
use Yiisoft\Definitions\Exception\InvalidConfigException;
910
use Yiisoft\Factory\StrictFactory;
1011
use Yiisoft\Queue\Adapter\AdapterInterface;
12+
use Yiisoft\Queue\ChannelNormalizer;
1113
use Yiisoft\Queue\QueueInterface;
1214

1315
use function array_key_exists;
@@ -50,17 +52,21 @@ public function __construct(
5052
}
5153
}
5254

53-
public function get(string $channel): QueueInterface
55+
public function get(string|BackedEnum $channel): QueueInterface
5456
{
57+
$channel = ChannelNormalizer::normalize($channel);
58+
5559
$queue = $this->getOrTryToCreate($channel);
5660
if ($queue === null) {
5761
throw new ChannelNotFoundException($channel);
5862
}
63+
5964
return $queue;
6065
}
6166

62-
public function has(string $channel): bool
67+
public function has(string|BackedEnum $channel): bool
6368
{
69+
$channel = ChannelNormalizer::normalize($channel);
6470
return $this->factory->has($channel);
6571
}
6672

src/Provider/ChannelNotFoundException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Yiisoft\Queue\Provider;
66

7+
use BackedEnum;
78
use LogicException;
89
use Throwable;
10+
use Yiisoft\Queue\ChannelNormalizer;
911

1012
use function sprintf;
1113

@@ -14,10 +16,10 @@
1416
*/
1517
final class ChannelNotFoundException extends LogicException implements QueueProviderException
1618
{
17-
public function __construct(string $channel, int $code = 0, ?Throwable $previous = null)
19+
public function __construct(string|BackedEnum $channel, int $code = 0, ?Throwable $previous = null)
1820
{
1921
parent::__construct(
20-
sprintf('Channel "%s" not found.', $channel),
22+
sprintf('Channel "%s" not found.', ChannelNormalizer::normalize($channel)),
2123
$code,
2224
$previous,
2325
);

src/Provider/CompositeQueueProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Provider;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\QueueInterface;
89

910
/**
@@ -25,7 +26,7 @@ public function __construct(
2526
$this->providers = $providers;
2627
}
2728

28-
public function get(string $channel): QueueInterface
29+
public function get(string|BackedEnum $channel): QueueInterface
2930
{
3031
foreach ($this->providers as $provider) {
3132
if ($provider->has($channel)) {
@@ -35,7 +36,7 @@ public function get(string $channel): QueueInterface
3536
throw new ChannelNotFoundException($channel);
3637
}
3738

38-
public function has(string $channel): bool
39+
public function has(string|BackedEnum $channel): bool
3940
{
4041
foreach ($this->providers as $provider) {
4142
if ($provider->has($channel)) {

src/Provider/PrototypeQueueProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Provider;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\Adapter\AdapterInterface;
89
use Yiisoft\Queue\QueueInterface;
910

@@ -22,12 +23,12 @@ public function __construct(
2223
) {
2324
}
2425

25-
public function get(string $channel): QueueInterface
26+
public function get(string|BackedEnum $channel): QueueInterface
2627
{
2728
return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel));
2829
}
2930

30-
public function has(string $channel): bool
31+
public function has(string|BackedEnum $channel): bool
3132
{
3233
return true;
3334
}

src/Provider/QueueProviderInterface.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Provider;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\QueueInterface;
89

910
/**
@@ -14,21 +15,21 @@ interface QueueProviderInterface
1415
/**
1516
* Find a queue by channel name and returns it.
1617
*
17-
* @param string $channel Channel name.
18+
* @param BackedEnum|string $channel Channel name.
1819
*
1920
* @throws InvalidQueueConfigException If the queue configuration is invalid.
2021
* @throws ChannelNotFoundException If the channel is not found.
2122
* @throws QueueProviderException If the queue provider fails to provide a queue.
2223
* @return QueueInterface Queue instance.
2324
*/
24-
public function get(string $channel): QueueInterface;
25+
public function get(string|BackedEnum $channel): QueueInterface;
2526

2627
/**
2728
* Check if a queue with the specified channel name exists.
2829
*
29-
* @param string $channel Channel name.
30+
* @param BackedEnum|string $channel Channel name.
3031
*
3132
* @return bool Whether the queue exists.
3233
*/
33-
public function has(string $channel): bool;
34+
public function has(string|BackedEnum $channel): bool;
3435
}

stubs/StubAdapter.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Yiisoft\Queue\Stubs;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\Adapter\AdapterInterface;
9+
use Yiisoft\Queue\ChannelNormalizer;
810
use Yiisoft\Queue\Enum\JobStatus;
911
use Yiisoft\Queue\Message\MessageInterface;
1012
use Yiisoft\Queue\QueueInterface;
@@ -14,8 +16,12 @@
1416
*/
1517
final class StubAdapter implements AdapterInterface
1618
{
17-
public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME)
18-
{
19+
private string $channelName;
20+
21+
public function __construct(
22+
string|BackedEnum $channelName = QueueInterface::DEFAULT_CHANNEL_NAME
23+
) {
24+
$this->channelName = ChannelNormalizer::normalize($channelName);
1925
}
2026

2127
public function runExisting(callable $handlerCallback): void
@@ -36,11 +42,10 @@ public function subscribe(callable $handlerCallback): void
3642
{
3743
}
3844

39-
public function withChannel(string $channel): AdapterInterface
45+
public function withChannel(string|BackedEnum $channel): AdapterInterface
4046
{
4147
$new = clone $this;
42-
$new->channelName = $channel;
43-
48+
$new->channelName = ChannelNormalizer::normalize($channel);
4449
return $new;
4550
}
4651

tests/App/FakeAdapter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Yiisoft\Queue\Tests\App;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\Adapter\AdapterInterface;
9+
use Yiisoft\Queue\ChannelNormalizer;
810
use Yiisoft\Queue\Enum\JobStatus;
911
use Yiisoft\Queue\Message\MessageInterface;
1012

@@ -35,12 +37,11 @@ public function subscribe(callable $handlerCallback): void
3537
//skip
3638
}
3739

38-
public function withChannel(string $channel): AdapterInterface
40+
public function withChannel(string|BackedEnum $channel): AdapterInterface
3941
{
4042
$instance = clone $this;
4143
$instance->pushMessages = [];
42-
$instance->channel = $channel;
43-
44+
$instance->channel = ChannelNormalizer::normalize($channel);
4445
return $instance;
4546
}
4647

tests/Benchmark/Support/VoidAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Tests\Benchmark\Support;
66

7+
use BackedEnum;
78
use InvalidArgumentException;
89
use RuntimeException;
910
use Yiisoft\Queue\Adapter\AdapterInterface;
@@ -45,7 +46,7 @@ public function subscribe(callable $handlerCallback): void
4546
throw new RuntimeException('Method is not implemented');
4647
}
4748

48-
public function withChannel(string $channel): AdapterInterface
49+
public function withChannel(string|BackedEnum $channel): AdapterInterface
4950
{
5051
throw new RuntimeException('Method is not implemented');
5152
}

tests/Unit/SynchronousAdapterTest.php renamed to tests/Unit/Adapter/SynchronousAdapterTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace Yiisoft\Queue\Tests\Unit;
5+
namespace Yiisoft\Queue\Tests\Unit\Adapter;
66

7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use Yiisoft\Queue\Adapter\SynchronousAdapter;
79
use Yiisoft\Queue\Enum\JobStatus;
10+
use Yiisoft\Queue\Message\IdEnvelope;
811
use Yiisoft\Queue\Message\Message;
912
use Yiisoft\Queue\QueueInterface;
13+
use Yiisoft\Queue\Stubs\StubQueue;
14+
use Yiisoft\Queue\Stubs\StubWorker;
1015
use Yiisoft\Queue\Tests\TestCase;
11-
use Yiisoft\Queue\Message\IdEnvelope;
16+
use Yiisoft\Queue\Tests\Unit\Support\IntEnum;
17+
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;
1218

1319
final class SynchronousAdapterTest extends TestCase
1420
{
@@ -92,4 +98,27 @@ public function testStatusNotMessage(): void
9298
$this->expectExceptionMessage('There is no message with the given ID.');
9399
$adapter->status('1');
94100
}
101+
102+
public static function dataChannels(): iterable
103+
{
104+
yield 'string' => ['test', 'test'];
105+
yield 'string-enum' => ['red', StringEnum::RED];
106+
yield 'integer-enum' => ['1', IntEnum::ONE];
107+
}
108+
109+
#[DataProvider('dataChannels')]
110+
public function testWithChannel(string $expected, mixed $channel): void
111+
{
112+
$adapter = (new SynchronousAdapter(new StubWorker(), new StubQueue()))->withChannel($channel);
113+
114+
$this->assertSame($expected, $adapter->getChannelName());
115+
}
116+
117+
#[DataProvider('dataChannels')]
118+
public function testChannelInConstructor(string $expected, mixed $channel): void
119+
{
120+
$adapter = new SynchronousAdapter(new StubWorker(), new StubQueue(), $channel);
121+
122+
$this->assertSame($expected, $adapter->getChannelName());
123+
}
95124
}

0 commit comments

Comments
 (0)