Completed
Push — master ( 408932...ce7eb5 )
by Dawid
06:58
created

ConnectionManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 76%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 54
ccs 19
cts 25
cp 0.76
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasDefaultConnection() 0 3 1
A release() 0 8 2
A addConnection() 0 11 3
A getDefaultConnection() 0 6 2
A getConnection() 0 7 2
A hasConnection() 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
    private static $defaultConnection;
10
    /** @var Connection[] */
11
    private static $connections = [];
12
13 49
    public static function release(): void
14
    {
15 49
        foreach (self::$connections as $connection) {
16 48
            $connection->close();
17
        }
18
19 49
        self::$defaultConnection = null;
20 49
        self::$connections = [];
21 49
    }
22
23 49
    public static function addConnection(Connection $connection, string $name = 'default'): void
24
    {
25 49
        if (!self::hasDefaultConnection()) {
26 49
            self::$defaultConnection = $connection;
27
        }
28
29 49
        if (self::hasConnection($name)) {
30
            throw StorageException::forAlreadyExistingConnection($name);
31
        }
32
33 49
        self::$connections[$name] = $connection;
34 49
    }
35
36 49
    public static function hasConnection(string $name): bool
37
    {
38 49
        return isset(self::$connections[$name]);
39
    }
40
41 49
    public static function hasDefaultConnection(): bool
42
    {
43 49
        return self::$defaultConnection !== null;
44
    }
45
46
    public static function getDefaultConnection(): Connection
47
    {
48
        if (!self::hasDefaultConnection()) {
49
            throw StorageException::forNotRegisteredConnection('default');
50
        }
51
        return self::$defaultConnection;
52
    }
53
54 22
    public static function getConnection(string $name): Connection
55
    {
56 22
        if (!self::hasConnection($name)) {
57
            throw StorageException::forNotRegisteredConnection($name);
58
        }
59
60 22
        return self::$connections[$name];
61
    }
62
}
63