Completed
Push — master ( 499e80...f97bb0 )
by Jean-Christophe
01:49
created

Database::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 16
nc 4
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
 * Classe d'accès aux Bases de données encapsulant un objet PDO
10
 * @author heron
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
	 * Constructeur
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
		$this->options=$options;
45
		if ($cache !== false) {
46
			if(\is_callable($cache)){
47
				$this->cache=$cache();
48
			}else{
49
				if(\class_exists($cache)){
50
					$this->cache=new $cache();
51
				}else{
52
					throw new CacheException($cache." is not a valid value for database cache");
53
				}
54
			}
55
		}
56
	}
57
58
	/**
59
	 * Réalise la connexion à la base de données
60
	 */
61
	public function connect() {
62
		$this->pdoObject=new \PDO($this->dbType.':host=' . $this->serverName . ';dbname=' . $this->dbName . ';charset=UTF8;port:' . $this->port, $this->user, $this->password,$this->options);
63
		$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
64
	}
65
66
	/**
67
	 * Exécute l'instruction SQL passée en paramètre et retourne un statement
68
	 * @param string $sql
69
	 * @return PDOStatement
70
	 */
71
	public function query($sql) {
72
		return $this->pdoObject->query($sql);
73
	}
74
75
	public function prepareAndExecute($tableName, $condition,$fields,$useCache=NULL) {
76
		$cache=(DbCache::$active && $useCache !== false) || (!DbCache::$active && $useCache === true);
77
		$result=false;
78
		if ($cache) {
79
			$result=$this->cache->fetch($tableName, $condition);
80
		}
81
		if ($result === false) {
82
			$fields=SqlUtils::getFieldList($fields);
83
			$statement=$this->getStatement("SELECT {$fields} FROM " . $tableName . $condition);
84
			$statement->execute();
85
			$result=$statement->fetchAll();
86
			$statement->closeCursor();
87
			if ($cache) {
88
				$this->cache->store($tableName, $condition, $result);
89
			}
90
		}
91
		return $result;
92
	}
93
94
	private function getStatement($sql) {
95
		if (!isset($this->statements[$sql])) {
96
			$this->statements[$sql]=$this->pdoObject->prepare($sql);
97
			$this->statements[$sql]->setFetchMode(\PDO::FETCH_ASSOC);
98
		}
99
		return $this->statements[$sql];
100
	}
101
102
	/**
103
	 * Exécute l'instruction sql $sql de mise à jour (INSERT, UPDATE ou DELETE)
104
	 * @return le nombre d'enregistrements affectés
105
	 */
106
	public function execute($sql) {
107
		return $this->pdoObject->exec($sql);
108
	}
109
110
	public function getServerName() {
111
		return $this->serverName;
112
	}
113
114
	public function setServerName($serverName) {
115
		$this->serverName=$serverName;
116
	}
117
118
	/**
119
	 * Prépare l'instruction $sql pour son exécution
120
	 * @param String $sql
121
	 * @return PDOStatement
122
	 */
123
	public function prepareStatement($sql) {
124
		return $this->pdoObject->prepare($sql);
125
	}
126
127
	/**
128
	 * Sets $value to $parameter
129
	 * @param PDOStatement $statement
130
	 * @param String $parameter
131
	 * @param mixed $value
132
	 * @return boolean
133
	 */
134
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
135
		return $statement->bindValue(":" . $parameter, $value);
136
	}
137
138
	/**
139
	 * Returns the last insert id
140
	 * @return integer
141
	 */
142
	public function lastInserId() {
143
		return $this->pdoObject->lastInsertId();
144
	}
145
146
	public function getTablesName() {
147
		$sql='SHOW TABLES';
148
		$query=$this->pdoObject->query($sql);
149
		return $query->fetchAll(\PDO::FETCH_COLUMN);
150
	}
151
152
	public function isConnected(){
153
		return ($this->pdoObject!==null && $this->pdoObject instanceof \PDO);
154
	}
155
156
	public function setDbType($dbType) {
157
		$this->dbType=$dbType;
158
		return $this;
159
	}
160
161
	public function getPort() {
162
		return $this->port;
163
	}
164
165
	public function getDbName() {
166
		return $this->dbName;
167
	}
168
169
	public function getUser() {
170
		return $this->user;
171
	}
172
173
	public function getPdoObject() {
174
		return $this->pdoObject;
175
	}
176
177
}
178