Completed
Push — master ( 9f7bd0...42ad31 )
by Jean-Christophe
01:49
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
 * @author jc
11
 * @version 1.0.0.3
12
 * @package db
13
 *
14
 */
15
class Database {
16
	private $dbType;
17
	private $serverName;
18
	private $port;
19
	private $dbName;
20
	private $user;
21
	private $password;
22
	private $pdoObject;
23
	private $statements=[ ];
24
	private $cache;
25
	private $options;
26
27
	/**
28
	 * Constructor
29
	 * @param string $dbName
30
	 * @param string $serverName
31
	 * @param string $port
32
	 * @param string $user
33
	 * @param string $password
34
	 * @param array $options
35
	 * @param boolean|string $cache
36
	 */
37
	public function __construct($dbType,$dbName, $serverName="localhost", $port="3306", $user="root", $password="", $options=[],$cache=false) {
38
		$this->dbType=$dbType;
39
		$this->dbName=$dbName;
40
		$this->serverName=$serverName;
41
		$this->port=$port;
42
		$this->user=$user;
43
		$this->password=$password;
44
		if(isset($options["quote"]))
45
			SqlUtils::$quote=$options["quote"];
46
		$this->options=$options;
47
		if ($cache !== false) {
48
			if(\is_callable($cache)){
49
				$this->cache=$cache();
50
			}else{
51
				if(\class_exists($cache)){
52
					$this->cache=new $cache();
53
				}else{
54
					throw new CacheException($cache." is not a valid value for database cache");
55
				}
56
			}
57
		}
58
	}
59
60
	/**
61
	 * Creates the PDO instance
62
	 */
63
	public function connect() {
64
		$this->pdoObject=new \PDO($this->dbType.':host=' . $this->serverName . ';dbname=' . $this->dbName . ';charset=UTF8;port:' . $this->port, $this->user, $this->password,$this->options);
65
		$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
66
	}
67
68
	/**
69
	 * Executes an SQL statement, returning a result set as a PDOStatement object
70
	 * @param string $sql
71
	 * @return \PDOStatement
72
	 */
73
	public function query($sql) {
74
		return $this->pdoObject->query($sql);
75
	}
76
77
	/**
78
	 * @param string $tableName
79
	 * @param string $condition
80
	 * @param array|string $fields
81
	 * @param boolean|null $useCache
82
	 * @return boolean|array
83
	 */
84
	public function prepareAndExecute($tableName, $condition,$fields,$useCache=NULL) {
85
		$cache=(DbCache::$active && $useCache !== false) || (!DbCache::$active && $useCache === true);
86
		$result=false;
87
		if ($cache) {
88
			$result=$this->cache->fetch($tableName, $condition);
89
		}
90
		if ($result === false) {
91
			$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...
92
			$statement=$this->getStatement("SELECT {$fields} FROM " . $tableName . $condition);
93
			$statement->execute();
94
			$result=$statement->fetchAll();
95
			$statement->closeCursor();
96
			if ($cache) {
97
				$this->cache->store($tableName, $condition, $result);
98
			}
99
		}
100
		return $result;
101
	}
102
103
	/**
104
	 * @param string $sql
105
	 * @return \PDOStatement
106
	 */
107
	private function getStatement($sql) {
108
		if (!isset($this->statements[$sql])) {
109
			$this->statements[$sql]=$this->pdoObject->prepare($sql);
110
			$this->statements[$sql]->setFetchMode(\PDO::FETCH_ASSOC);
111
		}
112
		return $this->statements[$sql];
113
	}
114
115
	/**
116
	 * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE)
117
	 * @param string $sql
118
	 * @return int the number of rows that were modified or deleted by the SQL statement you issued
119
	 */
120
	public function execute($sql) {
121
		return $this->pdoObject->exec($sql);
122
	}
123
124
	public function getServerName() {
125
		return $this->serverName;
126
	}
127
128
	public function setServerName($serverName) {
129
		$this->serverName=$serverName;
130
	}
131
132
	/**
133
	 * Prepares a statement for execution and returns a statement object
134
	 * @param String $sql
135
	 * @return \PDOStatement
136
	 */
137
	public function prepareStatement($sql) {
138
		return $this->pdoObject->prepare($sql);
139
	}
140
141
	/**
142
	 * Sets $value to $parameter
143
	 * @param \PDOStatement $statement
144
	 * @param String $parameter
145
	 * @param mixed $value
146
	 * @return boolean
147
	 */
148
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
149
		return $statement->bindValue(":" . $parameter, $value);
150
	}
151
152
	/**
153
	 * Returns the last insert id
154
	 * @return integer
155
	 */
156
	public function lastInserId() {
157
		return $this->pdoObject->lastInsertId();
158
	}
159
160
	public function getTablesName() {
161
		$sql='SHOW TABLES';
162
		$query=$this->pdoObject->query($sql);
163
		return $query->fetchAll(\PDO::FETCH_COLUMN);
164
	}
165
166
	/**
167
	 * Returns the number of records in $tableName that respects the condition passed as a parameter
168
	 * @param string $tableName
169
	 * @param string $condition Partie suivant le WHERE d'une instruction SQL
170
	 */
171
	public function count($tableName, $condition='') {
172
		if ($condition != '')
173
			$condition=" WHERE " . $condition;
174
			return $this->query("SELECT COUNT(*) FROM " . $tableName . $condition)->fetchColumn();
175
	}
176
177
	public function isConnected(){
178
		return ($this->pdoObject!==null && $this->pdoObject instanceof \PDO);
179
	}
180
181
	public function setDbType($dbType) {
182
		$this->dbType=$dbType;
183
		return $this;
184
	}
185
186
	public function getPort() {
187
		return $this->port;
188
	}
189
190
	public function getDbName() {
191
		return $this->dbName;
192
	}
193
194
	public function getUser() {
195
		return $this->user;
196
	}
197
198
	public function getPdoObject() {
199
		return $this->pdoObject;
200
	}
201
202
}
203