| Total Complexity | 47 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Driver 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 Driver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Driver implements ExtendedCacheItemPoolInterface |
||
| 33 | { |
||
| 34 | use DriverBaseTrait, IOHelperTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * |
||
| 38 | */ |
||
| 39 | const FILE_DIR = 'sqlite'; |
||
| 40 | /** |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | const INDEXING_FILE = 'indexing'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $maxSize = 10; // 10 mb |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $currentDB = 1; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $SqliteDir = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \PDO |
||
| 62 | */ |
||
| 63 | protected $indexing; |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * @return string |
||
| 68 | * @throws \Phpfastcache\Exceptions\PhpfastcacheCoreException |
||
| 69 | */ |
||
| 70 | public function getSqliteDir(): string |
||
| 71 | { |
||
| 72 | return $this->SqliteDir ?: $this->getPath() . DIRECTORY_SEPARATOR . self::FILE_DIR; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function driverCheck(): bool |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return bool |
||
| 85 | * @throws PhpfastcacheIOException |
||
| 86 | */ |
||
| 87 | protected function driverConnect(): bool |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 105 | * @return null|array |
||
| 106 | */ |
||
| 107 | protected function driverRead(CacheItemInterface $item) |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 139 | * @return mixed |
||
| 140 | * @throws PhpfastcacheInvalidArgumentException |
||
| 141 | */ |
||
| 142 | protected function driverWrite(CacheItemInterface $item): bool |
||
| 143 | { |
||
| 144 | /** |
||
| 145 | * Check for Cross-Driver type confusion |
||
| 146 | */ |
||
| 147 | if ($item instanceof Item) { |
||
| 148 | try { |
||
| 149 | $stm = $this->getDb($item->getKey()) |
||
| 150 | ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
||
| 151 | $stm->execute([ |
||
| 152 | ':keyword' => $item->getKey(), |
||
| 153 | ':object' => $this->encode($this->driverPreWrap($item)), |
||
| 154 | ':exp' => $item->getExpirationDate()->getTimestamp(), |
||
| 155 | ]); |
||
| 156 | |||
| 157 | return true; |
||
| 158 | } catch (\PDOException $e) { |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 168 | * @return bool |
||
| 169 | * @throws PhpfastcacheInvalidArgumentException |
||
| 170 | */ |
||
| 171 | protected function driverDelete(CacheItemInterface $item): bool |
||
| 172 | { |
||
| 173 | /** |
||
| 174 | * Check for Cross-Driver type confusion |
||
| 175 | */ |
||
| 176 | if ($item instanceof Item) { |
||
| 177 | try { |
||
| 178 | $stm = $this->getDb($item->getKey()) |
||
| 179 | ->prepare("DELETE FROM `caching` WHERE (`exp` <= :U) OR (`keyword`=:keyword) "); |
||
| 180 | |||
| 181 | return $stm->execute([ |
||
| 182 | ':keyword' => $item->getKey(), |
||
| 183 | ':U' => \time(), |
||
| 184 | ]); |
||
| 185 | } catch (PDOException $e) { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | protected function driverClear(): bool |
||
| 197 | { |
||
| 198 | $this->instance = []; |
||
| 199 | $this->indexing = null; |
||
| 200 | |||
| 201 | // delete everything before reset indexing |
||
| 202 | $dir = opendir($this->getSqliteDir()); |
||
| 203 | while ($file = readdir($dir)) { |
||
|
|
|||
| 204 | if ($file != '.' && $file != '..') { |
||
| 205 | unlink($this->getSqliteDir() . '/' . $file); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return true; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * INIT NEW DB |
||
| 214 | * @param \PDO $db |
||
| 215 | */ |
||
| 216 | public function initDB(\PDO $db) |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * INIT Indexing DB |
||
| 227 | * @param \PDO $db |
||
| 228 | */ |
||
| 229 | public function initIndexing(\PDO $db) |
||
| 230 | { |
||
| 231 | |||
| 232 | // delete everything before reset indexing |
||
| 233 | $dir = opendir($this->SqliteDir); |
||
| 234 | while ($file = readdir($dir)) { |
||
| 235 | if ($file != '.' && $file != '..' && $file != 'indexing' && $file != 'dbfastcache') { |
||
| 236 | unlink($this->SqliteDir . '/' . $file); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | $db->exec('DROP TABLE if exists "balancing"'); |
||
| 241 | $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); |
||
| 242 | $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); |
||
| 243 | $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); |
||
| 244 | |||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * INIT Instant DB |
||
| 249 | * Return Database of Keyword |
||
| 250 | * @param $keyword |
||
| 251 | * @return int |
||
| 252 | */ |
||
| 253 | public function indexing($keyword) |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param $keyword |
||
| 320 | * @param bool $reset |
||
| 321 | * @return PDO |
||
| 322 | */ |
||
| 323 | public function getDb($keyword, $reset = false): PDO |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | public function __sleep(): array |
||
| 360 | } |
||
| 361 | } |