. */ namespace SP\Infrastructure\File; /** * Class FileCacheBase * * @package SP\Infrastructure\File */ abstract class FileCacheBase implements FileCacheInterface { protected FileHandler $path; public function __construct(string $path) { $this->path = new FileHandler($path); } public static function factory(string $path): FileCacheBase { return new static($path); } /** * Returns if the file is expired adding time to modification date * * @throws FileException */ public function isExpired(int $time = 86400): bool { $this->path->checkFileExists(); return time() > $this->path->getFileTime() + $time; } /** * Returns if the file is expired adding time to modification date * * @throws FileException */ public function isExpiredDate(int $date): bool { $this->path->checkFileExists(); return $date > $this->path->getFileTime(); } /** * @throws FileException */ public function createPath(): void { $path = dirname($this->path->getFile()); if (!is_dir($path) && !mkdir($path, 0700, true) && !is_dir($path)) { throw new FileException(sprintf(__('Unable to create the directory (%s)'), $path)); } } /** * @throws FileException */ public function delete(): FileCacheInterface { $this->path->delete(); return $this; } public function exists(): bool { return file_exists($this->path->getFile()); } }