Passed
Push — master ( 11c56d...7a080f )
by butschster
02:58 queued 18s
created

BroadcastConfig::getAuthorizationPath()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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\Broadcasting\Config;
6
7
use Spiral\Broadcasting\Exception\InvalidArgumentException;
8
use Spiral\Core\InjectableConfig;
9
10
final class BroadcastConfig extends InjectableConfig
11
{
12
    public const CONFIG = 'broadcasting';
13
14
    protected $config = [
15
        'authorize' => [
16
            'path' => null,
17
            'topics' => [],
18
        ],
19
        'default' => 'null',
20
        'aliases' => [],
21
        'connections' => [],
22
        'driverAliases' => [],
23
    ];
24
25
    /**
26
     * Get registerer broadcast topics.
27
     *
28
     * @return array<string, callable>
29
     */
30 2
    public function getTopics(): array
31
    {
32 2
        return (array)($this->config['authorize']['topics'] ?? []);
33
    }
34
35
    /**
36
     * Get authorization path for broadcasting topics.
37
     */
38 198
    public function getAuthorizationPath(): ?string
39
    {
40 198
        return $this->config['authorize']['path'] ?? null;
41
    }
42
43
    /**
44
     * Get broadcast driver aliases
45
     */
46 7
    public function getAliases(): array
47
    {
48 7
        return (array)($this->config['aliases'] ?? []);
49
    }
50
51
    /**
52
     * Get default broadcast connection
53
     */
54 5
    public function getDefaultConnection(): string
55
    {
56 5
        if (!isset($this->config['default']) || empty($this->config['default'])) {
57 1
            throw new InvalidArgumentException('Default broadcast connection is not defined.');
58
        }
59
60 4
        if (!\is_string($this->config['default'])) {
61
            throw new InvalidArgumentException('Default broadcast connection config value must be a string');
62
        }
63
64 4
        return $this->config['default'];
65
    }
66
67 11
    public function getConnectionConfig(string $name): array
68
    {
69 11
        if (!isset($this->config['connections'][$name])) {
70 1
            throw new InvalidArgumentException(
71 1
                sprintf('Config for connection `%s` is not defined.', $name)
72
            );
73
        }
74
75 10
        $config = $this->config['connections'][$name];
76
77 10
        if (!isset($config['driver'])) {
78 1
            throw new InvalidArgumentException(
79 1
                sprintf('Driver for `%s` connection is not defined.', $name)
80
            );
81
        }
82
83 9
        if (!\is_string($config['driver'])) {
84
            throw new InvalidArgumentException(
85
                \sprintf('Driver value for `%s` connection must be a string', $name)
86
            );
87
        }
88
89 9
        if (isset($this->config['driverAliases'][$config['driver']])) {
90 2
            $config['driver'] = $this->config['driverAliases'][$config['driver']];
91
        }
92
93 9
        return $config;
94
    }
95
}
96