Passed
Pull Request — master (#46)
by Jean-Christophe
07:05
created

Database::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Database implementation
5
 */
6
namespace Ubiquity\db;
7
8
use Ubiquity\exceptions\CacheException;
9
use Ubiquity\db\traits\DatabaseOperationsTrait;
10
use Ubiquity\exceptions\DBException;
11
use Ubiquity\db\traits\DatabaseTransactionsTrait;
12
13
/**
14
 * Ubiquity PDO database class.
15
 * Ubiquity\db$Database
16
 * This class is part of Ubiquity
17
 *
18
 * @author jcheron <[email protected]>
19
 * @version 1.0.4
20
 *
21
 */
22
class Database {
23
	use DatabaseOperationsTrait,DatabaseTransactionsTrait;
24
	private $dbType;
25
	private $serverName;
26
	private $port;
27
	private $dbName;
28
	private $user;
29
	private $password;
30
	private $cache;
31
	private $options;
32
33
	/**
34
	 *
35
	 * @var \PDO
36
	 */
37
	protected $pdoObject;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param string $dbName
43
	 * @param string $serverName
44
	 * @param string $port
45
	 * @param string $user
46
	 * @param string $password
47
	 * @param array $options
48
	 * @param boolean|string $cache
49
	 */
50 108
	public function __construct($dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) {
51 108
		$this->dbType = $dbType;
52 108
		$this->dbName = $dbName;
53 108
		$this->serverName = $serverName;
54 108
		$this->port = $port;
55 108
		$this->user = $user;
56 108
		$this->password = $password;
57 108
		if (isset ( $options ["quote"] ))
58 1
			SqlUtils::$quote = $options ["quote"];
59 108
		$this->options = $options;
60 108
		if ($cache !== false) {
61 17
			if (\is_callable ( $cache )) {
62 1
				$this->cache = $cache ();
63
			} else {
64 17
				if (\class_exists ( $cache )) {
65 17
					$this->cache = new $cache ();
66
				} else {
67 1
					throw new CacheException ( $cache . " is not a valid value for database cache" );
68
				}
69
			}
70
		}
71 108
	}
72
73
	/**
74
	 * Creates the PDO instance and realize a safe connection.
75
	 *
76
	 * @throws DBException
77
	 * @return boolean
78
	 */
79 97
	public function connect() {
80
		try {
81 97
			$this->_connect ();
82 97
			return true;
83 1
		} catch ( \PDOException $e ) {
84 1
			throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
85
		}
86
	}
87
88 98
	public function getDSN() {
89 98
		return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port;
90
	}
91
92
	/**
93
	 *
94
	 * @return string
95
	 * @codeCoverageIgnore
96
	 */
97
	public function getServerName() {
98
		return $this->serverName;
99
	}
100
101 3
	public function setServerName($serverName) {
102 3
		$this->serverName = $serverName;
103 3
	}
104
105 3
	public function setDbType($dbType) {
106 3
		$this->dbType = $dbType;
107 3
		return $this;
108
	}
109
110
	/**
111
	 *
112
	 * @return string
113
	 * @codeCoverageIgnore
114
	 */
115
	public function getPort() {
116
		return $this->port;
117
	}
118
119
	/**
120
	 *
121
	 * @return string
122
	 * @codeCoverageIgnore
123
	 */
124
	public function getDbName() {
125
		return $this->dbName;
126
	}
127
128
	/**
129
	 *
130
	 * @return string
131
	 * @codeCoverageIgnore
132
	 */
133
	public function getUser() {
134
		return $this->user;
135
	}
136
137 2
	public static function getAvailableDrivers() {
138 2
		return \PDO::getAvailableDrivers ();
139
	}
140
141
	/**
142
	 *
143
	 * @return mixed
144
	 * @codeCoverageIgnore
145
	 */
146
	public function getDbType() {
147
		return $this->dbType;
148
	}
149
150
	/**
151
	 *
152
	 * @return string
153
	 * @codeCoverageIgnore
154
	 */
155
	public function getPassword() {
156
		return $this->password;
157
	}
158
159
	/**
160
	 *
161
	 * @return array
162
	 * @codeCoverageIgnore
163
	 */
164
	public function getOptions() {
165
		return $this->options;
166
	}
167
168
	/**
169
	 *
170
	 * @param string $port
171
	 */
172 2
	public function setPort($port) {
173 2
		$this->port = $port;
174 2
	}
175
176
	/**
177
	 *
178
	 * @param string $dbName
179
	 */
180 1
	public function setDbName($dbName) {
181 1
		$this->dbName = $dbName;
182 1
	}
183
184
	/**
185
	 *
186
	 * @param string $user
187
	 */
188 2
	public function setUser($user) {
189 2
		$this->user = $user;
190 2
	}
191
192
	/**
193
	 *
194
	 * @param string $password
195
	 */
196 1
	public function setPassword($password) {
197 1
		$this->password = $password;
198 1
	}
199
200
	/**
201
	 *
202
	 * @param array $options
203
	 */
204 1
	public function setOptions($options) {
205 1
		$this->options = $options;
206 1
	}
207
208
	/**
209
	 * Closes the active pdo connection
210
	 */
211 22
	public function close() {
212 22
		$this->pdoObject = null;
213 22
	}
214
}
215