Completed
Push — master ( f53b98...356c58 )
by Dawid
03:12
created

ConnectionManager::has()   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
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 register(Connection $connection, string $name = 'default'): void
24
    {
25 49
        if (!self::hasDefault()) {
26 49
            self::$defaultConnection = $connection;
27
        }
28
29 49
        if (self::has($name)) {
30
            throw StorageException::forAlreadyExistingConnection($name);
31
        }
32
33 49
        self::$connections[$name] = $connection;
34 49
    }
35
36 49
    public static function has(string $name): bool
37
    {
38 49
        return isset(self::$connections[$name]);
39
    }
40
41 49
    public static function hasDefault(): bool
42
    {
43 49
        return self::$defaultConnection !== null;
44
    }
45
46
    public static function getDefault(): Connection
47
    {
48
        if (!self::hasDefault()) {
49
            throw StorageException::forNotRegisteredConnection('default');
50
        }
51
        return self::$defaultConnection;
52
    }
53
54 22
    public static function get(string $name): Connection
55
    {
56 22
        if (!self::has($name)) {
57
            throw StorageException::forNotRegisteredConnection($name);
58
        }
59
60 22
        return self::$connections[$name];
61
    }
62
}
63