ConnectionManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 73.91%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 52
ccs 17
cts 23
cp 0.7391
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerDefault() 0 3 1
A getDefault() 0 3 1
A get() 0 7 2
A has() 0 3 1
A register() 0 7 2
A release() 0 7 2
A hasDefault() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Driver;
4
5
use Igni\Storage\Exception\StorageException;
6
7
final class ConnectionManager
8
{
9
    /** @var Connection[] */
10
    private static $connections = [];
11
12
    private const DEFAULT_NAME = 'default';
13
14 49
    public static function release(): void
15
    {
16 49
        foreach (self::$connections as $connection) {
17 48
            $connection->close();
18
        }
19
20 49
        self::$connections = [];
21 49
    }
22
23 49
    public static function register(string $name, Connection $connection): void
24
    {
25 49
        if (self::has($name)) {
26
            throw StorageException::forAlreadyExistingConnection($name);
27
        }
28
29 49
        self::$connections[$name] = $connection;
30 49
    }
31
32 49
    public static function registerDefault(Connection $connection): void
33
    {
34 49
        self::register(self::DEFAULT_NAME, $connection);
35 49
    }
36
37 49
    public static function has(string $name): bool
38
    {
39 49
        return isset(self::$connections[$name]);
40
    }
41
42
    public static function hasDefault(): bool
43
    {
44
        return self::has(self::DEFAULT_NAME);
45
    }
46
47
    public static function getDefault(): Connection
48
    {
49
        return self::get(self::DEFAULT_NAME);
50
    }
51
52 22
    public static function get(string $name): Connection
53
    {
54 22
        if (!self::has($name)) {
55
            throw StorageException::forNotRegisteredConnection($name);
56
        }
57
58 22
        return self::$connections[$name];
59
    }
60
}
61