| Total Complexity | 41 |
| Total Lines | 408 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 2 |
Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Database extends BaseClass { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The DatabaseConnection instance |
||
| 35 | * @var object |
||
| 36 | */ |
||
| 37 | private $connection = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The number of rows returned by the last query |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | private $numRows = 0; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The last insert id for the primary key column that have auto increment or sequence |
||
| 47 | * @var mixed |
||
| 48 | */ |
||
| 49 | private $insertId = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The full SQL query statment after build for each command |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $query = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The result returned for the last query |
||
| 59 | * @var mixed |
||
| 60 | */ |
||
| 61 | private $result = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The number of executed query for the current request |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | private $queryCount = 0; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The default data to be used in the INSERT, UPDATE statments |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $data = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The cache default time to live in second. 0 means no need |
||
| 77 | * to use the cache feature |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | private $cacheTtl = 0; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The cache current time to live. 0 means no need |
||
| 84 | * to use the cache feature |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | private $temporaryCacheTtl = 0; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The DatabaseQueryBuilder instance |
||
| 91 | * @var object |
||
| 92 | */ |
||
| 93 | protected $queryBuilder = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The DatabaseQueryRunner instance |
||
| 97 | * @var object |
||
| 98 | */ |
||
| 99 | protected $queryRunner = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The DatabaseCache instance |
||
| 103 | * @var object |
||
| 104 | */ |
||
| 105 | protected $cache = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Construct new instance |
||
| 109 | * |
||
| 110 | * @param object $connection the DatabaseConnection instance |
||
| 111 | */ |
||
| 112 | public function __construct(DatabaseConnection $connection = null) { |
||
| 113 | parent::__construct(); |
||
| 114 | if ($connection !== null) { |
||
| 115 | $this->connection = $connection; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the result of one record rows returned by the current query |
||
| 121 | * @param boolean|string $sqlOrResult if is boolean and true will return the SQL query string. |
||
| 122 | * If is string will determine the result type "array" or "object" |
||
| 123 | * @return mixed the query SQL string or the record result |
||
| 124 | */ |
||
| 125 | public function get($sqlOrResult = false) { |
||
| 126 | $this->queryBuilder->limit(1); |
||
| 127 | $query = $this->getAll(true); |
||
| 128 | if ($sqlOrResult === true) { |
||
| 129 | return $query; |
||
| 130 | } |
||
| 131 | return $this->query($query, false, $sqlOrResult == 'array'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the result of record rows list returned by the current query |
||
| 136 | * @param boolean|string $sqlOrResult if is boolean and true will return the SQL query string. |
||
| 137 | * If is string will determine the result type "array" or "object" |
||
| 138 | * @return mixed the query SQL string or the record result |
||
| 139 | */ |
||
| 140 | public function getAll($sqlOrResult = false) { |
||
| 141 | $query = $this->queryBuilder->getQuery(); |
||
| 142 | if ($sqlOrResult === true) { |
||
| 143 | return $query; |
||
| 144 | } |
||
| 145 | return $this->query($query, true, $sqlOrResult == 'array'); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Insert new record in the database |
||
| 150 | * @param array $data the record data if is empty will use the $this->data array. |
||
| 151 | * @param boolean $escape whether to escape or not the values |
||
| 152 | * @return mixed the insert id of the new record or null |
||
| 153 | */ |
||
| 154 | public function insert($data = array(), $escape = true) { |
||
| 155 | if (empty($data) && !empty($this->data)) { |
||
| 156 | //as when using $this->setData() may be the data already escaped |
||
| 157 | $escape = false; |
||
| 158 | $data = $this->data; |
||
| 159 | } |
||
| 160 | $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
||
| 161 | $result = $this->query($query); |
||
| 162 | if ($result) { |
||
| 163 | $this->setLastInsertId(); |
||
| 164 | return $this->insertId; |
||
| 165 | } |
||
| 166 | return false; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Update record in the database |
||
| 171 | * @param array $data the record data if is empty will use the $this->data array. |
||
| 172 | * @param boolean $escape whether to escape or not the values |
||
| 173 | * @return mixed the update status |
||
| 174 | */ |
||
| 175 | public function update($data = array(), $escape = true) { |
||
| 176 | if (empty($data) && !empty($this->data)) { |
||
| 177 | //as when using $this->setData() may be the data already escaped |
||
| 178 | $escape = false; |
||
| 179 | $data = $this->data; |
||
| 180 | } |
||
| 181 | $query = $this->queryBuilder->update($data, $escape)->getQuery(); |
||
| 182 | return $this->query($query); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Delete the record in database |
||
| 187 | * @return mixed the delete status |
||
| 188 | */ |
||
| 189 | public function delete() { |
||
| 190 | $query = $this->queryBuilder->delete()->getQuery(); |
||
| 191 | return $this->query($query); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set database cache time to live |
||
| 196 | * @param integer $ttl the cache time to live in second |
||
| 197 | * @return object the current instance |
||
| 198 | */ |
||
| 199 | public function setCacheTimeToLive($ttl = 0) { |
||
| 200 | $this->cacheTtl = $ttl; |
||
| 201 | $this->temporaryCacheTtl = $ttl; |
||
| 202 | return $this; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get database cache time to live |
||
| 207 | * @return integer |
||
| 208 | */ |
||
| 209 | public function getCacheTimeToLive() { |
||
| 210 | return $this->cacheTtl; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get database current cache time to live |
||
| 215 | * @return integer |
||
| 216 | */ |
||
| 217 | public function getTempCacheTimeToLive() { |
||
| 218 | return $this->temporaryCacheTtl; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Enabled cache temporary for the current query not globally |
||
| 223 | * @param integer $ttl the cache time to live in second |
||
| 224 | * @return object the current instance |
||
| 225 | */ |
||
| 226 | public function cached($ttl = 0) { |
||
| 227 | $this->temporaryCacheTtl = $ttl; |
||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return the number query executed count for the current request |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | public function queryCount() { |
||
| 236 | return $this->queryCount; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Return the current query SQL string |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getQuery() { |
||
| 244 | return $this->query; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Return the DatabaseConnection instance |
||
| 249 | * @return object DatabaseConnection |
||
| 250 | */ |
||
| 251 | public function getConnection() { |
||
| 252 | return $this->connection; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Set the DatabaseConnection instance |
||
| 257 | * @param object DatabaseConnection $connection the DatabaseConnection object |
||
| 258 | * |
||
| 259 | * @return object the current instance |
||
| 260 | */ |
||
| 261 | public function setConnection(DatabaseConnection $connection = null) { |
||
| 262 | $this->connection = $connection; |
||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Return the DatabaseQueryBuilder instance |
||
| 268 | * @return object DatabaseQueryBuilder |
||
| 269 | */ |
||
| 270 | public function getQueryBuilder() { |
||
| 271 | return $this->queryBuilder; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the DatabaseQueryBuilder instance |
||
| 276 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
||
| 277 | */ |
||
| 278 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder = null) { |
||
| 279 | $this->queryBuilder = $queryBuilder; |
||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return the DatabaseCache instance |
||
| 285 | * @return object DatabaseCache |
||
| 286 | */ |
||
| 287 | public function getCache() { |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Set the DatabaseCache instance |
||
| 293 | * @param object DatabaseCache $cache the DatabaseCache object |
||
| 294 | */ |
||
| 295 | public function setCache(DatabaseCache $cache = null) { |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return the DatabaseQueryRunner instance |
||
| 302 | * @return object DatabaseQueryRunner |
||
| 303 | */ |
||
| 304 | public function getQueryRunner() { |
||
| 305 | return $this->queryRunner; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set the DatabaseQueryRunner instance |
||
| 310 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
||
| 311 | */ |
||
| 312 | public function setQueryRunner(DatabaseQueryRunner $queryRunner = null) { |
||
| 313 | $this->queryRunner = $queryRunner; |
||
| 314 | return $this; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Return the data to be used for insert, update, etc. |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function getData() { |
||
| 322 | return $this->data; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set the data to be used for insert, update, etc. |
||
| 327 | * @param string|array $key the data key identified |
||
| 328 | * @param mixed $value the data value |
||
| 329 | * @param boolean $escape whether to escape or not the $value |
||
| 330 | * @return object the current Database instance |
||
| 331 | */ |
||
| 332 | public function setData($key, $value = null, $escape = true) { |
||
| 333 | if (is_array($key)) { |
||
| 334 | foreach ($key as $k => $v) { |
||
| 335 | $this->setData($k, $v, $escape); |
||
| 336 | } |
||
| 337 | } else { |
||
| 338 | $this->data[$key] = $this->connection->escape($value, $escape); |
||
| 339 | } |
||
| 340 | return $this; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Return the number of rows returned by the current query |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | public function numRows() { |
||
| 348 | return $this->numRows; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Return the last insert id value |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | public function insertId() { |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Execute an SQL query |
||
| 361 | * @param string $query the query SQL string |
||
| 362 | * @param boolean $returnAsList indicate whether to return all record or just one row |
||
| 363 | * @param boolean $returnAsArray return the result as array or not |
||
| 364 | * @return mixed the query result |
||
| 365 | */ |
||
| 366 | public function query($query, $returnAsList = true, $returnAsArray = false) { |
||
| 367 | $this->reset(); |
||
| 368 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
||
| 369 | $this->logger->info('Execute SQL query [' . $this->query . ']'); |
||
| 370 | |||
| 371 | $cacheExpire = $this->temporaryCacheTtl; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set the last insert id value |
||
| 413 | * |
||
| 414 | * @return object the current instance |
||
| 415 | */ |
||
| 416 | protected function setLastInsertId() { |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Reset the database class attributs to the initial values before each query. |
||
| 429 | */ |
||
| 430 | protected function reset() { |
||
| 441 |