| Total Complexity | 47 |
| Total Lines | 239 |
| Duplicated Lines | 0 % |
| Coverage | 87.1% |
| Changes | 8 | ||
| Bugs | 0 | Features | 3 |
Complex classes like DatabaseOperationsTrait 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 DatabaseOperationsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | trait DatabaseOperationsTrait { |
||
| 20 | |||
| 21 | abstract public function getDSN(); |
||
| 22 | |||
| 23 | public function getDbObject() { |
||
| 24 | 3 | return $this->wrapperObject->getDbInstance(); |
|
| 25 | 3 | } |
|
| 26 | |||
| 27 | public function _connect() { |
||
| 28 | 94 | $this->wrapperObject->connect($this->dbType, $this->dbName, $this->serverName, $this->port, $this->user, $this->password, $this->options); |
|
| 29 | 94 | } |
|
| 30 | 94 | ||
| 31 | /** |
||
| 32 | * Executes an SQL statement, returning a result set as a statement object |
||
| 33 | * |
||
| 34 | * @param string $sql |
||
| 35 | * @return object|boolean |
||
| 36 | */ |
||
| 37 | public function query($sql) { |
||
| 38 | 1 | return $this->wrapperObject->query($sql); |
|
| 39 | 1 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * |
||
| 43 | * @param string $tableName |
||
| 44 | * @param string $condition |
||
| 45 | * @param array|string $fields |
||
| 46 | * @param array $parameters |
||
| 47 | * @param boolean|null $useCache |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = false, $one = false) { |
||
| 51 | 64 | $cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true)); |
|
| 52 | 64 | $result = false; |
|
| 53 | 64 | if ($cache) { |
|
| 54 | 64 | $result = $this->getCacheValue($tableName, $condition, $parameters, $cKey); |
|
| 55 | 18 | } |
|
| 56 | 18 | if ($result === false) { |
|
| 57 | 15 | $quote = SqlUtils::$quote; |
|
| 58 | $result = $this->wrapperObject->_optPrepareAndExecute("SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters, $one); |
||
| 59 | if ($cache) { |
||
| 60 | 18 | $this->cache->store($tableName, $cKey, $result); |
|
|
1 ignored issue
–
show
|
|||
| 61 | 17 | } |
|
| 62 | 1 | } |
|
| 63 | return $result; |
||
| 64 | } |
||
| 65 | |||
| 66 | 64 | private function getCacheValue($tableName, $condition, $parameters, &$cKey) { |
|
| 67 | 62 | $cKey = $condition; |
|
| 68 | 62 | if (\is_array($parameters)) { |
|
| 69 | 62 | $cKey .= \implode(',', $parameters); |
|
| 70 | 15 | } |
|
| 71 | try { |
||
| 72 | $result = $this->cache->fetch($tableName, $cKey); |
||
| 73 | 64 | Logger::info("Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters); |
|
| 74 | } catch (\Exception $e) { |
||
| 75 | throw new CacheException("Cache is not created in Database constructor"); |
||
| 76 | 24 | } |
|
| 77 | 24 | return $result; |
|
| 78 | } |
||
| 79 | |||
| 80 | public function _optExecuteAndFetch($statement, $tableName, $condition, $parameters = null, $useCache = false, $one = false) { |
||
| 81 | $cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true)); |
||
| 82 | $result = false; |
||
| 83 | if ($cache) { |
||
| 84 | 1 | $result = $this->getCacheValue($tableName, $condition, $parameters, $cKey); |
|
| 85 | 1 | } |
|
| 86 | if ($result === false) { |
||
| 87 | if ($one) { |
||
| 88 | 22 | $result = $this->wrapperObject->_optExecuteAndFetchOne($statement, $parameters); |
|
| 89 | 22 | } else { |
|
| 90 | 22 | $result = $this->wrapperObject->_optExecuteAndFetch($statement, $parameters); |
|
| 91 | 22 | } |
|
| 92 | 22 | if ($cache) { |
|
| 93 | $this->cache->store($tableName, $cKey, $result); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | return $result; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function _optExecuteAndFetchNoCache($statement, $parameters = null, $one = false) { |
||
| 100 | return $this->wrapperObject->_optExecuteAndFetch($statement, $parameters, $one); |
||
|
1 ignored issue
–
show
|
|||
| 101 | } |
||
| 102 | |||
| 103 | public function getDaoPreparedStatement($tableName, $condition, $fields) { |
||
| 104 | $quote = SqlUtils::$quote; |
||
| 105 | return $this->wrapperObject->prepareStatement("SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}"); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function prepareAndExecuteNoCache($tableName, $condition, $fields, $parameters = null) { |
||
| 109 | $quote = SqlUtils::$quote; |
||
| 110 | return $this->wrapperObject->_optPrepareAndExecute("SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters); |
||
| 111 | 12 | } |
|
| 112 | 12 | ||
| 113 | public function storeCache() { |
||
| 114 | $this->cache->storeDeferred(); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function prepareAndFetchAll($sql, $parameters = null, $mode = null) { |
||
| 119 | } |
||
| 120 | |||
| 121 | public function prepareAndFetchOne($sql, $parameters = null, $mode = null) { |
||
| 122 | return $this->wrapperObject->fetchOne($this->wrapperObject->_getStatement($sql), $parameters, $mode); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) { |
||
| 126 | return $this->wrapperObject->fetchAllColumn($this->wrapperObject->_getStatement($sql), $parameters, $column); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = null) { |
||
| 130 | $statement = $this->wrapperObject->_getStatement($sql); |
||
| 131 | if ($statement->execute($parameters)) { |
||
| 132 | 2 | Logger::info("Database", $sql, "prepareAndFetchColumn", $parameters); |
|
| 133 | 2 | return $statement->fetchColumn($columnNumber); |
|
| 134 | } |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * |
||
| 140 | * @param string $sql |
||
| 141 | * @return object statement |
||
| 142 | 9 | */ |
|
| 143 | 9 | private function getStatement($sql) { |
|
| 144 | return $this->wrapperObject->_getStatement($sql); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * |
||
| 149 | * @param string $sql |
||
| 150 | * @return object statement |
||
| 151 | */ |
||
| 152 | public function getUpdateStatement($sql) { |
||
| 153 | return $this->wrapperObject->_getStatement($sql); |
||
| 154 | 1 | } |
|
| 155 | 1 | ||
| 156 | /** |
||
| 157 | * Prepares a statement and execute a query for update (INSERT, UPDATE, DELETE...) |
||
| 158 | * |
||
| 159 | * @param string $sql |
||
| 160 | * @param array|null $parameters |
||
| 161 | * @return boolean |
||
| 162 | */ |
||
| 163 | 11 | public function prepareAndExecuteUpdate($sql, $parameters = null) { |
|
| 164 | 11 | return $this->getUpdateStatement($sql)->execute($parameters); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
||
| 169 | * |
||
| 170 | * @param string $sql |
||
| 171 | * @return int the number of rows that were modified or deleted by the SQL statement you issued |
||
| 172 | */ |
||
| 173 | 1 | public function execute($sql) { |
|
| 174 | 1 | return $this->wrapperObject->execute($sql); |
|
| 175 | 1 | } |
|
| 176 | 1 | ||
| 177 | /** |
||
| 178 | * Prepares a statement for execution and returns a statement object |
||
| 179 | 1 | * |
|
| 180 | 1 | * @param String $sql |
|
| 181 | * @return object|boolean |
||
| 182 | */ |
||
| 183 | 1 | public function prepareStatement($sql) { |
|
| 184 | 1 | return $this->wrapperObject->prepareStatement($sql); |
|
| 185 | } |
||
| 186 | |||
| 187 | 21 | /** |
|
| 188 | 21 | * Prepares and returns a statement for execution and gives it a name. |
|
| 189 | * |
||
| 190 | * @param |
||
| 191 | 21 | * $name |
|
| 192 | 21 | * @param String $sql |
|
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function prepareNamedStatement(string $name, string $sql) { |
||
| 196 | return $this->wrapperObject->prepareNamedStatement($name, $sql); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the statement corresponding to the name. |
||
| 201 | * |
||
| 202 | * @param string $name |
||
| 203 | * @param ?string $sql |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function getNamedStatement(string $name, ?string $sql = null) { |
||
| 207 | return $this->wrapperObject->getNamedStatement($name, $sql); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Sets $value to $parameter |
||
| 212 | * |
||
| 213 | * @param mixed $statement |
||
| 214 | * @param String $parameter |
||
| 215 | * @param mixed $value |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | public function bindValueFromStatement($statement, $parameter, $value) { |
||
| 219 | return $this->wrapperObject->bindValueFromStatement($statement, $parameter, $value); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the last insert id |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function lastInserId($name = null) { |
||
| 228 | return $this->wrapperObject->lastInsertId($name); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the number of records in $tableName matching with the condition passed as a parameter |
||
| 233 | * |
||
| 234 | * @param string $tableName |
||
| 235 | * @param string $condition |
||
| 236 | * Part following the WHERE of an SQL statement |
||
| 237 | */ |
||
| 238 | public function count($tableName, $condition = '') { |
||
| 239 | if ($condition != '') |
||
| 240 | $condition = " WHERE " . $condition; |
||
| 241 | return $this->wrapperObject->queryColumn("SELECT COUNT(*) FROM " . $tableName . $condition); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function queryColumn($query, $columnNumber = null) { |
||
| 245 | return $this->wrapperObject->queryColumn($query, $columnNumber); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function fetchAll($query, $mode = null) { |
||
| 250 | } |
||
| 251 | |||
| 252 | public function isConnected() { |
||
| 253 | return ($this->wrapperObject !== null && $this->ping()); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function ping() { |
||
| 257 | return ($this->wrapperObject && $this->wrapperObject->ping()); |
||
| 258 | } |
||
| 259 | } |
||
| 260 |