Passed
Push — master ( f461b3...0c0438 )
by Jean-Christophe
10:20
created

Database::__construct()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 21
ccs 15
cts 16
cp 0.9375
rs 9.0777
cc 6
nc 7
nop 10
crap 6.0087

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Database implementation
5
 */
6
namespace Ubiquity\db;
7
8
use Ubiquity\exceptions\CacheException;
9
use Ubiquity\db\traits\DatabaseOperationsTrait;
10
use Ubiquity\exceptions\DBException;
11
use Ubiquity\db\traits\DatabaseTransactionsTrait;
12
use Ubiquity\controllers\Startup;
13
use Ubiquity\db\traits\DatabaseMetadatas;
14
use Ubiquity\cache\database\DbCache;
15
16
/**
17
 * Ubiquity Generic database class.
18
 * Ubiquity\db$Database
19
 * This class is part of Ubiquity
20
 *
21
 * @author jcheron <[email protected]>
22
 * @version 1.1.4
23
 *
24
 */
25
class Database {
26
	use DatabaseOperationsTrait,DatabaseTransactionsTrait,DatabaseMetadatas;
27
	public static $wrappers = [ 'pdo' => \Ubiquity\db\providers\pdo\PDOWrapper::class,'tarantool' => '\Ubiquity\db\providers\tarantool\TarantoolWrapper','mysqli' => '\Ubiquity\db\providers\mysqli\MysqliWrapper','swoole' => '\Ubiquity\db\providers\swoole\SwooleWrapper' ];
28
	private $dbType;
29
	private $serverName;
30
	private $port;
31
	private $dbName;
32
	private $user;
33
	private $password;
34
	private $cache;
35
	private $options;
36
	public $quote;
37
38
	/**
39
	 *
40
	 * @var \Ubiquity\db\providers\AbstractDbWrapper
41
	 */
42
	protected $wrapperObject;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param string $dbWrapperClass
48
	 * @param string $dbName
49
	 * @param string $serverName
50
	 * @param string $port
51
	 * @param string $user
52
	 * @param string $password
53
	 * @param array $options
54
	 * @param boolean|string $cache
55
	 * @param mixed $pool
56
	 */
57 113
	public function __construct($dbWrapperClass, $dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [ ], $cache = false, $pool = null) {
58 113
		$this->setDbWrapperClass ( $dbWrapperClass, $dbType );
59 113
		$this->dbName = $dbName;
60 113
		$this->serverName = $serverName;
61 113
		$this->port = $port;
62 113
		$this->user = $user;
63 113
		$this->password = $password;
64 113
		$this->options = $options;
65 113
		if ($cache !== false) {
66 18
			if ($cache instanceof \Closure) {
67 1
				$this->cache = $cache ();
68
			} else {
69 18
				if (\class_exists ( $cache )) {
70 18
					$this->cache = new $cache ();
71
				} else {
72 1
					throw new CacheException ( $cache . " is not a valid value for database cache" );
73
				}
74
			}
75
		}
76 113
		if ($pool && (\method_exists ( $this->wrapperObject, 'pool' ))) {
77
			$this->wrapperObject->setPool ( $pool );
78
		}
79 113
	}
80
81 113
	private function setDbWrapperClass($dbWrapperClass, $dbType) {
82 113
		$this->wrapperObject = new $dbWrapperClass ( $this->dbType = $dbType );
83 113
	}
84
85
	/**
86
	 * Creates the Db instance and realize a safe connection.
87
	 *
88
	 * @throws DBException
89
	 * @return boolean
90
	 */
91 103
	public function connect() {
92
		try {
93 103
			$this->_connect ();
94 103
			$this->quote = $this->wrapperObject->quote;
95 103
			return true;
96 1
		} catch ( \Exception $e ) {
97 1
			throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
98
		}
99
	}
100
101 1
	public function getDSN() {
102 1
		return $this->wrapperObject->getDSN ( $this->serverName, $this->port, $this->dbName, $this->dbType );
103
	}
104
105
	/**
106
	 *
107
	 * @return string
108
	 * @codeCoverageIgnore
109
	 */
110
	public function getServerName() {
111
		return $this->serverName;
112
	}
113
114 3
	public function setServerName($serverName) {
115 3
		$this->serverName = $serverName;
116 3
	}
117
118 3
	public function setDbType($dbType) {
119 3
		$this->dbType = $dbType;
120 3
		return $this;
121
	}
122
123
	/**
124
	 *
125
	 * @return string
126
	 * @codeCoverageIgnore
127
	 */
128
	public function getPort() {
129
		return $this->port;
130
	}
131
132
	/**
133
	 *
134
	 * @return string
135
	 * @codeCoverageIgnore
136
	 */
137
	public function getDbName() {
138
		return $this->dbName;
139
	}
140
141
	/**
142
	 *
143
	 * @return string
144
	 * @codeCoverageIgnore
145
	 */
146
	public function getUser() {
147
		return $this->user;
148
	}
149
150 3
	public static function getAvailableDrivers($dbWrapperClass = \Ubiquity\db\providers\pdo\PDOWrapper::class) {
151 3
		return \call_user_func ( $dbWrapperClass . '::getAvailableDrivers' );
152
	}
153
154
	/**
155
	 *
156
	 * @return mixed
157
	 * @codeCoverageIgnore
158
	 */
159
	public function getDbType() {
160
		return $this->dbType;
161
	}
162
163
	/**
164
	 *
165
	 * @return string
166
	 * @codeCoverageIgnore
167
	 */
168
	public function getPassword() {
169
		return $this->password;
170
	}
171
172
	/**
173
	 *
174
	 * @return array
175
	 * @codeCoverageIgnore
176
	 */
177
	public function getOptions() {
178
		return $this->options;
179
	}
180
181
	/**
182
	 *
183
	 * @param string $port
184
	 */
185 2
	public function setPort($port) {
186 2
		$this->port = $port;
187 2
	}
188
189
	/**
190
	 *
191
	 * @param string $dbName
192
	 */
193 1
	public function setDbName($dbName) {
194 1
		$this->dbName = $dbName;
195 1
	}
196
197
	/**
198
	 *
199
	 * @param string $user
200
	 */
201 2
	public function setUser($user) {
202 2
		$this->user = $user;
203 2
	}
204
205
	/**
206
	 *
207
	 * @param string $password
208
	 */
209 1
	public function setPassword($password) {
210 1
		$this->password = $password;
211 1
	}
212
213
	/**
214
	 *
215
	 * @param array $options
216
	 */
217 1
	public function setOptions($options) {
218 1
		$this->options = $options;
219 1
	}
220
221
	/**
222
	 * Closes the active connection
223
	 */
224 38
	public function close() {
225 38
		$this->wrapperObject->close ();
226 38
	}
227
228
	/**
229
	 * Starts and returns a database instance corresponding to an offset in config
230
	 *
231
	 * @param string $offset
232
	 * @param array $config Ubiquity config file content
233
	 * @return \Ubiquity\db\Database|NULL
234
	 */
235
	public static function start(string $offset = null, ?array $config = null): ?self {
236
		$config ??= Startup::$config;
237
		$db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ?? [ ]);
238
		if ($db ['dbName'] !== '') {
239
			$database = new Database ( $db ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class, $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false);
240
			$database->connect ();
241
			return $database;
242
		}
243
		return null;
244
	}
