Passed
Push — master ( e59108...b06663 )
by Jean-Christophe
17:44 queued 14s
created

DAO::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
ccs 3
cts 6
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 10
crap 2.5

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
namespace Ubiquity\orm;
4
5
use Ubiquity\db\Database;
6
use Ubiquity\log\Logger;
7
use Ubiquity\db\SqlUtils;
8
use Ubiquity\orm\traits\DAOUpdatesTrait;
9
use Ubiquity\orm\traits\DAORelationsTrait;
10
use Ubiquity\orm\parser\ConditionParser;
11
use Ubiquity\orm\traits\DAOUQueries;
12
use Ubiquity\orm\traits\DAOCoreTrait;
13
use Ubiquity\orm\traits\DAORelationsPrepareTrait;
14
use Ubiquity\exceptions\DAOException;
15
use Ubiquity\orm\traits\DAORelationsAssignmentsTrait;
16
use Ubiquity\orm\traits\DAOTransactionsTrait;
17
use Ubiquity\controllers\Startup;
18
use Ubiquity\cache\CacheManager;
19
use Ubiquity\orm\traits\DAOPooling;
20
use Ubiquity\orm\traits\DAOBulkUpdatesTrait;
21
use Ubiquity\orm\traits\DAOPreparedTrait;
22
use Ubiquity\cache\dao\AbstractDAOCache;
23
24
/**
25
 * Gateway class between database and object model.
26
 * This class is part of Ubiquity
27
 *
28
 * @author jcheron <[email protected]>
29
 * @version 1.2.5
30
 *
31
 */
