| Total Complexity | 47 |
| Total Lines | 295 |
| Duplicated Lines | 0 % |
| Coverage | 56.91% |
| Changes | 15 | ||
| 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 | /** @var int */ |
||
| 31 | protected $duration; |
||
| 32 | |||
| 33 | 1 | public function __construct(Builder $builder, string $pool = '') |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | 1 | public function has($key): bool |
|
| 44 | { |
||
| 45 | 1 | $key = self::sanitizeKey($key); |
|
| 46 | 1 | if (!Util\File::getFS()->exists($this->getFilePathname($key))) { |
|
| 47 | 1 | return false; |
|
| 48 | } |
||
| 49 | |||
| 50 | 1 | return true; |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | 1 | public function get($key, $default = null): mixed |
|
| 57 | { |
||
| 58 | try { |
||
| 59 | 1 | $key = self::sanitizeKey($key); |
|
| 60 | // return default value if file doesn't exists |
||
| 61 | 1 | if (false === $content = Util\File::fileGetContents($this->getFilePathname($key))) { |
|
| 62 | return $default; |
||
| 63 | } |
||
| 64 | // unserialize data |
||
| 65 | 1 | $data = unserialize($content); |
|
| 66 | // check expiration |
||
| 67 | 1 | if ($data['expiration'] <= time()) { |
|
| 68 | $this->delete($key); |
||
| 69 | |||
| 70 | return $default; |
||
| 71 | } |
||
| 72 | // get content from dedicated file |
||
| 73 | 1 | if (\is_array($data['value']) && isset($data['value']['path'])) { |
|
| 74 | 1 | if (false !== $content = Util\File::fileGetContents($this->getContentFilePathname($data['value']['path']))) { |
|
| 75 | 1 | $data['value']['content'] = $content; |
|
| 76 | } |
||
| 77 | } |
||
| 78 | } catch (\Exception $e) { |
||
| 79 | $this->builder->getLogger()->error($e->getMessage()); |
||
| 80 | |||
| 81 | return $default; |
||
| 82 | } |
||
| 83 | |||
| 84 | 1 | return $data['value']; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 1 | public function set($key, $value, $ttl = null): bool |
|
| 91 | { |
||
| 92 | try { |
||
| 93 | 1 | $key = self::sanitizeKey($key); |
|
| 94 | 1 | $this->prune($key); |
|
| 95 | // put file content in a dedicated file |
||
| 96 | 1 | if (\is_array($value) && !empty($value['content']) && !empty($value['path'])) { |
|
| 97 | 1 | Util\File::getFS()->dumpFile($this->getContentFilePathname($value['path']), $value['content']); |
|
| 98 | 1 | unset($value['content']); |
|
| 99 | } |
||
| 100 | // serialize data |
||
| 101 | 1 | $data = serialize([ |
|
| 102 | 1 | 'value' => $value, |
|
| 103 | 1 | 'expiration' => time() + $this->duration($ttl), |
|
| 104 | 1 | ]); |
|
| 105 | 1 | Util\File::getFS()->dumpFile($this->getFilePathname($key), $data); |
|
| 106 | } catch (\Exception $e) { |
||
| 107 | $this->builder->getLogger()->error($e->getMessage()); |
||
| 108 | |||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | 1 | return true; |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function delete($key): bool |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function clear(): bool |
||
| 137 | { |
||
| 138 | try { |
||
| 139 | Util\File::getFS()->remove($this->cacheDir); |
||
| 140 | } catch (\Exception $e) { |
||
| 141 | $this->builder->getLogger()->error($e->getMessage()); |
||
| 142 | |||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | return true; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function getMultiple($keys, $default = null): iterable |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function setMultiple($values, $ttl = null): bool |
||
| 161 | { |
||
| 162 | throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__)); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function deleteMultiple($keys): bool |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Creates key with the MD5 hash of a string. |
||
| 175 | * $suffix is optional to add a human readable suffix to the key (e.g. "css", "js"). |
||
| 176 | */ |
||
| 177 | 1 | public function createKeyFromString(string $value, ?string $suffix = null): string |
|
| 178 | { |
||
| 179 | 1 | return \sprintf('%s%s__%s', hash('md5', $value), ($suffix ? '_' . $suffix : ''), $this->builder->getVersion()); |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Creates key from an Asset: "$path_$ext_$tags__HASH__VERSION". |
||
| 184 | */ |
||
| 185 | 1 | public function createKeyFromAsset(Asset $asset, ?array $tags = null): string |
|
| 186 | { |
||
| 187 | 1 | $tags = implode('_', $tags ?? []); |
|
| 188 | |||
| 189 | 1 | return self::sanitizeKey(\sprintf( |
|
| 190 | 1 | '%s%s%s__%s', |
|
| 191 | 1 | $asset['_path'], |
|
| 192 | 1 | "_{$asset['ext']}", |
|
| 193 | 1 | $tags ? "_$tags" : '', |
|
| 194 | 1 | $this->createKeyFromString($asset['content'] ?? '') |
|
| 195 | 1 | )); |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Creates key from a file: "RelativePathname__MD5". |
||
| 200 | * |
||
| 201 | * @throws RuntimeException |
||
| 202 | */ |
||
| 203 | 1 | public function createKeyFromFile(\Symfony\Component\Finder\SplFileInfo $file): string |
|
| 204 | { |
||
| 205 | 1 | if (false === $content = Util\File::fileGetContents($file->getRealPath())) { |
|
| 206 | throw new RuntimeException(\sprintf('Can\'t create cache key for "%s".', $file)); |
||
| 207 | } |
||
| 208 | |||
| 209 | 1 | return self::sanitizeKey(\sprintf('%s__%s', $file->getRelativePathname(), $this->createKeyFromString($content))); |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Clear cache by pattern. |
||
| 214 | */ |
||
| 215 | public function clearByPattern(string $pattern): int |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns cache content file pathname from path. |
||
| 246 | */ |
||
| 247 | 1 | public function getContentFilePathname(string $path): string |
|
| 248 | { |
||
| 249 | 1 | $path = str_replace(['https://', 'http://'], '', $path); // remove protocol (if URL) |
|
| 250 | |||
| 251 | 1 | return Util::joinFile($this->cacheDir, 'files', $path); |
|
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns cache file pathname from key. |
||
| 256 | */ |
||
| 257 | 1 | private function getFilePathname(string $key): string |
|
| 258 | { |
||
| 259 | 1 | return Util::joinFile($this->cacheDir, "$key.ser"); |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Prepares and validate $key. |
||
| 264 | */ |
||
| 265 | 1 | public static function sanitizeKey(string $key): string |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Removes previous cache files. |
||
| 278 | */ |
||
| 279 | 1 | private function prune(string $key): bool |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Convert the various expressions of a TTL value into duration in seconds. |
||
| 303 | */ |
||
| 304 | 1 | protected function duration(\DateInterval|int|null $ttl): int |
|
| 319 |