1 | <?php |
||
2 | /** |
||
3 | * DronePHP (http://www.dronephp.com) |
||
4 | * |
||
5 | * @link http://github.com/Pleets/DronePHP |
||
6 | * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
||
7 | * @license http://www.dronephp.com/license |
||
8 | * @author Darío Rivera <[email protected]> |
||
9 | */ |
||
10 | |||
11 | namespace Drone\Db\TableGateway; |
||
12 | |||
13 | use Drone\Db\Driver\DriverFactory; |
||
14 | use Drone\Db\Entity; |
||
15 | |||
16 | /** |
||
17 | * AbstractTableGateway class |
||
18 | * |
||
19 | * This class stores statically different connections from a TableGateway |
||
20 | */ |
||
21 | abstract class AbstractTableGateway |
||
22 | { |
||
23 | /** |
||
24 | * Entity instance |
||
25 | * |
||
26 | * @var Entity |
||
27 | */ |
||
28 | protected $entity; |
||
29 | |||
30 | /** |
||
31 | * Driver collector |
||
32 | * |
||
33 | * @var AbstractDriver[] |
||
34 | */ |
||
35 | private static $drivers; |
||
36 | |||
37 | /** |
||
38 | * Current connection |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $currentConnection; |
||
43 | |||
44 | /** |
||
45 | * Returns the entity |
||
46 | * |
||
47 | * @return Entity |
||
48 | */ |
||
49 | 1 | public function getEntity() |
|
50 | { |
||
51 | 1 | return $this->entity; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Returns all registered drivers |
||
56 | * |
||
57 | * @return AbstractDriver[] |
||
58 | */ |
||
59 | public static function getDrivers() |
||
60 | { |
||
61 | return self::$drivers; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns the current connection identifier |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getCurrentConnection() |
||
70 | { |
||
71 | return $this->currentConnection; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Constructor |
||
76 | * |
||
77 | * @param Entity $entity |
||
78 | * @param array|string $connection |
||
79 | * |
||
80 | * @throws \RuntimeException |
||
81 | */ |
||
82 | 22 | public function __construct(Entity $entity, $connection) |
|
83 | { |
||
84 | 22 | $this->entity = $entity; |
|
85 | |||
86 | 22 | if (is_string($connection)) { |
|
87 | 18 | $this->currentConnection = $connection; |
|
88 | 18 | $this->getDriver($connection); |
|
89 | 4 | } elseif (is_array($connection)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
90 | 4 | $identifier = key($connection); |
|
91 | 4 | $connection_options = $connection[$identifier]; |
|
92 | |||
93 | 4 | $this->currentConnection = $identifier; |
|
94 | |||
95 | 4 | if (!array_key_exists('driver', $connection_options)) { |
|
96 | throw new \RuntimeException("The database driver key has not been declared"); |
||
97 | } |
||
98 | |||
99 | 4 | if (!isset(self::$drivers[$identifier])) { |
|
100 | 3 | self::$drivers[$identifier] = DriverFactory::create($connection_options); |
|
101 | } else { |
||
102 | 4 | throw new \RuntimeException("The database connection already exists"); |
|
103 | } |
||
104 | } else { |
||
105 | throw new \InvalidArgumentException("Invalid type given. Array or string expected"); |
||
106 | } |
||
107 | 21 | } |
|
108 | |||
109 | /** |
||
110 | * Returns the a particular connection |
||
111 | * |
||
112 | * @param string |
||
113 | * @param mixed $identifier |
||
114 | * |
||
115 | * @throws \RuntimeException |
||
116 | * @return AbstractDriver |
||
0 ignored issues
–
show
The type
Drone\Db\TableGateway\AbstractDriver was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
117 | * |
||
118 | */ |
||
119 | 19 | public static function getDriver($identifier) |
|
120 | { |
||
121 | 19 | if (!array_key_exists($identifier, self::$drivers)) { |
|
122 | 1 | throw new \RuntimeException("The database connection does not exists"); |
|
123 | } |
||
124 | |||
125 | 19 | return self::$drivers[$identifier]; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Returns true if the connection exists |
||
130 | * |
||
131 | * @param string |
||
132 | * @param mixed $identifier |
||
133 | * |
||
134 | * @throws \RuntimeException |
||
135 | * @return boolean |
||
136 | * |
||
137 | */ |
||
138 | public static function hasDriver($identifier) |
||
139 | { |
||
140 | if (!array_key_exists($identifier, self::$drivers)) { |
||
141 | return false; |
||
142 | } |
||
143 | |||
144 | return true; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns the current connection driver |
||
149 | * |
||
150 | * @throws \RuntimeException |
||
151 | * @return AbstractDriver |
||
152 | * |
||
153 | */ |
||
154 | 17 | public function getCurrentDriver() |
|
155 | { |
||
156 | 17 | return self::$drivers[$this->currentConnection]; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Alias for getCurrentDriver() |
||
161 | * |
||
162 | * @throws \RuntimeException |
||
163 | * @return AbstractDriver |
||
164 | * |
||
165 | */ |
||
166 | 17 | public function getDb() |
|
167 | { |
||
168 | 17 | return $this->getCurrentDriver(); |
|
169 | } |
||
170 | } |
||
171 |