Passed
Push — master ( caa32e...4f267a )
by Jean-Christophe
09:15
created

Database::__construct()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 21
ccs 15
cts 16
cp 0.9375
rs 9.3888
cc 5
nc 7
nop 10
crap 5.0061

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