245
246 2
	public function quoteValue($value, $type = 2) {
247 2
		return $this->wrapperObject->quoteValue ( ( string ) $value, $type );
248
	}
249
250 1
	public function getUpdateFieldsKeyAndValues($keyAndValues, $fields) {
251 1
		$ret = array ();
252 1
		foreach ( $fields as $field ) {
253 1
			$ret [] = $this->quote . $field . $this->quote . ' = ' . $this->quoteValue ( $keyAndValues [$field] );
254
		}
255 1
		return \implode ( ',', $ret );
256
	}
257
258 1
	public function getInsertValues($keyAndValues) {
259 1
		$ret = array ();
260 1
		foreach ( $keyAndValues as $value ) {
261 1
			$ret [] = $this->quoteValue ( $value );
262
		}
263 1
		return \implode ( ',', $ret );
264
	}
265
266 2
	public function getCondition(array $keyValues, $separator = ' AND ') {
267 2
		$retArray = array ();
268 2
		foreach ( $keyValues as $key => $value ) {
269 2
			$retArray [] = $this->quote . $key . $this->quote . " = " . $this->quoteValue ( $value );
270
		}
271 2
		return \implode ( $separator, $retArray );
272
	}
273
274
	/**
275
	 * For databases with Connection pool (retrieve a new dbInstance from pool wrapper)
276
	 */
277
	public function pool() {
278
		return $this->wrapperObject->pool ();
279
	}
280
281
	/**
282
	 * For databases with Connection pool (put a dbInstance in pool wrapper)
283
	 */
284
	public function freePool($db) {
285
		$this->wrapperObject->freePool ( $db );
286
	}
287
288
	public function setPool($pool) {
289
		$this->wrapperObject->setPool ( $pool );
290
	}
291
292 2
	public static function getAvailableWrappers() {
293 2
		$wrappers = [ ];
294 2
		foreach ( self::$wrappers as $k => $wrapper ) {
295 2
			if (\class_exists ( $wrapper, true )) {
296 2
				$wrappers [$k] = $wrapper;
297
			}
298
		}
299 2
		return $wrappers;
300
	}
301
302 24
	public function getSpecificSQL($key, ?array $params = null) {
303 24
		switch ($key) {
304 24
			case 'groupconcat' :
305 23
				return $this->wrapperObject->groupConcat ( $params [0], $params [1] ?? ',');
306 24
			case 'tostring' :
307 24
				return $this->wrapperObject->toStringOperator ();
308
		}
309
	}
310
311
	public function setCacheInstance(DbCache $cache) {
312
		$this->cache = $cache;
313
	}
314
315
	public function getCacheInstance() {
316
		return $this->cache;
317
	}
318
}
319