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

BroadcastingBootloader::initTopicRegistry()   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 1
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\Bootloader;
6
7
use Spiral\Boot\Bootloader\Bootloader;
8
use Spiral\Boot\EnvironmentInterface;
9
use Spiral\Broadcasting\BroadcastInterface;
10
use Spiral\Broadcasting\BroadcastManager;
11
use Spiral\Broadcasting\BroadcastManagerInterface;
12
use Spiral\Broadcasting\Config\BroadcastConfig;
13
use Spiral\Broadcasting\Driver\LogBroadcast;
14
use Spiral\Broadcasting\Driver\NullBroadcast;
15
use Spiral\Broadcasting\TopicRegistry;
16
use Spiral\Broadcasting\TopicRegistryInterface;
17
use Spiral\Config\ConfiguratorInterface;
18
use Spiral\Config\Patch\Append;
19
20
final class BroadcastingBootloader extends Bootloader
21
{
22
    protected const SINGLETONS = [
23
        BroadcastManagerInterface::class => BroadcastManager::class,
24
        BroadcastInterface::class => [self::class, 'initDefaultBroadcast'],
25
        TopicRegistryInterface::class => [self::class, 'initTopicRegistry'],
26
    ];
27
28
    private ConfiguratorInterface $config;
29
30 196
    public function __construct(ConfiguratorInterface $config)
31
    {
32 196
        $this->config = $config;
33
    }
34
35
    public function registerDriverAlias(string $driverClass, string $alias): void
36
    {
37
        $this->config->modify(
38
            BroadcastConfig::CONFIG,
39
            new Append('driverAliases', $alias, $driverClass)
40
        );
41
    }
42
43 196
    public function boot(EnvironmentInterface $env): void
44
    {
45 196
        $this->initConfig($env);
46
    }
47
48 196
    private function initConfig(EnvironmentInterface $env): void
49
    {
50 196
        $this->config->setDefaults(
51
            BroadcastConfig::CONFIG,
52
            [
53 196
                'default' => $env->get('BROADCAST_CONNECTION', 'null'),
54
                'authorize' => [
55 196
                    'path' => $env->get('BROADCAST_AUTHORIZE_PATH'),
56
                    'topics' => [],
57
                ],
58
                'aliases' => [],
59
                'connections' => [
60
                    'null' => [
61
                        'driver' => 'null',
62
                    ],
63
                ],
64
                'driverAliases' => [
65
                    'null' => NullBroadcast::class,
66
                    'log' => LogBroadcast::class,
67
                ],
68
            ]
69
        );
70
    }
71
72 1
    private function initDefaultBroadcast(BroadcastManagerInterface $manager): BroadcastInterface
73
    {
74 1
        return $manager->connection();
75
    }
76
77 1
    private function initTopicRegistry(BroadcastConfig $config): TopicRegistryInterface
78
    {
79 1
        return new TopicRegistry($config->getTopics());
80
    }
81
}
82