Passed
Push — master ( 9e15fe...1d956f )
by Jean-Christophe
09:32
created

PDOWrapper   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 71.96%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 40
eloc 75
c 5
b 0
f 1
dl 0
loc 180
ccs 77
cts 107
cp 0.7196
rs 9.2

32 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKeys() 0 8 2
A query() 0 2 1
A executeStatement() 0 2 1
A queryColumn() 0 2 1
A savePoint() 0 2 1
A _optPrepareAndExecute() 0 8 2
A commit() 0 2 1
A getFieldsInfos() 0 8 2
A rollbackPoint() 0 2 1
A releasePoint() 0 2 1
A lastInsertId() 0 2 1
A beginTransaction() 0 2 1
A execute() 0 2 1
A inTransaction() 0 2 1
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 2 1
A getStatement() 0 4 1
A statementRowCount() 0 2 1
A fetchAllColumn() 0 7 2
A getTablesName() 0 3 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 getForeignKeys() 0 9 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
7
/**
8
 * Ubiquity\db\providers$PDOWrapper
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 * @property \PDO $dbInstance
14
 *
15
 */
16
class PDOWrapper extends AbstractDbWrapper {
17
	protected static $savepointsDrivers = [ 'pgsql' => true,'mysql' => true,'sqlite' => true ];
18
	private static $quotes = [ 'mysql' => '`','sqlite' => '"','pgsql' => '"' ];
19
	protected $transactionLevel = 0;
20
	protected $dbType;
21
22 75
	public function __construct($dbType = 'mysql') {
23 75
		$this->quote = self::$quotes [$dbType] ?? '';
24 75
		$this->dbType = $dbType;
25 75
	}
26
27
	public function fetchAllColumn($statement, array $values = null, string $column = null) {
28
		$result = false;
29
		if ($statement->execute ( $values )) {
30
			$result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
31
		}
32
		$statement->closeCursor ();
33
		return $result;
34
	}
35
36 10
	public function lastInsertId() {
37 10
		return $this->dbInstance->lastInsertId ();
38
	}
39
40 23
	public function fetchAll($statement, array $values = null, $mode = null) {
41 23
		$result = false;
42 23
		if ($statement->execute ( $values )) {
43 23
			$result = $statement->fetchAll ( $mode );
44
		}
45 23
		$statement->closeCursor ();
46 23
		return $result;
47
	}
48
49
	public function fetchOne($statement, array $values = null, $mode = null) {
50
		$result = false;
51
		if ($statement->execute ( $values )) {
52
			$result = $statement->fetch ( $mode );
53
		}
54
		$statement->closeCursor ();
55
		return $result;
56
	}
57
58 2
	public static function getAvailableDrivers() {
59 2
		return \PDO::getAvailableDrivers ();
60
	}
61
62 7
	public function prepareStatement(string $sql) {
63 7
		return $this->dbInstance->prepare ( $sql );
64
	}
65
66
	public function fetchColumn($statement, array $values = null, int $columnNumber = null) {
67
		if ($statement->execute ( $values )) {
68
			return $statement->fetchColumn ( $columnNumber );
69
		}
70
		return false;
71
	}
72
73 67
	public function getStatement($sql) {
74 67
		$st = $this->dbInstance->prepare ( $sql );
75 67
		$st->setFetchMode ( \PDO::FETCH_ASSOC );
76 67
		return $st;
77
	}
78
79
	public function execute($sql) {
80
		return $this->dbInstance->exec ( $sql );
81
	}
82
83 70
	public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) {
84 70
		$options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
85 70
		$this->dbInstance = new \PDO ( $this->getDSN ( $serverName, $port, $dbName, $dbType ), $user, $password, $options );
86 70
	}
87
88 70
	public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') {
89 70
		return $dbType . ':dbname=' . $dbName . ';host=' . $serverName . ';charset=UTF8;port=' . $port;
90
	}
