| Total Complexity | 58 |
| Total Lines | 339 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Cache 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 Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Cache implements CacheInterface |
||
| 23 | { |
||
| 24 | /** @var Builder */ |
||
| 25 | protected $builder; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | protected $cacheDir; |
||
| 29 | |||
| 30 | public function __construct(Builder $builder, string $pool = '') |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function set($key, $value, $ttl = null): bool |
||
| 40 | { |
||
| 41 | try { |
||
| 42 | $key = self::sanitizeKey($key); |
||
| 43 | $this->prune($key); |
||
| 44 | // put file content in a dedicated file |
||
| 45 | if (\is_array($value) && !empty($value['content']) && !empty($value['path'])) { |
||
| 46 | Util\File::getFS()->dumpFile($this->getContentFilePathname($value['path']), $value['content']); |
||
| 47 | unset($value['content']); |
||
| 48 | } |
||
| 49 | // serialize data |
||
| 50 | $data = serialize([ |
||
| 51 | 'value' => $value, |
||
| 52 | 'expiration' => $ttl === null ? null : time() + $this->duration($ttl), |
||
| 53 | ]); |
||
| 54 | Util\File::getFS()->dumpFile($this->getFilePathname($key), $data); |
||
| 55 | } catch (\Exception $e) { |
||
| 56 | $this->builder->getLogger()->error($e->getMessage()); |
||
| 57 | |||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | return true; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | public function has($key): bool |
||
| 68 | { |
||
| 69 | $key = self::sanitizeKey($key); |
||
| 70 | if (!Util\File::getFS()->exists($this->getFilePathname($key))) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | return true; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | public function get($key, $default = null): mixed |
||
| 81 | { |
||
| 82 | try { |
||
| 83 | $key = self::sanitizeKey($key); |
||
| 84 | // return default value if file doesn't exists |
||
| 85 | if (false === $content = Util\File::fileGetContents($this->getFilePathname($key))) { |
||
| 86 | return $default; |
||
| 87 | } |
||
| 88 | // unserialize data |
||
| 89 | $data = unserialize($content); |
||
| 90 | // check expiration |
||
| 91 | if ($data['expiration'] !== null && $data['expiration'] <= time()) { |
||
| 92 | $this->builder->getLogger()->debug(\sprintf('Cache expired: "%s"', $key)); |
||
| 93 | // remove expired cache |
||
| 94 | if ($this->delete($key)) { |
||
| 95 | // remove content file if exists |
||
| 96 | if (!empty($data['value']['path'])) { |
||
| 97 | $this->deleteContentFile($data['value']['path']); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return $default; |
||
| 102 | } |
||
| 103 | // get content from dedicated file |
||
| 104 | if (\is_array($data['value']) && isset($data['value']['path'])) { |
||
| 105 | if (false !== $content = Util\File::fileGetContents($this->getContentFilePathname($data['value']['path']))) { |
||
| 106 | $data['value']['content'] = $content; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } catch (\Exception $e) { |
||
| 110 | $this->builder->getLogger()->error($e->getMessage()); |
||
| 111 | |||
| 112 | return $default; |
||
| 113 | } |
||
| 114 | |||
| 115 | return $data['value']; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function delete($key): bool |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function clear(): bool |
||
| 140 | { |
||
| 141 | try { |
||
| 142 | Util\File::getFS()->remove($this->cacheDir); |
||
| 143 | } catch (\Exception $e) { |
||
| 144 | $this->builder->getLogger()->error($e->getMessage()); |
||
| 145 | |||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | return true; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function getMultiple($keys, $default = null): iterable |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function setMultiple($values, $ttl = null): bool |
||
| 164 | { |
||
| 165 | throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__)); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function deleteMultiple($keys): bool |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Creates key from a name and a hash: "$name__HASH__VERSION". |
||
| 178 | */ |
||
| 179 | public function createKey(string $name, string $hash): string |
||
| 180 | { |
||
| 181 | $name = self::sanitizeKey($name); |
||
| 182 | |||
| 183 | return \sprintf('%s__%s__%s', $name, $hash, $this->builder->getVersion()); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Creates key from a string: "$name__HASH__VERSION". |
||
| 188 | * $name is optional to add a human readable name to the key. |
||
| 189 | */ |
||
| 190 | public function createKeyFromValue(?string $name, string $value): string |
||
| 191 | { |
||
| 192 | $hash = hash('md5', $value); |
||
| 193 | $name = $name ?? $hash; |
||
| 194 | |||
| 195 | return $this->createKey($name, $hash); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Creates key from an Asset: "$path_$ext_$tags__HASH__VERSION". |
||
| 200 | */ |
||
| 201 | public function createKeyFromAsset(Asset $asset, ?array $tags = null): string |
||
| 202 | { |
||
| 203 | $t = $tags; |
||
| 204 | $tags = []; |
||
| 205 | |||
| 206 | if ($t !== null) { |
||
| 207 | foreach ($t as $key => $value) { |
||
| 208 | switch (\gettype($value)) { |
||
| 209 | case 'boolean': |
||
| 210 | if ($value === true) { |
||
| 211 | $tags[] = $key; |
||
| 212 | } |
||
| 213 | break; |
||
| 214 | case 'string': |
||
| 215 | case 'integer': |
||
| 216 | if (!empty($value)) { |
||
| 217 | $tags[] = substr($key, 0, 1) . $value; |
||
| 218 | } |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | $tagsInline = implode('_', str_replace('_', '', $tags)); |
||
| 225 | $name = "{$asset['_path']}_{$asset['ext']}_$tagsInline"; |
||
| 226 | |||
| 227 | return $this->createKey($name, $asset['hash']); |
||
|
|
|||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Creates key from a file: "RelativePathname__MD5". |
||
| 232 | * |
||
| 233 | * @throws RuntimeException |
||
| 234 | */ |
||
| 235 | public function createKeyFromFile(\Symfony\Component\Finder\SplFileInfo $file): string |
||
| 236 | { |
||
| 237 | if (false === $content = Util\File::fileGetContents($file->getRealPath())) { |
||
| 238 | throw new RuntimeException(\sprintf('Can\'t create cache key for "%s".', $file)); |
||
| 239 | } |
||
| 240 | |||
| 241 | return $this->createKeyFromValue($file->getRelativePathname(), $content); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Clear cache by pattern. |
||
| 246 | */ |
||
| 247 | public function clearByPattern(string $pattern): int |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns cache content file pathname from path. |
||
| 278 | */ |
||
| 279 | public function getContentFilePathname(string $path): string |
||
| 280 | { |
||
| 281 | $path = str_replace(['https://', 'http://'], '', $path); // remove protocol (if URL) |
||
| 282 | |||
| 283 | return Util::joinFile($this->cacheDir, 'files', $path); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns cache file pathname from key. |
||
| 288 | */ |
||
| 289 | private function getFilePathname(string $key): string |
||
| 290 | { |
||
| 291 | return Util::joinFile($this->cacheDir, "$key.ser"); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Prepares and validate $key. |
||
| 296 | */ |
||
| 297 | public static function sanitizeKey(string $key): string |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Removes previous cache files. |
||
| 310 | */ |
||
| 311 | private function prune(string $key): bool |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Convert the various expressions of a TTL value into duration in seconds. |
||
| 334 | */ |
||
| 335 | protected function duration(int|\DateInterval $ttl): int |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Removes the cache content file. |
||
| 349 | */ |
||
| 350 | protected function deleteContentFile(string $path): bool |
||
| 361 | } |
||
| 362 | } |
||
| 363 |