Passed
Push — master ( 60d2ef...11c56d )
by butschster
03:22 queued 17s
created

BroadcastManager::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Broadcasting;
6
7
use Spiral\Broadcasting\Config\BroadcastConfig;
8
use Spiral\Core\Container\SingletonInterface;
9
use Spiral\Core\FactoryInterface;
10
11
final class BroadcastManager implements BroadcastManagerInterface, SingletonInterface
12
{
13
    private FactoryInterface $factory;
14
    private BroadcastConfig $config;
15
    /** @var BroadcastInterface[] */
16
    private array $connections = [];
17
18 7
    public function __construct(
19
        FactoryInterface $factory,
20
        BroadcastConfig $config
21
    ) {
22 7
        $this->factory = $factory;
23 7
        $this->config = $config;
24
    }
25
26 7
    public function connection(?string $name = null): BroadcastInterface
27
    {
28 7
        $name = $name ?: $this->config->getDefaultConnection();
29
30
        // Replaces alias with real storage name
31 7
        $name = $this->config->getAliases()[$name] ?? $name;
32
33 7
        if (isset($this->connections[$name])) {
34 1
            return $this->connections[$name];
35
        }
36
37 7
        return $this->connections[$name] = $this->resolve($name);
38
    }
39
40 7
    private function resolve(string $name): BroadcastInterface
41
    {
42 7
        $config = $this->config->getConnectionConfig($name);
43
44 7
        return $this->factory->make($config['driver'], $config);
45
    }
46
}
47