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

Lines changed: 197 additions & 29 deletions

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
}

0 commit comments

Comments
 (0)