| Total Complexity | 50 |
| Total Lines | 489 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SqlDriverProvider 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 SqlDriverProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class SqlDriverProvider extends DriverProvider |
||
| 27 | { |
||
| 28 | use SqlDriverTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Data engine will be used on. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $tableDbEngine = 'innodb'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * PDO instance. |
||
| 39 | * |
||
| 40 | * @var object |
||
| 41 | */ |
||
| 42 | protected $db; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor. |
||
| 46 | * |
||
| 47 | * @param PDO $pdo |
||
| 48 | * @param bool $debug |
||
| 49 | */ |
||
| 50 | public function __construct(PDO $pdo, bool $debug = false) |
||
| 51 | { |
||
| 52 | $this->db = $pdo; |
||
| 53 | |||
| 54 | if ($debug) { |
||
| 55 | $this->db->setAttribute($this->db::ATTR_ERRMODE, $this->db::ERRMODE_EXCEPTION); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Initialize data tables. |
||
| 61 | * |
||
| 62 | * @param bool $dbCheck This is for creating data tables automatically |
||
| 63 | * Turn it off, if you don't want to check data tables every pageview. |
||
| 64 | * |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | protected function doInitialize(bool $dbCheck = true): void |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritDoc} |
||
| 84 | */ |
||
| 85 | protected function doFetch(string $ip, string $type = 'filter'): array |
||
| 86 | { |
||
| 87 | $tables = [ |
||
| 88 | 'rule' => 'doFetchFromRuleTable', |
||
| 89 | 'filter' => 'doFetchFromFilterTable', |
||
| 90 | 'session' => 'doFetchFromSessionTable', |
||
| 91 | ]; |
||
| 92 | |||
| 93 | $method = $tables[$type]; |
||
| 94 | |||
| 95 | // Fetch from SqlDriverTrait. |
||
| 96 | return $this->{$method}($ip); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritDoc} |
||
| 101 | */ |
||
| 102 | protected function doFetchAll(string $type = 'filter'): array |
||
| 103 | { |
||
| 104 | $tables = [ |
||
| 105 | 'rule' => 'doFetchAllFromRuleTable', |
||
| 106 | 'filter' => 'doFetchAllFromFilterTable', |
||
| 107 | 'session' => 'doFetchAllFromSessionTable', |
||
| 108 | ]; |
||
| 109 | |||
| 110 | $method = $tables[$type]; |
||
| 111 | |||
| 112 | // Fetch from SqlDriverTrait. |
||
| 113 | return $this->{$method}(); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritDoc} |
||
| 118 | */ |
||
| 119 | protected function checkExist(string $ip, string $type = 'filter'): bool |
||
| 120 | { |
||
| 121 | $tables = [ |
||
| 122 | 'rule' => [ |
||
| 123 | 'table' => $this->tableRuleList, |
||
| 124 | 'field' => 'log_ip', |
||
| 125 | ], |
||
| 126 | 'filter' => [ |
||
| 127 | 'table' => $this->tableFilterLogs, |
||
| 128 | 'field' => 'log_ip', |
||
| 129 | ], |
||
| 130 | 'session' => [ |
||
| 131 | 'table' => $this->tableSessions, |
||
| 132 | 'field' => 'id', |
||
| 133 | ], |
||
| 134 | ]; |
||
| 135 | |||
| 136 | $tableName = $tables[$type]['table']; |
||
| 137 | $field = $tables[$type]['field']; |
||
| 138 | |||
| 139 | $sql = 'SELECT ' . $field . ' FROM ' . $tableName . ' |
||
| 140 | WHERE ' . $field . ' = :' . $field . ' |
||
| 141 | LIMIT 1'; |
||
| 142 | |||
| 143 | $query = $this->db->prepare($sql); |
||
| 144 | $query->bindValue(':' . $field, $ip); |
||
| 145 | |||
| 146 | $query->execute(); |
||
| 147 | $result = $query->fetch(); |
||
| 148 | |||
| 149 | if (!empty($result[$field])) { |
||
| 150 | return true; |
||
| 151 | } |
||
| 152 | |||
| 153 | return false; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritDoc} |
||
| 158 | */ |
||
| 159 | protected function doSave(string $ip, array $data, string $type = 'filter', $expire = 0): bool |
||
| 160 | { |
||
| 161 | switch ($type) { |
||
| 162 | |||
| 163 | case 'rule': |
||
| 164 | $tableName = $this->tableRuleList; |
||
| 165 | $logWhere['log_ip'] = $ip; |
||
|
|
|||
| 166 | $logData = $data; |
||
| 167 | $logData['log_ip'] = $ip; |
||
| 168 | break; |
||
| 169 | |||
| 170 | case 'filter': |
||
| 171 | $tableName = $this->tableFilterLogs; |
||
| 172 | $logWhere['log_ip'] = $ip; |
||
| 173 | $logData['log_ip'] = $ip; |
||
| 174 | $logData['log_data'] = json_encode($data); |
||
| 175 | break; |
||
| 176 | |||
| 177 | case 'session': |
||
| 178 | $tableName = $this->tableSessions; |
||
| 179 | $logWhere['id'] = $data['id']; |
||
| 180 | $logData = $data; |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($this->checkExist($ip, $type)) { |
||
| 185 | return $this->update($tableName, $logData, $logWhere); |
||
| 186 | } |
||
| 187 | |||
| 188 | return (bool) $this->insert($tableName, $logData); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritDoc} |
||
| 193 | */ |
||
| 194 | protected function doDelete(string $ip, string $type = 'filter'): bool |
||
| 195 | { |
||
| 196 | $tables = [ |
||
| 197 | 'rule' => [ |
||
| 198 | 'table' => $this->tableRuleList, |
||
| 199 | 'field' => 'log_ip', |
||
| 200 | 'value' => $ip, |
||
| 201 | ], |
||
| 202 | 'filter' => [ |
||
| 203 | 'table' => $this->tableFilterLogs, |
||
| 204 | 'field' => 'log_ip', |
||
| 205 | 'value' => $ip, |
||
| 206 | ], |
||
| 207 | 'session' => [ |
||
| 208 | 'table' => $this->tableSessions, |
||
| 209 | 'field' => 'id', |
||
| 210 | 'value' => $ip, |
||
| 211 | ], |
||
| 212 | ]; |
||
| 213 | |||
| 214 | if (empty($tables[$type])) { |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | |||
| 218 | $tableName = $tables[$type]['table']; |
||
| 219 | $field = $tables[$type]['field']; |
||
| 220 | $value = $tables[$type]['value']; |
||
| 221 | |||
| 222 | return $this->remove($tableName, [$field => $value]); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritDoc} |
||
| 227 | */ |
||
| 228 | protected function doRebuild(): bool |
||
| 229 | { |
||
| 230 | return $this->rebuildSql(); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Update database table. |
||
| 235 | * |
||
| 236 | * @param string $table |
||
| 237 | * @param array $data |
||
| 238 | * @param array $where |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | protected function update(string $table, array $data, array $where) |
||
| 243 | { |
||
| 244 | $placeholder = []; |
||
| 245 | foreach ($data as $k => $v) { |
||
| 246 | $placeholder[] = "$k = :$k"; |
||
| 247 | } |
||
| 248 | |||
| 249 | $dataPlaceholder = implode(', ', $placeholder); |
||
| 250 | |||
| 251 | $placeholder = []; |
||
| 252 | foreach ($where as $k => $v) { |
||
| 253 | $placeholder[] = "$k = :$k"; |
||
| 254 | } |
||
| 255 | |||
| 256 | $wherePlaceholder = implode(' AND ', $placeholder); |
||
| 257 | |||
| 258 | try { |
||
| 259 | $sql = 'UPDATE ' . $table . ' SET ' . $dataPlaceholder . ' WHERE ' . $wherePlaceholder; |
||
| 260 | $query = $this->db->prepare($sql); |
||
| 261 | |||
| 262 | $bind = array_merge($data, $where); |
||
| 263 | |||
| 264 | foreach ($bind as $k => $v) { |
||
| 265 | |||
| 266 | // @codeCoverageIgnoreStart |
||
| 267 | |||
| 268 | if (is_numeric($v)) { |
||
| 269 | $pdoParam = $this->db::PARAM_INT; |
||
| 270 | |||
| 271 | // Solve problem with bigint. |
||
| 272 | if ($v >= 2147483647) { |
||
| 273 | $pdoParam = $this->db::PARAM_STR; |
||
| 274 | } |
||
| 275 | } elseif (is_bool($v)) { |
||
| 276 | $pdoParam = $this->db::PARAM_BOOL; |
||
| 277 | } elseif (is_null($v)) { |
||
| 278 | $pdoParam = $this->db::PARAM_NULL; |
||
| 279 | } else { |
||
| 280 | $pdoParam = $this->db::PARAM_STR; |
||
| 281 | } |
||
| 282 | |||
| 283 | // @codeCoverageIgnoreEnd |
||
| 284 | |||
| 285 | $query->bindValue(":$k", $bind[$k], $pdoParam); |
||
| 286 | } |
||
| 287 | |||
| 288 | return $query->execute(); |
||
| 289 | |||
| 290 | // @codeCoverageIgnoreStart |
||
| 291 | |||
| 292 | } catch(Exception $e) { |
||
| 293 | return false; |
||
| 294 | } |
||
| 295 | |||
| 296 | // @codeCoverageIgnoreEnd |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Insert database table. |
||
| 301 | * |
||
| 302 | * @param string $table |
||
| 303 | * @param array $data |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | protected function insert(string $table, array $data) |
||
| 353 | } |
||
| 354 | |||
| 355 | // @codeCoverageIgnoreEnd |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Remove a row from a table. |
||
| 360 | * |
||
| 361 | * @param string $table |
||
| 362 | * @param array $where |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | protected function remove(string $table, array $where): bool |
||
| 406 | } |
||
| 407 | |||
| 408 | // @codeCoverageIgnoreEnd |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Create SQL tables that Shieldon needs. |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | protected function installSql(): bool |
||
| 463 | } |
||
| 464 | |||
| 465 | // @codeCoverageIgnoreEnd |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Clean all records in IP log and IP rule tables, and then rebuild new tables. |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | protected function rebuildSql(): bool |
||
| 494 | } |
||
| 495 | |||
| 496 | // @codeCoverageIgnoreEnd |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Check required tables exist or not. |
||
| 501 | * |
||
| 502 | * @return bool |
||
| 503 | */ |
||
| 504 | protected function checkTableExists(): bool |
||
| 515 | } |
||
| 516 | } |