| Total Complexity | 75 |
| Total Lines | 447 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Persistence 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 Persistence, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Persistence |
||
| 23 | { |
||
| 24 | |||
| 25 | /* |
||
| 26 | * @param Configuration $config |
||
| 27 | * |
||
| 28 | * @return PDO |
||
| 29 | */ |
||
| 30 | public static function getPDO($config) { |
||
| 31 | $options = array( |
||
| 32 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
||
| 33 | ); |
||
| 34 | |||
| 35 | if ($config->getDsn()) { |
||
| 36 | try { |
||
| 37 | return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options); |
||
| 38 | } catch (Exception $e) { |
||
| 39 | if (!$config->getUseOnMemoryDB()) { |
||
| 40 | throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | if ($config->getUseOnMemoryDB()) { |
||
| 46 | try { |
||
| 47 | return new PDO("sqlite::memory:", null, null, $options); |
||
| 48 | } catch (Exception $e) { |
||
| 49 | throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | throw new Exception("Error when trying to obtain a connection to a database. Check the configuration. "); |
||
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | /* |
||
| 58 | * @param $pdo PDO |
||
| 59 | * @param $config Configuration |
||
| 60 | * |
||
| 61 | */ |
||
| 62 | public static function crateDatabase($pdo, $config) { |
||
| 63 | try { |
||
| 64 | $db_hit_table = $config->getHitTableName(); |
||
| 65 | $db_options_table = $config->getOptionsTableName(); |
||
| 66 | $db_from_table = $config->getFromTableName(); |
||
| 67 | $db_url_table = $config->getUrlTableName(); |
||
| 68 | |||
| 69 | $stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)"); |
||
| 70 | $stmt->execute(); |
||
| 71 | $stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))"); |
||
| 72 | $stmt->execute(); |
||
| 73 | $stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)"); |
||
| 74 | $stmt->execute(); |
||
| 75 | $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
||
| 76 | $stmt->execute(); |
||
| 77 | } catch (Exception $e) { |
||
| 78 | throw new Exception("Could not create the database. ".$e->getMessage()); |
||
| 79 | } |
||
| 80 | return true; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | /* |
||
| 85 | * @param $pdo PDO |
||
| 86 | * @param $config Configuration |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | public static function deleteDatabase($pdo, $config) { |
||
| 90 | $resp = true; |
||
| 91 | |||
| 92 | $db_hit_table = $config->getHitTableName(); |
||
| 93 | $db_options_table = $config->getOptionsTableName(); |
||
| 94 | $db_from_table = $config->getFromTableName(); |
||
| 95 | $db_url_table = $config->getUrlTableName(); |
||
| 96 | |||
| 97 | $resp = $resp && Persistence::dropTable($pdo, $db_hit_table); |
||
| 98 | $resp = $resp && Persistence::dropTable($pdo, $db_options_table); |
||
| 99 | $resp = $resp && Persistence::dropTable($pdo, $db_from_table); |
||
| 100 | $resp = $resp && Persistence::dropTable($pdo, $db_url_table); |
||
| 101 | |||
| 102 | return $resp; |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /* |
||
| 107 | * @param $pdo PDO |
||
| 108 | * @param $config Configuration |
||
| 109 | * |
||
| 110 | */ |
||
| 111 | private static function dropTable($pdo, $tableName) { |
||
| 112 | try { |
||
| 113 | $stmt = $pdo->prepare("DROP TABLE $tableName"); |
||
| 114 | $stmt->execute(); |
||
| 115 | |||
| 116 | } catch (Exception $e) { |
||
| 117 | throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
||
| 118 | } |
||
| 119 | return true; |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /* |
||
| 124 | * @param $pdo PDO |
||
| 125 | * @param $config Configuration |
||
| 126 | * |
||
| 127 | */ |
||
| 128 | public static function checkTables($pdo, $config) { |
||
| 129 | $resp = true; |
||
| 130 | try { |
||
| 131 | |||
| 132 | $resp = $resp && Persistence::checkHitTable($pdo, $config); |
||
|
|
|||
| 133 | $resp = $resp && Persistence::checkOptionsTable($pdo, $config); |
||
| 134 | $resp = $resp && Persistence::checkFromTable($pdo, $config); |
||
| 135 | $resp = $resp && Persistence::checkUrlTable($pdo, $config); |
||
| 136 | } catch (Exception $e) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | return $resp; |
||
| 140 | |||
| 141 | } |
||
| 142 | |||
| 143 | /* |
||
| 144 | * @param $pdo PDO |
||
| 145 | * @param $config Configuration |
||
| 146 | */ |
||
| 147 | public static function checkFromTable($pdo, $config) { |
||
| 156 | } |
||
| 157 | |||
| 158 | /* |
||
| 159 | * @param $pdo PDO |
||
| 160 | * @param $config Configuration |
||
| 161 | */ |
||
| 162 | public static function checkUrlTable($pdo, $config) { |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | /* |
||
| 176 | * @param $pdo PDO |
||
| 177 | * @param $config Configuration |
||
| 178 | */ |
||
| 179 | public static function checkOptionsTable($pdo, $config) { |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | /* |
||
| 195 | * @param $pdo PDO |
||
| 196 | * @param $config Configuration |
||
| 197 | * |
||
| 198 | */ |
||
| 199 | public static function countFrom($pdo, $config, $options = []) { |
||
| 200 | if (array_key_exists('from_id', $options)) { |
||
| 201 | $ids = [$options['from_id']]; |
||
| 202 | } else { |
||
| 203 | $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info'; |
||
| 204 | $ids = Persistence::findHitIdsByUrl($pdo, $config, $from_url); |
||
| 205 | if (count($ids)==0) { |
||
| 206 | $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
||
| 207 | $stmt->execute([$from_url, $from_url]); |
||
| 208 | $ids = [$from_url]; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | foreach ($ids as $from_id) { |
||
| 212 | $stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?"); |
||
| 213 | $stmt->execute([$id, $from_id]); |
||
| 214 | if ($stmt->rowCount()==0) { |
||
| 215 | $stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)"); |
||
| 216 | $stmt->execute([$id, $from_id]); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /* |
||
| 222 | * @param $pdo PDO |
||
| 223 | * @param $config Configuration |
||
| 224 | * |
||
| 225 | */ |
||
| 226 | public static function countOptions($pdo, $config, $options = []) { |
||
| 227 | $user = null; |
||
| 228 | if ($store_user) { |
||
| 229 | if (array_key_exists('user', $options)) { |
||
| 230 | $user = $options['user']; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($store_time || $store_user) { |
||
| 235 | $stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)"); |
||
| 236 | $stmt->execute([$id, time(), $user]); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /* |
||
| 241 | * @param $pdo PDO |
||
| 242 | * @param $config Configuration |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | public static function updateCount($pdo, $config, $options = []) { |
||
| 246 | |||
| 247 | $db_hit_table = $config->getHitTableName(); |
||
| 248 | $db_options_table = $config->getOptionsTableName(); |
||
| 249 | $db_from_table = $config->getFromTableName(); |
||
| 250 | $db_url_table = $config->getUrlTableName(); |
||
| 251 | |||
| 252 | $store_from = true; |
||
| 253 | $store_time = true; |
||
| 254 | $store_user = true; |
||
| 255 | |||
| 256 | Persistence::countHit($pdo, $config, $options); |
||
| 257 | Persistence::countFrom($pdo, $config, $options); |
||
| 258 | Persistence::countOptions($pdo, $config, $options); |
||
| 259 | |||
| 260 | return true; |
||
| 261 | } |
||
| 262 | |||
| 263 | /* |
||
| 264 | * @param $pdo PDO |
||
| 265 | * @param $config Configuration |
||
| 266 | * |
||
| 267 | */ |
||
| 268 | public static function findHitIdsByUrl($pdo, $config, $url) { |
||
| 269 | $resp = []; |
||
| 270 | try { |
||
| 271 | |||
| 272 | $dbtable = $config->getUrlTableName(); |
||
| 273 | $stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'"); |
||
| 274 | if ($stmt->execute()) { |
||
| 275 | while ($row = $stmt->fetch()) { |
||
| 276 | $resp[] = $row['id']; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } catch (Exception $e) { |
||
| 280 | throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage()); |
||
| 281 | } |
||
| 282 | return $resp; |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | /* |
||
| 288 | * @param $pdo PDO |
||
| 289 | * @param $config Configuration |
||
| 290 | * |
||
| 291 | */ |
||
| 292 | public static function findUrls($pdo, $config, $by = []) { |
||
| 293 | $resp = []; |
||
| 294 | try { |
||
| 295 | $dbtable = $config->getUrlTableName(); |
||
| 296 | $qdata = []; |
||
| 297 | $tquery = []; |
||
| 298 | if (array_key_exists('id', $by)) { |
||
| 299 | $qdata[] = $by['id']; |
||
| 300 | $tquery[] = "id = ?"; |
||
| 301 | } |
||
| 302 | |||
| 303 | if (array_key_exists('url', $by)) { |
||
| 304 | $qdata[] = $by['url']; |
||
| 305 | $tquery[] = "url = ?"; |
||
| 306 | } |
||
| 307 | |||
| 308 | $sql = "SELECT id,url,count FROM $dbtable"; |
||
| 309 | if (count($tquery) > 0) { |
||
| 310 | $sql = $sql." WHERE ".join(" AND ", $tquery); |
||
| 311 | } |
||
| 312 | |||
| 313 | $stmt = $pdo->prepare($sql); |
||
| 314 | if ($stmt->execute($qdata)) { |
||
| 315 | while ($row = $stmt->fetch()) { |
||
| 316 | $resp[] = [$row['id'], $row['url'], $row['count']]; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | } catch (Exception $e) { |
||
| 321 | throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
||
| 322 | } |
||
| 323 | return $resp; |
||
| 324 | } |
||
| 325 | |||
| 326 | /* |
||
| 327 | * @param $pdo PDO |
||
| 328 | * @param $config Configuration |
||
| 329 | * |
||
| 330 | */ |
||
| 331 | public static function findIdByTimeUser($pdo, $config, $by = []) { |
||
| 332 | $resp = []; |
||
| 333 | try { |
||
| 334 | $dbtable = $config->getOptionsTableName(); |
||
| 335 | $qdata = []; |
||
| 336 | $tquery = []; |
||
| 337 | if (array_key_exists('from', $by)) { |
||
| 338 | $qdata[] = $by['from']; |
||
| 339 | $tquery[] = "time >= ?"; |
||
| 340 | } |
||
| 341 | |||
| 342 | if (array_key_exists('to', $by)) { |
||
| 343 | $qdata[] = $by['to']; |
||
| 344 | $tquery[] = "time <= ?"; |
||
| 345 | } |
||
| 346 | |||
| 347 | if (array_key_exists('user', $by)) { |
||
| 348 | $qdata[] = $by['user']; |
||
| 349 | $tquery[] = "user = ?"; |
||
| 350 | } |
||
| 351 | |||
| 352 | $sql = "SELECT id,time,user FROM $dbtable"; |
||
| 353 | if (count($tquery) > 0) { |
||
| 354 | $sql = $sql." WHERE ".join(" AND ", $tquery); |
||
| 355 | } |
||
| 356 | |||
| 357 | $stmt = $pdo->prepare($sql); |
||
| 358 | if ($stmt->execute($qdata)) { |
||
| 359 | while ($row = $stmt->fetch()) { |
||
| 360 | $resp[] = [$row['id'], $row['time'], $row['user']]; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | } catch (Exception $e) { |
||
| 365 | throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
||
| 366 | } |
||
| 367 | return $resp; |
||
| 368 | } |
||
| 369 | |||
| 370 | |||
| 371 | /* |
||
| 372 | * @param $pdo PDO |
||
| 373 | * @param $config Configuration |
||
| 374 | * |
||
| 375 | */ |
||
| 376 | public static function findByFrom($pdo, $config, $by = []) { |
||
| 409 | } |
||
| 410 | |||
| 411 | |||
| 412 | |||
| 413 | /* |
||
| 414 | * @param $pdo PDO |
||
| 415 | * @param $config Configuration |
||
| 416 | * |
||
| 417 | */ |
||
| 418 | public static function getCounts($pdo, $config) |
||
| 436 | } |
||
| 437 | |||
| 438 | |||
| 439 | /* |
||
| 440 | * @param $pdo PDO |
||
| 441 | * @param $config Configuration |
||
| 442 | * |
||
| 443 | */ |
||
| 444 | public static function getHitsWithOptions($pdo, $config) { |
||
| 460 | } |
||
| 461 | |||
| 462 | /* |
||
| 463 | * @param $pdo PDO |
||
| 464 | * @param $config Configuration |
||
| 465 | * |
||
| 466 | */ |
||
| 467 | public static function getCountsById($pdo, $config) { |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | |||
| 474 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.