Passed
Push — develop ( a25cb7...a43744 )
by nguereza
03:28
created

Pool::addConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 19
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Pool.php
33
 *
34
 *  The Database Connection pool class
35
 *
36
 *  @package    Platine\Database
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
declare(strict_types=1);
45
46
namespace Platine\Database;
47
48
use Platine\Database\Exception\ConnectionAlreadyExistsException;
49
use Platine\Database\Exception\ConnectionNotFoundException;
50
51
/**
52
 * Class Pool
53
 * @package Platine\Database
54
 */
55
class Pool
56
{
57
58
    /**
59
     * The default connection name
60
     * @var string
61
     */
62
    protected string $default = 'default';
63
64
    /**
65
     * The list of connections
66
     * @var array<string, Connection>
67
     */
68
    protected array $connections = [];
69
70
    /**
71
     * Class constructor
72
     * @param array<string, mixed> $config
73
     */
74
    public function __construct(array $config = [])
75
    {
76
        if (!empty($config)) {
77
            if (
78
                !empty($config['connections'])
79
                && is_array($config['connections'])
80
            ) {
81
                 /** @var array<string, array<string, mixed>> $connections */
82
                $connections = $config['connections'];
83
84
                foreach ($connections as $name => $connection) {
85
                    $connection['name'] = $name;
86
                    $this->addConnection($connection);
87
                }
88
            }
89
90
            if (isset($config['default'])) {
91
                $this->setDefault($config['default']);
92
            }
93
        }
94
    }
95
96
    /**
97
     * Add new connection to the pool
98
     * @param array<string, mixed> $config
99
     * @return void
100
     * @throws ConnectionAlreadyExistsException
101
     */
102
    public function addConnection(array $config): void
103
    {
104
        /** @var ConfigurationInterface $cfg */
105
        $cfg = new Configuration($config);
106
107
        $name = $cfg->getName();
108
109
        if ($this->has($name)) {
110
            throw new ConnectionAlreadyExistsException(
111
                sprintf('The connection [%s] already exists', $name)
112
            );
113
        }
114
115
        $actives = count($this->connections);
116
117
        $this->connections[$name] = new Connection($cfg);
118
119
        if ($actives === 0) {
120
            $this->default = $name;
121
        }
122
    }
123
124
    /**
125
     * Check whether the connection exists
126
     * @param string $name
127
     * @return bool
128
     */
129
    public function has(string $name): bool
130
    {
131
        return isset($this->connections[$name]);
132
    }
133
134
    /**
135
     * Get the connection instance for the given name
136
     * if $name is null the default will be returned
137
     * @param string|null $name
138
     * @return Connection
139
     * @throws ConnectionNotFoundException
140
     */
141
    public function getConnection(?string $name = null): Connection
142
    {
143
        if ($name === null) {
144
            $name = $this->default;
145
        }
146
147
        if (!$this->has($name)) {
148
            throw new ConnectionNotFoundException(
149
                sprintf('The connection [%s] does not exist', $name)
150
            );
151
        }
152
153
        return $this->connections[$name];
154
    }
155
156
    /**
157
     * Set the default connection to use
158
     * @param string $name
159
     * @return void
160
     * @throws ConnectionNotFoundException
161
     */
162
    public function setDefault(string $name): void
163
    {
164
        if (!$this->has($name)) {
165
            throw new ConnectionNotFoundException(
166
                sprintf('The connection [%s] does not exist', $name)
167
            );
168
        }
169
170
        $this->default = $name;
171
    }
172
173
174
    /**
175
     * Remove the given connection
176
     * @param string $name
177
     * @return void
178
     */
179
    public function remove(string $name): void
180
    {
181
        unset($this->connections[$name]);
182
    }
183
}
184