Test Failed
Branch master (e9d41f)
by Jean-Christophe
15:30
created

PDOWrapper   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Test Coverage

Coverage 70.54%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 46
eloc 76
c 7
b 1
f 0
dl 0
loc 199
ccs 79
cts 112
cp 0.7054
rs 8.72

37 Methods

Rating   Name   Duplication   Size   Complexity  
A _optPrepareAndExecute() 0 8 2
A quoteValue() 0 2 1
A query() 0 2 1
A executeStatement() 0 2 1
A queryColumn() 0 2 1
A savePoint() 0 2 1
A commit() 0 2 1
A releasePoint() 0 2 1
A rollbackPoint() 0 2 1
A beginTransaction() 0 2 1
A execute() 0 2 1
A inTransaction() 0 2 1
A getDriverMetaDatas() 0 10 3
A queryAll() 0 2 1
A rollBack() 0 2 1
A ping() 0 2 2
A fetchOne() 0 7 2
A prepareStatement() 0 2 1
A getDSN() 0 6 2
A getStatement() 0 4 1
A statementRowCount() 0 2 1
A fetchAllColumn() 0 7 2
A getTablesName() 0 2 1
A fetchAll() 0 7 2
A bindValueFromStatement() 0 2 1
A fetchColumn() 0 5 2
A nestable() 0 2 1
A __construct() 0 3 1
A connect() 0 3 1
A getAvailableDrivers() 0 2 1
A getRowNum() 0 2 1
A lastInsertId() 0 2 1
A toStringOperator() 0 2 1
A groupConcat() 0 2 1
A getPrimaryKeys() 0 2 1
A getFieldsInfos() 0 2 1
A getForeignKeys() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like PDOWrapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PDOWrapper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\db\providers\pdo;
4
5
use Ubiquity\db\providers\AbstractDbWrapper;
6
use Ubiquity\exceptions\DBException;
7
8
/**
9
 * Ubiquity\db\providers$PDOWrapper
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.3
14
 * @property \PDO $dbInstance
15
 *
16
 */
17
class PDOWrapper extends AbstractDbWrapper {
18
	protected static $savepointsDrivers = [ 'pgsql' => true,'mysql' => true,'sqlite' => true ];
19
	private static $quotes = [ 'mysql' => '`','sqlite' => '"','pgsql' => '"' ];
20
	protected $driversMetasClasses = [ 'mysql' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\MysqlDriverMetas','pgsql' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\PgsqlDriverMetas','sqlite' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\SqliteDriverMetas' ];
21
	protected $transactionLevel = 0;
22 80
	protected $dbType;
23 80
	protected $driverMetaDatas;
24 80
25 80
	/**
26
	 *
27
	 * @throws DBException
28
	 * @return \Ubiquity\db\providers\pdo\drivers\AbstractDriverMetaDatas
29
	 */
30
	protected function getDriverMetaDatas() {
31
		if (! isset ( $this->driverMetaDatas )) {
32
			if (isset ( $this->driversMetasClasses [$this->dbType] )) {
33
				$metaClass = $this->driversMetasClasses [$this->dbType];
34
				$this->driverMetaDatas = new $metaClass ( $this->dbInstance );
35
			} else {
36 10
				throw new DBException ( "{$this->dbType} driver is not yet implemented!" );
37 10
			}
38
		}
39
		return $this->driverMetaDatas;
40 23
	}
41 23
42 23
	public function __construct($dbType = 'mysql') {
43 23
		$this->quote = self::$quotes [$dbType] ?? '';
44
		$this->dbType = $dbType;
45 23
	}
46 23
47
	public function fetchAllColumn($statement, array $values = null, string $column = null) {
48
		$result = false;
49
		if ($statement->execute ( $values )) {
50
			$result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
51
		}
52
		$statement->closeCursor ();
53
		return $result;
54
	}
55
56
	public function lastInsertId($name = null) {
57
		return $this->dbInstance->lastInsertId ( $name );
58 2
	}
59 2
60
	public function fetchAll($statement, array $values = null, $mode = null) {
61
		$result = false;
62 7
		if ($statement->execute ( $values )) {
63 7
			$result = $statement->fetchAll ( $mode );
64
		}
65
		$statement->closeCursor ();
66
		return $result;
67
	}
68
69
	public function fetchOne($statement, array $values = null, $mode = null) {
70
		$result = false;
71
		if ($statement->execute ( $values )) {
72
			$result = $statement->fetch ( $mode );
73 68
		}
74 68
		$statement->closeCursor ();
75 68
		return $result;
76 68
	}
77
78
	public static function getAvailableDrivers() {
79
		return \PDO::getAvailableDrivers ();
80
	}
81
82
	public function prepareStatement(string $sql) {
83 75
		return $this->dbInstance->prepare ( $sql );
84 75
	}
85 75
86 75
	public function fetchColumn($statement, array $values = null, int $columnNumber = null) {
87
		if ($statement->execute ( $values )) {
88 75
			return $statement->fetchColumn ( $columnNumber );
89 75
		}
90 75
		return false;
91
	}
92
93 75
	public function getStatement($sql) {
94
		$st = $this->dbInstance->prepare ( $sql );
95
		$st->setFetchMode ( \PDO::FETCH_ASSOC );
96
		return $st;
97
	}
98
99
	public function execute($sql) {
100
		return $this->dbInstance->exec ( $sql );
101
	}
102
103
	public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) {
104
		$options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
105
		$this->dbInstance = new \PDO ( $this->getDSN ( $serverName, $port, $dbName, $dbType ), $user, $password, $options );
106
	}
107
108 7
	public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') {
109 7
		$charsetString = [ 'mysql' => 'charset=UTF8','pgsql' => 'options=\'--client_encoding=UTF8\'','sqlite' => '' ] [$dbType] ?? 'charset=UTF8';
110
		if ($dbType === 'sqlite') {
111
			return "sqlite:{$dbName}";
112
		}
113
		return $dbType . ":dbname={$dbName};host={$serverName};{$charsetString};port=" . $port;
114
	}