91
92
	public function bindValueFromStatement($statement, $parameter, $value) {
93
		return $statement->bindValue ( ":" . $parameter, $value );
94
	}
95
96
	public function query(string $sql) {
97
		return $this->dbInstance->query ( $sql );
98
	}
99
100
	public function queryAll(string $sql, int $fetchStyle = null) {
101
		return $this->dbInstance->query ( $sql )->fetchAll ( $fetchStyle );
102
	}
103
104 5
	public function queryColumn(string $sql, int $columnNumber = null) {
105 5
		return $this->dbInstance->query ( $sql )->fetchColumn ( $columnNumber );
106
	}
107
108
	public function executeStatement($statement, array $values = null) {
109
		return $statement->execute ( $values );
110
	}
111
112 3
	public function getTablesName() {
113 3
		$query = $this->dbInstance->query ( 'SHOW TABLES' );
114 3
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
115
	}
116
117
	public function statementRowCount($statement) {
118
		return $statement->rowCount ();
119
	}
120
121
	public function inTransaction() {
122
		return $this->dbInstance->inTransaction ();
123
	}
124
125 4
	public function commit() {
126 4
		return $this->dbInstance->commit ();
127
	}
128
129 3
	public function rollBack() {
130 3
		return $this->dbInstance->rollBack ();
131
	}
132
133 8
	public function beginTransaction() {
134 8
		return $this->dbInstance->beginTransaction ();
135
	}
136
137 4
	public function savePoint($level) {
138 4
		$this->dbInstance->exec ( 'SAVEPOINT LEVEL' . $level );
139 4
	}
140
141 2
	public function releasePoint($level) {
142 2
		$this->dbInstance->exec ( 'RELEASE SAVEPOINT LEVEL' . $level );
143 2
	}
144
145 2
	public function rollbackPoint($level) {
146 2
		$this->dbInstance->exec ( 'ROLLBACK TO SAVEPOINT LEVEL' . $level );
147 2
	}
148
149 4
	public function nestable() {
150 4
		return isset ( self::$savepointsDrivers [$this->dbType] );
151
	}
152
153 2
	public function ping() {
154 2
		return ($this->dbInstance != null) && (1 === \intval ( $this->queryColumn ( 'SELECT 1', 0 ) ));
155
	}
156
157 1
	public function getPrimaryKeys($tableName) {
158 1
		$fieldkeys = array ();
159 1
		$recordset = $this->dbInstance->query ( "SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'" );
160 1
		$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
161 1
		foreach ( $keys as $key ) {
162 1
			$fieldkeys [] = $key ['Column_name'];
163
		}
164 1
		return $fieldkeys;
165
	}
166
167 1
	public function getForeignKeys($tableName, $pkName, $dbName = null) {
168 1
		$recordset = $this->dbInstance->query ( "SELECT *
169
												FROM
170
												 information_schema.KEY_COLUMN_USAGE
171
												WHERE
172 1
												 REFERENCED_TABLE_NAME = '" . $tableName . "'
173 1
												 AND REFERENCED_COLUMN_NAME = '" . $pkName . "'
174 1
												 AND TABLE_SCHEMA = '" . $dbName . "';" );
175 1
		return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
176
	}
177
178 1
	public function getFieldsInfos($tableName) {
179 1
		$fieldsInfos = array ();
180 1
		$recordset = $this->dbInstance->query ( "SHOW COLUMNS FROM `{$tableName}`" );
181 1
		$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
182 1
		foreach ( $fields as $field ) {
183 1
			$fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ];
184
		}
185 1
		return $fieldsInfos;
186
	}
187
188 59
	public function _optPrepareAndExecute($sql, array $values = null) {
189 59
		$statement = $this->_getStatement ( $sql );
190 59
		$result = false;
191 59
		if ($statement->execute ( $values )) {
192 59
			$result = $statement->fetchAll ( \PDO::FETCH_ASSOC );
193
		}
194 59
		$statement->closeCursor ();
195 59
		return $result;
196
	}
197
}