-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPredefinedQueueProvider.php
More file actions
71 lines (60 loc) · 1.85 KB
/
PredefinedQueueProvider.php
File metadata and controls
71 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Provider;
use BackedEnum;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\StringNormalizer;
use function array_key_exists;
use function array_keys;
use function get_debug_type;
use function sprintf;
/**
* Queue provider that uses a pre-defined map of queue name to queue instance.
*/
final class PredefinedQueueProvider implements QueueProviderInterface
{
/**
* @psalm-var array<string, QueueInterface>
*/
private readonly array $queues;
/**
* @param array $queues Map of queue name to queue instance.
*
* @psalm-param array<string, QueueInterface> $queues
*
* @throws InvalidQueueConfigException If a value in the array is not a {@see QueueInterface} instance.
*/
public function __construct(array $queues)
{
foreach ($queues as $name => $queue) {
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 = $queues;
}
public function get(string|BackedEnum $name): QueueInterface
{
$name = StringNormalizer::normalize($name);
if (!array_key_exists($name, $this->queues)) {
throw new QueueNotFoundException($name);
}
return $this->queues[$name];
}
public function has(string|BackedEnum $name): bool
{
$name = StringNormalizer::normalize($name);
return array_key_exists($name, $this->queues);
}
public function getNames(): array
{
return array_keys($this->queues);
}
}