32
class DAO {
33
	use DAOCoreTrait,DAOUpdatesTrait,DAORelationsTrait,DAORelationsPrepareTrait,DAORelationsAssignmentsTrait,
34
	DAOUQueries,DAOTransactionsTrait,DAOPooling,DAOBulkUpdatesTrait,DAOPreparedTrait;
35
36
	/**
37
	 *
38
	 * @var Database
39
	 */
40
	public static $db;
41
	public static $useTransformers = false;
42
	public static $transformerOp = 'transform';
43
	private static $conditionParsers = [ ];
44
	protected static $modelsDatabase = [ ];
45
46
	/**
47
	 *
48
	 * @var AbstractDAOCache
49
	 */
50
	protected static $cache;
51
52 149
	public static function getDb($model) {
53 149
		return self::getDatabase ( self::$modelsDatabase [$model] ?? 'default');
54
	}
55
56
	/**
57
	 * Returns an array of $className objects from the database
58
	 *
59
	 * @param string $className class name of the model to load
60
	 * @param string $condition Part following the WHERE of an SQL statement
61
	 * @param boolean|array $included if true, loads associate members with associations, if array, example : ['client.*','commands']
62
	 * @param array|null $parameters
63
	 * @param boolean $useCache use the active cache if true
64
	 * @return array
65
	 */
66 56
	public static function getAll($className, $condition = '', $included = true, $parameters = null, $useCache = NULL) {
67 56
		$db = self::getDb ( $className );
68 56
		return static::_getAll ( $db, $className, new ConditionParser ( $condition, null, $parameters ), $included, $useCache );
69
	}
70
71
	/**
72
	 * Returns an array of $className objects loaded by id from the database
73
	 *
74
	 * @param string $className class name of the model to load
75
	 * @param array $keyValues
76
	 * @param boolean|array $included if true, loads associate members with associations, if array, example : ['client.*','commands']
77
	 * @param string $condition additional condition
78
	 * @param boolean $useCache use the active cache if true
79
	 * @return array
80
	 */
81
	public static function getAllByIds($className, $keyValues = [ ], $included = true, $condition = '', $useCache = NULL) {
82
		$db = self::getDb ( $className );
83
		$key = OrmUtils::getFirstKey ( $className );
84
		$countK = \count ( $keyValues );
85
		if ($countK > 0) {
86
			$nCondition = $key . ' IN (' . \str_repeat ( '?,', $countK - 1 ) . '?)';
87
			if ($condition != null) {
88
				$nCondition .= ' AND ' . $condition;
89
			}
90
			return static::_getAll ( $db, $className, new ConditionParser ( $nCondition, null, $keyValues ), $included, $useCache );
91
		}
92
		return [];
93
		
94
	}
95
96 6
	public static function paginate($className, $page = 1, $rowsPerPage = 20, $condition = null, $included = true) {
97 6
		return self::getAll ( $className, ($condition ?? '1=1') . ' LIMIT ' . $rowsPerPage . ' OFFSET ' . (($page - 1) * $rowsPerPage), $included );
98
	}
99
100 5
	public static function getRownum($className, $ids) {
101 5
		$tableName = OrmUtils::getTableName ( $className );
102 5
		$db = self::getDb ( $className );
103 5
		$quote = $db->quote;
104 5
		self::parseKey ( $ids, $className, $quote );
105 5
		$condition = SqlUtils::getCondition ( $ids, $className );
106 5
		$keyFields = OrmUtils::getKeyFields ( $className );
107 5
		if (\is_array ( $keyFields )) {
108 5
			$keys = \implode ( ',', $keyFields );
109
		} else {
110
			$keys = '1';
111
		}
112 5
		return $db->getRowNum ( $tableName, $keys, $condition );
113
	}
114
115
	/**
116
	 * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter
117
	 *
118
	 * @param string $className complete classname of the model to load
119
	 * @param string $condition Part following the WHERE of an SQL statement
120
	 * @param array|null $parameters The query parameters
121
	 * @return int|false count of objects
122
	 */
123 31
	public static function count($className, $condition = '', $parameters = null) {
124 31
		$tableName = OrmUtils::getTableName ( $className );
125 31
		if ($condition != '') {
126 10
			$condition = SqlUtils::checkWhere($condition);
127
		}
128 31
		$db = self::getDb ( $className );
129 31
		$quote = $db->quote;
130 31
		return $db->prepareAndFetchColumn ( 'SELECT COUNT(*) FROM ' . $quote . $tableName . $quote . $condition, $parameters );
131
	}
132
133
	/**
134
	 * Tests the existence of objects of $className from the database respecting the condition possibly passed as parameter
135
	 *
136
	 * @param string $className complete classname of the model to load
137
	 * @param string $condition Part following the WHERE of an SQL statement
138
	 * @param array|null $parameters The query parameters
139
	 * @return boolean
140
	 */
141 2
	public static function exists($className, $condition = '', $parameters = null) {
142 2
		$tableName = OrmUtils::getTableName ( $className );
143 2
		if ($condition != '') {
144 2
			$condition = SqlUtils::checkWhere($condition);
145
		}
146 2
		$db = self::getDb ( $className );
147 2
		$quote = $db->quote;
148 2
		return (1 == $db->prepareAndFetchColumn ( "SELECT EXISTS(SELECT 1 FROM {$quote}{$tableName}{$quote}{$condition})", $parameters ));
149
	}
150
151
	/**
152
	 * Returns an instance of $className from the database, from $keyvalues values of the primary key or with a condition
153
	 *
154
	 * @param String $className complete classname of the model to load
155
	 * @param array|string $condition condition or primary key values
156
	 * @param boolean|array $included if true, charges associate members with association
157
	 * @param array|null $parameters the request parameters
158
	 * @param boolean|null $useCache use cache if true
159
	 * @return object the instance loaded or null if not found
160
	 */
161 28
	public static function getOne($className, $condition, $included = true, $parameters = null, $useCache = NULL) {
162 28
		$db = self::getDb ( $className );
163 28
		$conditionParser = new ConditionParser ();
164 28
		if (! isset ( $parameters )) {
165 27
			$conditionParser->addKeyValues ( $condition, $className );
166 3
		} elseif (! is_array ( $condition )) {
167 3
			$conditionParser->setCondition ( $condition );
168 3
			$conditionParser->setParams ( $parameters );
169
		} else {
170
			throw new DAOException ( "The \$condition parameter should not be an array if \$parameters is not null" );
171
		}
172 28
		return static::_getOne ( $db, $className, $conditionParser, $included, $useCache );
173
	}
174
175
	/**
176
	 * Returns an instance of $className from the database, from $keyvalues values of the primary key
177
	 *
178
	 * @param String $className complete classname of the model to load
179
	 * @param array|string $keyValues primary key values or condition
180
	 * @param boolean|array $included if true, charges associate members with association
181
	 * @param boolean|null $useCache use cache if true
182
	 * @return object the instance loaded or null if not found
183
	 */
184 43
	public static function getById($className, $keyValues, $included = true, $useCache = NULL) {
185 43
		return static::_getOne ( self::getDatabase ( self::$modelsDatabase [$className] ?? 'default'), $className, self::getConditionParser ( $className, $keyValues ), $included, $useCache );
186
	}
187
188 43
	protected static function getConditionParser($className, $keyValues): ConditionParser {
189 43
		if (! isset ( self::$conditionParsers [$className] )) {
190 15
			$conditionParser = new ConditionParser ();
191 15
			$conditionParser->addKeyValues ( $keyValues, $className );
192 15
			self::$conditionParsers [$className] = $conditionParser;
193
		} else {
194 31
			self::$conditionParsers [$className]->setKeyValues ( $keyValues );
195
		}
196 43
		return self::$conditionParsers [$className];
197
	}
198
199
	/**
200
	 * Establishes the connection to the database using the past parameters
201
	 *
202
	 * @param string $offset
203
	 * @param string $wrapper
204
	 * @param string $dbType
205
	 * @param string $dbName
206
	 * @param string $serverName
207
	 * @param string $port
208
	 * @param string $user
209
	 * @param string $password
210
	 * @param array $options
211
	 * @param boolean $cache
212
	 */
213 151
	public static function connect($offset, $wrapper, $dbType, $dbName, $serverName = '127.0.0.1', $port = '3306', $user = 'root', $password = '', $options = [ ], $cache = false) {
214 151
		self::$db [$offset] = new Database ( $wrapper, $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache, self::$pool );
215
		try {
216 151
			self::$db [$offset]->connect ();
217
		} catch ( \Exception $e ) {
218
			Logger::error ( "DAO", $e->getMessage () );
219
			throw new DAOException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
220
		}
221 151
	}
222
223
	/**
224
	 * Establishes the connection to the database using the $config array
225
	 *
226
	 * @param array $config the config array (Startup::getConfig())
227
	 */
228 27
	public static function startDatabase(&$config, $offset = null) {
229 27
		$db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ['default'] ?? $config ['database']);
230 27
		if ($db ['dbName'] !== '') {
231 27
			self::connect ( $offset ?? 'default', $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);
232
		}
233 27
	}
