Passed
Push — master ( 4211b3...817f31 )
by Jean-Christophe
05:14
created

Database::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 8
nop 8
dl 0
loc 18
ccs 16
cts 16
cp 1
crap 5
rs 9.3888
c 0
b 0
f 0

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\exceptions\CacheException;
6
use Ubiquity\db\traits\DatabaseOperationsTrait;
7
8
/**
9
 * PDO database class
10
 * Ubiquity\db$Database
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.3
15
 *
16
 */
17
class Database {
18
	use DatabaseOperationsTrait;
19
	private $dbType;
20
	private $serverName;
21
	private $port;
22
	private $dbName;
23
	private $user;
24
	private $password;
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 61
	public function __construct($dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) {
40 61
		$this->dbType = $dbType;
41 61
		$this->dbName = $dbName;
42 61
		$this->serverName = $serverName;
43 61
		$this->port = $port;
44 61
		$this->user = $user;
45 61
		$this->password = $password;
46 61
		if (isset ( $options ["quote"] ))
47 1
			SqlUtils::$quote = $options ["quote"];
48 61
		$this->options = $options;
49 61
		if ($cache !== false) {
50 1
			if (\is_callable ( $cache )) {
51 1
				$this->cache = $cache ();
52
			} else {
53 1
				if (\class_exists ( $cache )) {
54 1
					$this->cache = new $cache ();
55
				} else {
56 1
					throw new CacheException ( $cache . " is not a valid value for database cache" );
57
				}
58
			}
59
		}
60 61
	}
61
62
	/**
63
	 * Creates the PDO instance and realize a safe connection
64
	 *
65
	 * @return boolean true if connection is established
66
	 */
67 50
	public function connect() {
68
		try {
69 50
			$this->_connect ();
70 50
			return true;
71 1
		} catch ( \PDOException $e ) {
72 1
			echo $e->getMessage ();
73 1
			return false;
74
		}
75
	}
76
77 51
	public function getDSN() {
78 51
		return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port;
79
	}
80
81
	/**
82
	 *
83
	 * @return string
84
	 * @codeCoverageIgnore
85
	 */
86
	public function getServerName() {
87
		return $this->serverName;
88
	}
89
90 3
	public function setServerName($serverName) {
91 3
		$this->serverName = $serverName;
92 3
	}
93
94 3
	public function setDbType($dbType) {
95 3
		$this->dbType = $dbType;
96 3
		return $this;
97
	}
98
99
	/**
100
	 *
101
	 * @return string
102
	 * @codeCoverageIgnore
103
	 */
104
	public function getPort() {
105
		return $this->port;
106
	}
107
108
	/**
109
	 *
110
	 * @return string
111
	 * @codeCoverageIgnore
112
	 */
113
	public function getDbName() {
114
		return $this->dbName;
115
	}
116
117
	/**
118
	 *
119
	 * @return string
120
	 * @codeCoverageIgnore
121
	 */
122
	public function getUser() {
123
		return $this->user;
124
	}
125
126 2
	public static function getAvailableDrivers() {
127 2
		return \PDO::getAvailableDrivers ();
128
	}
129
130
	/**
131
	 *
132
	 * @return mixed
133
	 * @codeCoverageIgnore
134
	 */
135
	public function getDbType() {
136
		return $this->dbType;
137
	}
138
139
	/**
140
	 *
141
	 * @return string
142
	 * @codeCoverageIgnore
143
	 */
144
	public function getPassword() {
145
		return $this->password;
146
	}
147
148
	/**
149
	 *
150
	 * @return array
151
	 * @codeCoverageIgnore
152
	 */
153
	public function getOptions() {
154
		return $this->options;
155
	}
156
157
	/**
158
	 *
159
	 * @param string $port
160
	 */
161 2
	public function setPort($port) {
162 2
		$this->port = $port;
163 2
	}
164
165
	/**
166
	 *
167
	 * @param string $dbName
168
	 */
169 1
	public function setDbName($dbName) {
170 1
		$this->dbName = $dbName;
171 1
	}
172
173
	/**
174
	 *
175
	 * @param string $user
176
	 */
177 2
	public function setUser($user) {
178 2
		$this->user = $user;
179 2
	}
180
181
	/**
182
	 *
183
	 * @param string $password
184
	 */
185 1
	public function setPassword($password) {
186 1
		$this->password = $password;
187 1
	}
188
189
	/**
190
	 *
191
	 * @param array $options
192
	 */
193 1
	public function setOptions($options) {
194 1
		$this->options = $options;
195 1
	}
196
}
197