| Total Complexity | 43 |
| Total Lines | 266 |
| 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) { |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | } |
||
| 44 | |||
| 45 | /* |
||
| 46 | * @param $pdo PDO |
||
| 47 | * @param $config Configuration |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | public static function crateDatabase($pdo, $config) { |
||
| 51 | try { |
||
| 52 | $db_hit_table = $config->getHitTableName(); |
||
| 53 | $db_options_table = $config->getOptionsTableName(); |
||
| 54 | $db_from_table = $config->getFromTableName(); |
||
| 55 | $db_url_table = $config->getUrlTableName(); |
||
| 56 | |||
| 57 | $stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)"); |
||
| 58 | $stmt->execute(); |
||
| 59 | $stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))"); |
||
| 60 | $stmt->execute(); |
||
| 61 | $stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)"); |
||
| 62 | $stmt->execute(); |
||
| 63 | $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
||
| 64 | $stmt->execute(); |
||
| 65 | |||
| 66 | } catch (Exception $e) { |
||
| 67 | throw new Exception("Could not create the database. ".$e->getMessage()); |
||
| 68 | } |
||
| 69 | return true; |
||
| 70 | } |
||
| 71 | |||
| 72 | /* |
||
| 73 | * @param $pdo PDO |
||
| 74 | * @param $config Configuration |
||
| 75 | * |
||
| 76 | */ |
||
| 77 | public static function deleteDatabase($pdo, $config) { |
||
| 91 | } |
||
| 92 | |||
| 93 | /* |
||
| 94 | * @param $pdo PDO |
||
| 95 | * @param $config Configuration |
||
| 96 | * |
||
| 97 | */ |
||
| 98 | private static function dropTable($pdo, $tableName) { |
||
| 99 | try { |
||
| 100 | $stmt = $pdo->prepare("DROP TABLE $tableName"); |
||
| 101 | $stmt->execute(); |
||
| 102 | |||
| 103 | } catch (Exception $e) { |
||
| 104 | throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
||
| 105 | } |
||
| 106 | return true; |
||
| 107 | } |
||
| 108 | |||
| 109 | /* |
||
| 110 | * @param $pdo PDO |
||
| 111 | * @param $config Configuration |
||
| 112 | * |
||
| 113 | */ |
||
| 114 | public static function checkTables($pdo, $config) { |
||
| 115 | $resp = true; |
||
| 116 | try { |
||
| 117 | |||
| 118 | $resp = $resp && HitDAO::checkHitTable($pdo, $config); |
||
| 119 | $resp = $resp && OptionsDAO::checkOptionsTable($pdo, $config); |
||
| 120 | $resp = $resp && FromDAO::checkFromTable($pdo, $config); |
||
| 121 | $resp = $resp && UrlDAO::checkUrlTable($pdo, $config); |
||
| 122 | } catch (Exception $e) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | return $resp; |
||
| 126 | |||
| 127 | } |
||
| 128 | |||
| 129 | /* |
||
| 130 | * @param options |
||
| 131 | * |
||
| 132 | */ |
||
| 133 | public static function getURL($config, $options = []) { |
||
| 149 | } |
||
| 150 | |||
| 151 | /* |
||
| 152 | * @param $pdo PDO |
||
| 153 | * @param $config Configuration |
||
| 154 | * |
||
| 155 | */ |
||
| 156 | public static function updateCount($pdo, $config, $options = []) { |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | /* |
||
| 174 | * @param $pdo PDO |
||
| 175 | * @param $config Configuration |
||
| 176 | * |
||
| 177 | */ |
||
| 178 | public static function getCountsFromTest($pdo, $config, $tests){ |
||
| 179 | $resp = []; |
||
| 180 | foreach ($tests as $test) { |
||
| 181 | $counts = self::getCountsById($pdo, $config, $test[0]); |
||
| 182 | if (count($counts) > 0) $resp[] = $counts; |
||
| 183 | } |
||
| 184 | return $resp; |
||
| 185 | } |
||
| 186 | |||
| 187 | /* |
||
| 188 | * @param $pdo PDO |
||
| 189 | * @param $config Configuration |
||
| 190 | * |
||
| 191 | */ |
||
| 192 | public static function getFromByTest($pdo, $config, $tests){ |
||
| 193 | $resp = []; |
||
| 194 | foreach ($tests as $test) { |
||
| 195 | $counts = FromDAO::findByFromById($pdo, $config, $test[1][1], $test[1][0]); |
||
| 196 | foreach ($counts as $row) { |
||
| 197 | $resp[] = $row; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | return $resp; |
||
| 201 | } |
||
| 202 | |||
| 203 | /* |
||
| 204 | * @param $pdo PDO |
||
| 205 | * @param $config Configuration |
||
| 206 | * |
||
| 207 | */ |
||
| 208 | public static function getCounts($pdo, $config) |
||
| 209 | { |
||
| 210 | $resp = []; |
||
| 211 | try { |
||
| 212 | |||
| 213 | $dbHitTable = $config->getHitTableName(); |
||
| 214 | $dbUrlTable = $config->getUrlTableName(); |
||
| 215 | $stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id"); |
||
| 216 | if ($stmt->execute()) { |
||
| 217 | while ($row = $stmt->fetch()) { |
||
| 218 | $resp[] = [$row[0], $row[1], $row[2]]; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | } catch (Exception $e) { |
||
| 223 | throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
||
| 224 | } |
||
| 225 | return $resp; |
||
| 226 | } |
||
| 227 | |||
| 228 | /* |
||
| 229 | * @param $pdo PDO |
||
| 230 | * @param $config Configuration |
||
| 231 | */ |
||
| 232 | public static function checkUrlTable($pdo, $config) { |
||
| 233 | return UrlDAO::checkUrlTable($pdo, $config); |
||
| 234 | } |
||
| 235 | |||
| 236 | /* |
||
| 237 | * @param $pdo PDO |
||
| 238 | * @param $config Configuration |
||
| 239 | * |
||
| 240 | */ |
||
| 241 | public static function getCountsById($pdo, $config, $id) { |
||
| 242 | $resp = []; |
||
| 243 | |||
| 244 | $dbHitTable = $config->getHitTableName(); |
||
| 245 | $stmt = $pdo->prepare("SELECT h.id, h.count FROM $dbHitTable h WHERE h.id=?"); |
||
| 246 | if ($stmt->execute([$id])) { |
||
| 247 | while ($row = $stmt->fetch()) { |
||
| 248 | $resp = [$row[0], $row[1]]; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | return $resp; |
||
| 252 | } |
||
| 253 | |||
| 254 | /* |
||
| 255 | * @param $pdo PDO |
||
| 256 | * @param $config Configuration |
||
| 257 | * |
||
| 258 | */ |
||
| 259 | public static function getAllHits($pdo, $config) { |
||
| 260 | return HitDAO::getAllHits($pdo, $config); |
||
| 261 | } |
||
| 262 | |||
| 263 | /* |
||
| 264 | * @param $pdo PDO |
||
| 265 | * @param $config Configuration |
||
| 266 | * |
||
| 267 | */ |
||
| 268 | public static function findUrls($pdo, $config, $by = []) { |
||
| 269 | return UrlDAO::findUrls($pdo, $config, $by); |
||
| 270 | } |
||
| 271 | |||
| 272 | /* |
||
| 273 | * @param $pdo PDO |
||
| 274 | * @param $config Configuration |
||
| 275 | * |
||
| 276 | */ |
||
| 277 | public static function findIdByTimeUser($pdo, $config, $by=[]) { |
||
| 278 | return OptionsDAO::findIdByTimeUser($pdo, $config, $by); |
||
| 279 | } |
||
| 280 | |||
| 281 | /* |
||
| 282 | * @param $pdo PDO |
||
| 283 | * @param $config Configuration |
||
| 284 | * |
||
| 285 | */ |
||
| 286 | public static function findByFrom($pdo, $config, $by = []) { |
||
| 288 | } |
||
| 289 | |||
| 290 | } |
||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 |