ConnectionPool::addConnectionConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm;
4
5
use Janisbiz\LightOrm\Connection\ConnectionConfigInterface;
6
use Janisbiz\LightOrm\Connection\ConnectionInterface;
7
use Janisbiz\LightOrm\Connection\ConnectionInvalidArgumentException;
8
9
class ConnectionPool
10
{
11
    /**
12
     * @var ConnectionConfigInterface[]
13
     */
14
    protected static $connectionConfig = [];
15
16
    /**
17
     * @var ConnectionInterface[]
18
     */
19
    protected static $connections = [];
20
21
    /**
22
     * @param ConnectionConfigInterface $connectionConfig
23
     *
24
     * @return $this
25
     */
26
    public function addConnectionConfig(ConnectionConfigInterface $connectionConfig): ConnectionPool
27
    {
28
        static::$connectionConfig[$connectionConfig->getDbname()] = $connectionConfig;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param string $dbName
35
     *
36
     * @return ConnectionInterface
37
     * @throws ConnectionInvalidArgumentException
38
     */
39
    public function getConnection($dbName): ConnectionInterface
40
    {
41
        if (!\array_key_exists($dbName, static::$connectionConfig)) {
42
            if (empty(static::$connectionConfig)) {
43
                throw new ConnectionInvalidArgumentException(\sprintf(
44
                    'Could not find connection by name "%s"!',
45
                    $dbName
46
                ));
47
            } else {
48
                throw new ConnectionInvalidArgumentException(\sprintf(
49
                    'Could not find connection by name "%s"! Available connections: "%s".',
50
                    $dbName,
51
                    \implode('", "', \array_keys(static::$connectionConfig))
52
                ));
53
            }
54
        }
55
56
        $connectionConfig = static::$connectionConfig[$dbName];
57
58
        if (!isset(static::$connections[$dbName])) {
59
            static::$connections[$dbName] = $this->createConnection($connectionConfig);
60
        }
61
62
        return static::$connections[$dbName];
63
    }
64
65
    /**
66
     * @param string $dbName
67
     *
68
     * @return ConnectionInterface
69
     */
70
    public static function getConnectionStatic($dbName): ConnectionInterface
71
    {
72
        return (new static())->getConnection($dbName);
73
    }
74
75
    /**
76
     * Ignoring for code coverage, as this requires to have a real connection to be tested.
77
     * @codeCoverageIgnore
78
     *
79
     * @param ConnectionConfigInterface $connectionConfig
80
     *
81
     * @return ConnectionInterface
82
     */
83
    protected function createConnection(ConnectionConfigInterface $connectionConfig): ConnectionInterface
84
    {
85
        $adapterConnectionClass = $connectionConfig->getAdapterConnectionClass();
86
87
        return new $adapterConnectionClass(
88
            $connectionConfig->generateDsn(),
89
            $connectionConfig->getUsername(),
90
            $connectionConfig->getPassword()
91
        );
92
    }
93
}
94