Completed
Push — master ( eb0184...954492 )
by Jean-Christophe
01:46
created

Database::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 18
nc 8
nop 8

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\db;
4
5
use Ubiquity\cache\database\DbCache;
6
use Ubiquity\exceptions\CacheException;
7
8
/**
9
 * PDO Database class
10
 *
11
 * @author jc
12
 * @version 1.0.0.3
13
 * @package db
14
 *
15
 */
16
class Database {
17
	private $dbType;
18
	private $serverName;
19
	private $port;
20
	private $dbName;
21
	private $user;
22
	private $password;
23
	private $pdoObject;
24
	private $statements = [ ];
25
	private $cache;
26
	private $options;
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param string $dbName
32
	 * @param string $serverName
33
	 * @param string $port
34
	 * @param string $user
35
	 * @param string $password
36
	 * @param array $options
37
	 * @param boolean|string $cache
38
	 */
39
	public function __construct($dbType, $dbName, $serverName = "localhost", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) {
40
		$this->dbType = $dbType;
41
		$this->dbName = $dbName;
42
		$this->serverName = $serverName;
43
		$this->port = $port;
44
		$this->user = $user;
45
		$this->password = $password;
46
		if (isset ( $options ["quote"] ))
47
			SqlUtils::$quote = $options ["quote"];
48
		$this->options = $options;
49
		if ($cache !== false) {
50
			if (\is_callable ( $cache )) {
51
				$this->cache = $cache ();
52
			} else {
53
				if (\class_exists ( $cache )) {
54
					$this->cache = new $cache ();
55
				} else {
56
					throw new CacheException ( $cache . " is not a valid value for database cache" );
57
				}
58
			}
59
		}
60
	}
61
62
	/**
63
	 * Creates the PDO instance
64
	 */
65
	public function connect() {
66
		$this->options[\PDO::ATTR_ERRMODE]=\PDO::ERRMODE_EXCEPTION;
67
		$this->pdoObject = new \PDO ( $this->getDSN(), $this->user, $this->password, $this->options );
68
	}
69
	
70
	public function getDSN(){
71
		return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port;
72
	}
73
74
	/**
75
	 * Executes an SQL statement, returning a result set as a PDOStatement object
76
	 *
77
	 * @param string $sql
78
	 * @return \PDOStatement
79
	 */
80
	public function query($sql) {
81
		return $this->pdoObject->query ( $sql );
82
	}
83
84
	/**
85
	 *
86
	 * @param string $tableName
87
	 * @param string $condition
88
	 * @param array|string $fields
89
	 * @param boolean|null $useCache
90
	 * @return array
91
	 */
92
	public function prepareAndExecute($tableName, $condition, $fields, $useCache = NULL) {
93
		$cache = (DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true);
94
		$result = false;
95
		if ($cache) {
96
			$result = $this->cache->fetch ( $tableName, $condition );
97
		}
98
		if ($result === false) {
99
			$fields = SqlUtils::getFieldList ( $fields, $tableName );
0 ignored issues
show
Documentation introduced by
$tableName is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
			$statement = $this->getStatement ( "SELECT {$fields} FROM " . $tableName . $condition );
101
			$statement->execute ();
102
			$result = $statement->fetchAll ();
103
			$statement->closeCursor ();
104
			if ($cache) {
105
				$this->cache->store ( $tableName, $condition, $result );
106
			}
107
		}
108
		return $result;
109
	}
110
111
	/**
112
	 *
113
	 * @param string $sql
114
	 * @return \PDOStatement
115
	 */
116
	private function getStatement($sql) {
117
		if (! isset ( $this->statements [$sql] )) {
118
			$this->statements [$sql] = $this->pdoObject->prepare ( $sql );
119
			$this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC );
120
		}
121
		return $this->statements [$sql];
122
	}
123
124
	/**
125
	 * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE)
126
	 *
127
	 * @param string $sql
128
	 * @return int the number of rows that were modified or deleted by the SQL statement you issued
129
	 */
130
	public function execute($sql) {
131
		return $this->pdoObject->exec ( $sql );
132
	}
133
134
	public function getServerName() {
135
		return $this->serverName;
136
	}
137
138
	public function setServerName($serverName) {
139
		$this->serverName = $serverName;
140
	}
141
142
	/**
143
	 * Prepares a statement for execution and returns a statement object
144
	 *
145
	 * @param String $sql
146
	 * @return \PDOStatement
147
	 */
148
	public function prepareStatement($sql) {
149
		return $this->pdoObject->prepare ( $sql );
150
	}
151
152
	/**
153
	 * Sets $value to $parameter
154
	 *
155
	 * @param \PDOStatement $statement
156
	 * @param String $parameter
157
	 * @param mixed $value
158
	 * @return boolean
159
	 */
160
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
161
		return $statement->bindValue ( ":" . $parameter, $value );
162
	}
163
164
	/**
165
	 * Returns the last insert id
166
	 *
167
	 * @return integer
168
	 */
169
	public function lastInserId() {
170
		return $this->pdoObject->lastInsertId ();
171
	}
172
173
	public function getTablesName() {
174
		$sql = 'SHOW TABLES';
175
		$query = $this->pdoObject->query ( $sql );
176
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
177
	}
178
179
	/**
180
	 * Returns the number of records in $tableName that respects the condition passed as a parameter
181
	 *
182
	 * @param string $tableName
183
	 * @param string $condition
184
	 *        	Partie suivant le WHERE d'une instruction SQL
185
	 */
186
	public function count($tableName, $condition = '') {
187
		if ($condition != '')
188
			$condition = " WHERE " . $condition;
189
		return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn ();
190
	}
191
192
	public function isConnected() {
193
		return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->ping());
194
	}
195
196
	public function setDbType($dbType) {
197
		$this->dbType = $dbType;
198
		return $this;
199
	}
200
	
201
	public function ping() {
202
		return (1 === intval($this->pdoObject->query('SELECT 1')->fetchColumn(0)));
203
	}
204
205
	public function getPort() {
206
		return $this->port;
207
	}
208
209
	public function getDbName() {
210
		return $this->dbName;
211
	}
212
213
	public function getUser() {
214
		return $this->user;
215
	}
216
217
	public function getPdoObject() {
218
		return $this->pdoObject;
219
	}
220
	
221
	public static function getAvailableDrivers(){
222
		return \PDO::getAvailableDrivers();
223
	}
224
}
225