Passed
Pull Request — master (#753)
by Maxim
17:58
created

QueueConfig::getRegistrySerializers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue\Config;
6
7
use Spiral\Core\Container\Autowire;
8
use Spiral\Core\InjectableConfig;
9
use Spiral\Queue\DefaultSerializer;
10
use Spiral\Queue\Exception\InvalidArgumentException;
11
use Spiral\Queue\SerializerInterface;
12
13
final class QueueConfig extends InjectableConfig
14
{
15
    public const CONFIG = 'queue';
16
17
    protected $config = [
18
        'default' => 'sync',
19
        'aliases' => [],
20
        'driverAliases' => [],
21
        'connections' => [],
22
        'defaultSerializer' => null,
23
        'registry' => [
24
            'handlers' => [],
25
            'serializers' => [],
26
        ],
27
    ];
28
29
    /**
30
     * Get connection aliases
31
     */
32 8
    public function getAliases(): array
33
    {
34 8
        return (array)($this->config['aliases'] ?? []);
35
    }
36
37
    /**
38
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
39
     * @throws InvalidArgumentException
40
     */
41 5
    public function getDefaultDriver(): string
42
    {
43 5
        if (!isset($this->config['default']) || empty($this->config['default'])) {
44 1
            throw new InvalidArgumentException('Default queue connection is not defined.');
45
        }
46
47 4
        if (!\is_string($this->config['default'])) {
48 1
            throw new InvalidArgumentException('Default queue connection config value must be a string');
49
        }
50
51 3
        return $this->config['default'];
52
    }
53
54
    /**
55
     * @return array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
56
     */
57 4
    public function getDriverAliases(): array
58
    {
59 4
        return (array)($this->config['driverAliases'] ?? []);
60
    }
61
62
    /**
63
     * @throws InvalidArgumentException
64
     */
65 15
    public function getConnections(?string $driver = null): array
66
    {
67 15
        $connections = $this->config['connections'] ?? [];
68
69 15
        if ($driver === null) {
70 13
            return $connections;
71
        }
72
73 2
        $driverAliases = $this->getDriverAliases();
74
75 2
        if (isset($driverAliases[$driver])) {
76 1
            if (!\is_string($this->config['driverAliases'][$driver])) {
77
                throw new InvalidArgumentException(
78
                    sprintf('Driver alias for `%s` value must be a string', $driver)
79
                );
80
            }
81
82 1
            $driver = $driverAliases[$driver];
83
        }
84
85 2
        return array_filter($connections, static function (array $connection) use ($driverAliases, $driver): bool {
86 2
            if (empty($connection['driver'])) {
87 2
                return false;
88
            }
89
90 2
            $connectionDriver = $driverAliases[$connection['driver']] ?? $connection['driver'];
91
92 2
            return $connectionDriver === $driver;
93
        });
94
    }
95
96
    /**
97
     * @return array<string, mixed>
98
     * @throws InvalidArgumentException
99
     */
100 11
    public function getConnection(string $name): array
101
    {
102 11
        $connections = $this->getConnections();
103
104 11
        if (!isset($connections[$name])) {
105 2
            throw new InvalidArgumentException(sprintf('Queue connection with given name `%s` is not defined.', $name));
106
        }
107
108 10
        if (!isset($connections[$name]['driver'])) {
109 1
            throw new InvalidArgumentException(sprintf('Driver for queue connection `%s` is not defined.', $name));
110
        }
111
112 9
        $connection = $connections[$name];
113 9
        $driver = $connection['driver'];
114
115 9
        if (!\is_string($driver)) {
116 1
            throw new InvalidArgumentException(
117 1
                sprintf('Driver for queue connection `%s` value must be a string', $name)
118
            );
119
        }
120
121 8
        if (isset($this->config['driverAliases'][$driver])) {
122 5
            $connection['driver'] = $this->config['driverAliases'][$driver];
123
        }
124
125 8
        if (!\is_string($connection['driver'])) {
126 1
            throw new InvalidArgumentException(
127 1
                sprintf('Driver alias for queue connection `%s` value must be a string', $name)
128
            );
129
        }
130
131 7
        return $connection;
132
    }
133
134
    /**
135
     * @return array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
136
     */
137 209
    public function getRegistryHandlers(): array
138
    {
139 209
        return (array)($this->config['registry']['handlers'] ?? []);
140
    }
141
142
    /**
143
     * @psalm-return array<string, SerializerInterface|class-string|Autowire>
144
     */
145 209
    public function getRegistrySerializers(): array
146
    {
147 209
        return (array)($this->config['registry']['serializers'] ?? []);
148
    }
149
150
    /**
151
     * @psalm-return SerializerInterface|class-string|Autowire
152
     */
153 212
    public function getDefaultSerializer()
154
    {
155 212
        return $this->config['defaultSerializer'] ?? new DefaultSerializer();
156
    }
157
}
158