Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
DatabasePool | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
1 / 1 |
add | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
get | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
6 | |||
remove | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\DataStorage\Database |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\DataStorage\Database; |
16 | |
17 | use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; |
18 | use phpOMS\DataStorage\Database\Connection\ConnectionFactory; |
19 | use phpOMS\DataStorage\Database\Connection\NullConnection; |
20 | use phpOMS\DataStorage\DataStorageConnectionInterface; |
21 | use phpOMS\DataStorage\DataStoragePoolInterface; |
22 | |
23 | /** |
24 | * Database pool handler. |
25 | * |
26 | * @package phpOMS\DataStorage\Database |
27 | * @license OMS License 2.0 |
28 | * @link https://jingga.app |
29 | * @since 1.0.0 |
30 | */ |
31 | final class DatabasePool implements DataStoragePoolInterface |
32 | { |
33 | /** |
34 | * Databases. |
35 | * |
36 | * @var ConnectionAbstract[] |
37 | * @since 1.0.0 |
38 | */ |
39 | private array $pool = []; |
40 | |
41 | /** |
42 | * Add database. |
43 | * |
44 | * @param string $key Database key |
45 | * @param DataStorageConnectionInterface $db Database |
46 | * |
47 | * @return bool |
48 | * |
49 | * @since 1.0.0 |
50 | */ |
51 | public function add(string $key, DataStorageConnectionInterface $db) : bool |
52 | { |
53 | if (isset($this->pool[$key])) { |
54 | return false; |
55 | } |
56 | |
57 | $this->pool[$key] = $db; |
58 | |
59 | return true; |
60 | } |
61 | |
62 | /** |
63 | * Get database. |
64 | * |
65 | * @param string $key Database key |
66 | * |
67 | * @return ConnectionAbstract |
68 | * |
69 | * @since 1.0.0 |
70 | */ |
71 | public function get(string $key = '') : ConnectionAbstract |
72 | { |
73 | if ((!empty($key) && !isset($this->pool[$key])) || empty($this->pool)) { |
74 | return new NullConnection(); |
75 | } |
76 | |
77 | $con = empty($key) ? \reset($this->pool) : $this->pool[$key]; |
78 | if ($con->getStatus() !== DatabaseStatus::OK) { |
79 | $con->connect(); |
80 | } |
81 | |
82 | return $con; |
83 | } |
84 | |
85 | /** |
86 | * Remove database. |
87 | * |
88 | * @param string $key Database key |
89 | * |
90 | * @return bool |
91 | * |
92 | * @since 1.0.0 |
93 | */ |
94 | public function remove(string $key) : bool |
95 | { |
96 | if (!isset($this->pool[$key])) { |
97 | return false; |
98 | } |
99 | |
100 | unset($this->pool[$key]); |
101 | |
102 | return true; |
103 | } |
104 | |
105 | /** |
106 | * Create database. |
107 | * |
108 | * @param string $key Database key |
109 | * @param array{db:string, database:string}|array{db:string, host:string, port:int, login:string, password:string, database:string} $config Database config data |
110 | * |
111 | * @return bool |
112 | * |
113 | * @since 1.0.0 |
114 | */ |
115 | public function create(string $key, array $config) : bool |
116 | { |
117 | if (isset($this->pool[$key])) { |
118 | return false; |
119 | } |
120 | |
121 | $this->pool[$key] = ConnectionFactory::create($config); |
122 | |
123 | return true; |
124 | } |
125 | } |