234
235 221
	public static function getDbOffset(&$config, $offset = null) {
236 221
		return $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ['default'] ?? $config ['database']);
237
	}
238
239
	/**
240
	 * Returns true if the connection to the database is established
241
	 *
242
	 * @return boolean
243
	 */
244 8
	public static function isConnected($offset = 'default') {
245 8
		$db = self::$db [$offset] ?? false;
246 8
		return $db && ($db instanceof Database) && $db->isConnected ();
247
	}
248
249
	/**
250
	 * Sets the transformer operation
251
	 *
252
	 * @param string $op
253
	 */
254
	public static function setTransformerOp($op) {
255
		self::$transformerOp = $op;
256
	}
257
258
	/**
259
	 * Closes the active pdo connection to the database
260
	 */
261 102
	public static function closeDb($offset = 'default') {
262 102
		$db = self::$db [$offset] ?? false;
263 102
		if ($db !== false) {
264 101
			$db->close ();
265
		}
266 102
	}
267
268
	/**
269
	 * Defines the database connection to use for $model class
270
	 *
271
	 * @param string $model a model class
272
	 * @param string $database a database connection defined in config.php
273
	 */
274 10
	public static function setModelDatabase($model, $database = 'default') {
275 10
		self::$modelsDatabase [$model] = $database;
276 10
	}
277
278
	/**
279
	 * Defines the database connections to use for models classes
280
	 *
281
	 * @param array $modelsDatabase
282
	 */
283
	public static function setModelsDatabases($modelsDatabase) {
284
		self::$modelsDatabase = $modelsDatabase;
285
	}
286
287
	/**
288
	 * Returns the database instance defined at $offset key in config
289
	 *
290
	 * @param string $offset
291
	 * @return \Ubiquity\db\Database
292
	 */
293 155
	public static function getDatabase($offset = 'default') {
294 155
		if (! isset ( self::$db [$offset] )) {
295 19
			self::startDatabase ( Startup::$config, $offset );
296
		}
297 155
		SqlUtils::$quote = self::$db [$offset]->quote;
298 155
		return self::$db [$offset];
299
	}
300
301 6
	public static function getDatabases() {
302 6
		$config = Startup::getConfig ();
303 6
		if (isset ( $config ['database'] )) {
304 6
			if (isset ( $config ['database'] ['dbName'] )) {
305
				return [ 'default' ];
306
			} else {
307 6
				return \array_keys ( $config ['database'] );
308
			}
309
		}
310
		return [ ];
311
	}
312
313
	public static function updateDatabaseParams(array &$config, array $parameters, $offset = 'default') {
314
		if ($offset === 'default') {
315
			if (isset ( $config ['database'] [$offset] )) {
316
				foreach ( $parameters as $k => $param ) {
317
					$config ['database'] [$offset] [$k] = $param;
318
				}
319
			} else {
320
				foreach ( $parameters as $k => $param ) {
321
					$config ['database'] [$k] = $param;
322
				}
323
			}
324
		} else {
325
			if (isset ( $config ['database'] [$offset] )) {
326
				foreach ( $parameters as $k => $param ) {
327
					$config ['database'] [$offset] [$k] = $param;
328
				}
329
			}
330
		}
331
	}
332
333 72
	public static function start() {
334 72
		self::$modelsDatabase = CacheManager::getModelsDatabases ();
335 72
	}
336
337
	public static function getDbCacheInstance($model) {
338
		$db = static::$db [self::$modelsDatabase [$model] ?? 'default'];
339
		return $db->getCacheInstance ();
340
	}
341
342 2
	public static function warmupCache($className, $condition = '', $included = false, $parameters = [ ]) {
343 2
		$objects = self::getAll ( $className, $condition, $included, $parameters );
344 2
		foreach ( $objects as $o ) {
345 2
			self::$cache->store ( $className, OrmUtils::getKeyValues ( $o ), $o );
346
		}
347 2
		self::$cache->optimize ();
348 2
		$offset = self::$modelsDatabase [$className] ?? 'default';
349 2
		$db = self::$db [$offset];
350 2
		$db->close ();
351 2
		unset ( self::$db [$offset] );
352 2
	}
353
354 2
	public static function setCache(AbstractDAOCache $cache) {
355 2
		self::$cache = $cache;
356 2
	}
357
358
	/**
359
	 *
360
	 * @return \Ubiquity\cache\dao\AbstractDAOCache
361
	 */
362 64
	public static function getCache() {
363 64
		return static::$cache;
364
	}
365
366
	/**
367
	 * @return array
368
	 */
369
	public static function getModelsDatabase(): array {
370
		return self::$modelsDatabase;
371
	}
372
}
373