115
116 3
	public function bindValueFromStatement($statement, $parameter, $value) {
117 3
		return $statement->bindValue ( ":" . $parameter, $value );
118 3
	}
119
120
	public function query(string $sql) {
121
		return $this->dbInstance->query ( $sql );
122
	}
123
124
	public function queryAll(string $sql, int $fetchStyle = null) {
125
		return $this->dbInstance->query ( $sql )->fetchAll ( $fetchStyle );
126
	}
127
128
	public function queryColumn(string $sql, int $columnNumber = null) {
129 4
		return $this->dbInstance->query ( $sql )->fetchColumn ( $columnNumber );
130 4
	}
131
132
	public function executeStatement($statement, array $values = null) {
133 3
		return $statement->execute ( $values );
134 3
	}
135
136
	public function getTablesName() {
137 8
		return $this->getDriverMetaDatas ()->getTablesName ();
138 8
	}
139
140
	public function statementRowCount($statement) {
141 4
		return $statement->rowCount ();
142 4
	}
143 4
144
	public function inTransaction() {
145 2
		return $this->dbInstance->inTransaction ();
146 2
	}
147 2
148
	public function commit() {
149 2
		return $this->dbInstance->commit ();
150 2
	}
151 2
152
	public function rollBack() {
153 4
		return $this->dbInstance->rollBack ();
154 4
	}
155
156
	public function beginTransaction() {
157 4
		return $this->dbInstance->beginTransaction ();
158 4
	}
159
160
	public function savePoint($level) {
161 1
		$this->dbInstance->exec ( 'SAVEPOINT LEVEL' . $level );
162 1
	}
163 1
164 1
	public function releasePoint($level) {
165 1
		$this->dbInstance->exec ( 'RELEASE SAVEPOINT LEVEL' . $level );
166 1
	}
167
168 1
	public function rollbackPoint($level) {
169
		$this->dbInstance->exec ( 'ROLLBACK TO SAVEPOINT LEVEL' . $level );
170
	}
171 1
172 1
	public function nestable() {
173
		return isset ( self::$savepointsDrivers [$this->dbType] );
174
	}
175
176 1
	public function ping() {
177 1
		return ($this->dbInstance != null) && (1 === \intval ( $this->queryColumn ( 'SELECT 1', 0 ) ));
178 1
	}
179 1
180
	public function getPrimaryKeys($tableName) {
181
		return $this->getDriverMetaDatas ()->getPrimaryKeys ( $tableName );
182 1
	}
183 1
184 1
	public function getForeignKeys($tableName, $pkName, $dbName = null) {
185 1
		return $this->getDriverMetaDatas ()->getForeignKeys ( $tableName, $pkName, $dbName );
186 1
	}
187 1
188
	public function getFieldsInfos($tableName) {
189 1
		return $this->getDriverMetaDatas ()->getFieldsInfos ( $tableName );
190
	}
191
192 60
	public function _optPrepareAndExecute($sql, array $values = null) {
193 60
		$statement = $this->_getStatement ( $sql );
194 60
		$result = false;
195 60
		if ($statement->execute ( $values )) {
196 60
			$result = $statement->fetchAll ( \PDO::FETCH_ASSOC );
197
		}
198 60
		$statement->closeCursor ();
199 60
		return $result;
200
	}
201
202
	public function quoteValue($value, $type = 2) {
203
		return $this->dbInstance->quote ( $value, $type );
204
	}
205
206
	public function getRowNum(string $tableName, string $pkName, string $condition): int {
207
		return $this->getDriverMetaDatas ()->getRowNum ( $tableName, $pkName, $condition );
208
	}
209
210
	public function groupConcat(string $fields, string $separator): string {
211
		return $this->getDriverMetaDatas ()->groupConcat ( $fields, $separator );
212
	}
213
214
	public function toStringOperator() {
215
		return $this->getDriverMetaDatas ()->toStringOperator ();
216
	}
217
}
218