| Total Complexity | 41 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 2 | Features | 1 |
Complex classes like FileCacheStore 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 FileCacheStore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class FileCacheStore implements CacheerInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @param string $cacheDir |
||
| 20 | */ |
||
| 21 | private string $cacheDir; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param array $options |
||
| 25 | */ |
||
| 26 | private array $options = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param string $message |
||
| 30 | */ |
||
| 31 | private string $message = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param integer $defaultTTL |
||
| 35 | */ |
||
| 36 | private int $defaultTTL = 3600; // 1 hour default TTL |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param boolean $success |
||
| 40 | */ |
||
| 41 | private bool $success = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param string $lastFlushTimeFile |
||
| 45 | */ |
||
| 46 | private string $lastFlushTimeFile; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var CacheLogger |
||
| 50 | */ |
||
| 51 | private $logger = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var FileCacheManager |
||
| 55 | */ |
||
| 56 | private FileCacheManager $fileManager; |
||
| 57 | |||
| 58 | public function __construct(array $options = []) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $cacheKey |
||
| 71 | * @param mixed $cacheData |
||
| 72 | * @param string $namespace |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
||
| 76 | { |
||
| 77 | $currentCacheFileData = $this->getCache($cacheKey, $namespace); |
||
| 78 | |||
| 79 | if (!$this->isSuccess()) { |
||
| 80 | return $this->getMessage(); |
||
|
|
|||
| 81 | } |
||
| 82 | |||
| 83 | $mergedCacheData = CacheFileHelper::arrayIdentifier($currentCacheFileData, $cacheData); |
||
| 84 | |||
| 85 | |||
| 86 | $this->putCache($cacheKey, $mergedCacheData, $namespace); |
||
| 87 | if ($this->isSuccess()) { |
||
| 88 | $this->setMessage("Cache updated successfully", true); |
||
| 89 | $this->logger->debug("{$this->getMessage()} from file driver."); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $cacheKey |
||
| 95 | * @param string $namespace |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | private function buildCacheFilePath(string $cacheKey, string $namespace) |
||
| 99 | { |
||
| 100 | $namespace = $namespace ? md5($namespace) . '/' : ''; |
||
| 101 | $cacheDir = "{$this->cacheDir}/"; |
||
| 102 | |||
| 103 | if (!empty($namespace)) { |
||
| 104 | $cacheDir = "{$this->cacheDir}/{$namespace}"; |
||
| 105 | $this->fileManager->createDirectory($cacheDir); |
||
| 106 | } |
||
| 107 | return $cacheDir . md5($cacheKey) . ".cache"; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $cacheKey |
||
| 112 | * @param string $namespace |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function clearCache(string $cacheKey, string $namespace = '') |
||
| 116 | { |
||
| 117 | $cacheFile = $this->buildCacheFilePath($cacheKey, $namespace); |
||
| 118 | if ($this->fileManager->readFile($cacheFile)) { |
||
| 119 | $this->fileManager->removeFile($cacheFile); |
||
| 120 | $this->setMessage("Cache file deleted successfully!", true); |
||
| 121 | } else { |
||
| 122 | $this->setMessage("Cache file does not exist!", false); |
||
| 123 | } |
||
| 124 | $this->logger->debug("{$this->getMessage()} from file driver."); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function flushCache() |
||
| 131 | { |
||
| 132 | $this->fileManager->clearDirectory($this->cacheDir); |
||
| 133 | file_put_contents($this->lastFlushTimeFile, time()); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param array $options |
||
| 138 | * @return integer |
||
| 139 | */ |
||
| 140 | private function getExpirationTime(array $options) |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getMessage() |
||
| 151 | { |
||
| 152 | return $this->message; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $cacheKey |
||
| 157 | * @param string $namespace |
||
| 158 | * @param string|int $ttl |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array $cacheKeys |
||
| 180 | * @param string $namespace |
||
| 181 | * @param string|int $ttl |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
||
| 185 | { |
||
| 186 | $ttl = CacheFileHelper::ttl($ttl, $this->defaultTTL); |
||
| 187 | $results = []; |
||
| 188 | |||
| 189 | foreach ($cacheKeys as $cacheKey) { |
||
| 190 | $cacheData = $this->getCache($cacheKey, $namespace, $ttl); |
||
| 191 | if ($this->isSuccess()) { |
||
| 192 | $results[$cacheKey] = $cacheData; |
||
| 193 | } else { |
||
| 194 | $results[$cacheKey] = null; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return $results; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param array $items |
||
| 203 | * @param string $namespace |
||
| 204 | * @param integer $batchSize |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $cacheKey |
||
| 221 | * @param mixed $cacheData |
||
| 222 | * @param string $namespace |
||
| 223 | * @param string|int $ttl |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600) |
||
| 227 | { |
||
| 228 | $cacheFile = $this->buildCacheFilePath($cacheKey, $namespace); |
||
| 229 | $data = $this->fileManager->serialize($cacheData); |
||
| 230 | |||
| 231 | $this->fileManager->writeFile($cacheFile, $data); |
||
| 232 | $this->setMessage("Cache file created successfully", true); |
||
| 233 | |||
| 234 | $this->logger->debug("{$this->getMessage()} from file driver."); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $cacheKey |
||
| 239 | * @param string $namespace |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function has(string $cacheKey, string $namespace = '') |
||
| 243 | { |
||
| 244 | $this->getCache($cacheKey, $namespace); |
||
| 245 | |||
| 246 | if ($this->isSuccess()) { |
||
| 247 | $this->setMessage("Cache key: {$cacheKey} exists and it's available! from file driver", true); |
||
| 248 | } else { |
||
| 249 | $this->setMessage("Cache key: {$cacheKey} does not exists or it's expired! from file driver", false); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $cacheKey |
||
| 255 | * @param string|int $ttl |
||
| 256 | * @param string $namespace |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | public function renewCache(string $cacheKey, string|int $ttl, string $namespace = '') |
||
| 260 | { |
||
| 261 | $cacheData = $this->getCache($cacheKey, $namespace); |
||
| 262 | if ($cacheData) { |
||
| 263 | $this->putCache($cacheKey, $cacheData, $namespace, $ttl); |
||
| 264 | $this->setMessage("Cache with key {$cacheKey} renewed successfully", true); |
||
| 265 | $this->logger->debug("{$this->getMessage()} from file driver."); |
||
| 266 | return; |
||
| 267 | } |
||
| 268 | $this->setMessage("Failed to renew Cache with key {$cacheKey}", false); |
||
| 269 | $this->logger->debug("{$this->getMessage()} from file driver."); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param array $batchItems |
||
| 274 | * @param string $namespace |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | private function processBatchItems(array $batchItems, string $namespace) |
||
| 278 | { |
||
| 279 | foreach ($batchItems as $item) { |
||
| 280 | CacheFileHelper::validateCacheItem($item); |
||
| 281 | $cacheKey = $item['cacheKey']; |
||
| 282 | $cacheData = $item['cacheData']; |
||
| 283 | $mergedData = CacheFileHelper::mergeCacheData($cacheData); |
||
| 284 | $this->putCache($cacheKey, $mergedData, $namespace); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | public function isSuccess() |
||
| 292 | { |
||
| 293 | return $this->success; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $message |
||
| 298 | * @param boolean $success |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | private function setMessage(string $message, bool $success) |
||
| 302 | { |
||
| 303 | $this->message = $message; |
||
| 304 | $this->success = $success; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $options |
||
| 309 | * @return void |
||
| 310 | */ |
||
| 311 | private function validateOptions(array $options) |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $cacheDir |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | private function initializeCacheDir(string $cacheDir) |
||
| 325 | { |
||
| 326 | $this->cacheDir = realpath($cacheDir) ?: ""; |
||
| 327 | $this->fileManager->createDirectory($cacheDir); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param array $options |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | private function handleAutoFlush(array $options) |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $flushAfter |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | private function scheduleFlush(string $flushAfter) |
||
| 346 | { |
||
| 347 | $flushAfterSeconds = CacheFileHelper::convertExpirationToSeconds($flushAfter); |
||
| 348 | |||
| 349 | if(!$this->fileManager->fileExists($this->lastFlushTimeFile)) { |
||
| 350 | $this->fileManager->writeFile($this->lastFlushTimeFile, time()); |
||
| 351 | return; |
||
| 352 | } |
||
| 353 | |||
| 354 | $lastFlushTime = (int) $this->fileManager->readFile($this->lastFlushTimeFile); |
||
| 355 | |||
| 356 | if ((time() - $lastFlushTime) >= $flushAfterSeconds) { |
||
| 357 | $this->flushCache(); |
||
| 358 | $this->fileManager->writeFile($this->lastFlushTimeFile, time()); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $cacheFile |
||
| 364 | * @param integer $ttl |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | private function isCacheValid(string $cacheFile, int $ttl) |
||
| 370 | } |
||
| 371 | } |