Completed
Push — master ( 77fbe2...eac339 )
by Jean-Christophe
02:27
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->pdoObject = new \PDO ( $this->dbType . ':host=' . $this->serverName . ';dbname=' . $this->dbName . ';charset=UTF8;port:' . $this->port, $this->user, $this->password, $this->options );
67
		$this->pdoObject->setAttribute ( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
68
	}
69
70
	/**
71
	 * Executes an SQL statement, returning a result set as a PDOStatement object
72
	 *
73
	 * @param string $sql
74
	 * @return \PDOStatement
75
	 */
76
	public function query($sql) {
77
		return $this->pdoObject->query ( $sql );
78
	}
79
80
	/**
81
	 *
82
	 * @param string $tableName
83
	 * @param string $condition
84
	 * @param array|string $fields
85
	 * @param boolean|null $useCache
86
	 * @return array
87
	 */
88
	public function prepareAndExecute($tableName, $condition, $fields, $useCache = NULL) {
89
		$cache = (DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true);
90
		$result = false;
91
		if ($cache) {
92
			$result = $this->cache->fetch ( $tableName, $condition );
93
		}
94
		if ($result === false) {
95
			$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...
96
			$statement = $this->getStatement ( "SELECT {$fields} FROM " . $tableName . $condition );
97
			$statement->execute ();
98
			$result = $statement->fetchAll ();
99
			$statement->closeCursor ();
100
			if ($cache) {
101
				$this->cache->store ( $tableName, $condition, $result );
102
			}
103
		}
104
		return $result;
105
	}
106
107
	/**
108
	 *
109
	 * @param string $sql
110
	 * @return \PDOStatement
111
	 */
112
	private function getStatement($sql) {
113
		if (! isset ( $this->statements [$sql] )) {
114
			$this->statements [$sql] = $this->pdoObject->prepare ( $sql );
115
			$this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC );
116
		}
117
		return $this->statements [$sql];
118
	}
119
120
	/**
121
	 * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE)
122
	 *
123
	 * @param string $sql
124
	 * @return int the number of rows that were modified or deleted by the SQL statement you issued
125
	 */
126
	public function execute($sql) {
127
		return $this->pdoObject->exec ( $sql );
128
	}
129
130
	public function getServerName() {
131
		return $this->serverName;
132
	}
133
134
	public function setServerName($serverName) {
135
		$this->serverName = $serverName;
136
	}
137
138
	/**
139
	 * Prepares a statement for execution and returns a statement object
140
	 *
141
	 * @param String $sql
142
	 * @return \PDOStatement
143
	 */
144
	public function prepareStatement($sql) {
145
		return $this->pdoObject->prepare ( $sql );
146
	}
147
148
	/**
149
	 * Sets $value to $parameter
150
	 *
151
	 * @param \PDOStatement $statement
152
	 * @param String $parameter
153
	 * @param mixed $value
154
	 * @return boolean
155
	 */
156
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
157
		return $statement->bindValue ( ":" . $parameter, $value );
158
	}
159
160
	/**
161
	 * Returns the last insert id
162
	 *
163
	 * @return integer
164
	 */
165
	public function lastInserId() {
166
		return $this->pdoObject->lastInsertId ();
167
	}
168
169
	public function getTablesName() {
170
		$sql = 'SHOW TABLES';
171
		$query = $this->pdoObject->query ( $sql );
172
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
173
	}
174
175
	/**
176
	 * Returns the number of records in $tableName that respects the condition passed as a parameter
177
	 *
178
	 * @param string $tableName
179
	 * @param string $condition
180
	 *        	Partie suivant le WHERE d'une instruction SQL
181
	 */
182
	public function count($tableName, $condition = '') {
183
		if ($condition != '')
184
			$condition = " WHERE " . $condition;
185
		return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn ();
186
	}
187
188
	public function isConnected() {
189
		return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO);
190
	}
191
192
	public function setDbType($dbType) {
193
		$this->dbType = $dbType;
194
		return $this;
195
	}
196
197
	public function getPort() {
198
		return $this->port;
199
	}
200
201
	public function getDbName() {
202
		return $this->dbName;
203
	}
204
205
	public function getUser() {
206
		return $this->user;
207
	}
208
209
	public function getPdoObject() {
210
		return $this->pdoObject;
211
	}
212
	
213
	public static function getAvailableDrivers(){
214
		return \PDO::getAvailableDrivers();
215
	}
216
}
217