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