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

BroadcastManager::connection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
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