. */ namespace SP\Infrastructure\File; use SP\Domain\Common\Adapters\Serde; use SP\Domain\Core\Exceptions\InvalidClassException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Storage\Ports\FileCacheService; use function SP\__u; /** * Class FileCache */ class FileCache extends FileCacheBase { /** * @throws FileException * @throws SPException */ public function load(?string $path = null): mixed { $this->checkOrInitializePath($path); return Serde::deserialize($this->path->checkIsReadable()->readToString()); } /** * @throws FileException */ public function save(mixed $data, ?string $path = null): FileCacheService { $this->checkOrInitializePath($path); $this->createPath(); $this->path->write(Serde::serialize($data)); return $this; } /** * @inheritDoc * @throws InvalidClassException */ public function loadWith(string $class): object { $data = unserialize($this->path->checkIsReadable()->readToString(), ['allowed_classes' => [$class]]); if (!class_exists($class) || !($data instanceof $class)) { throw InvalidClassException::error( sprintf(__u('Either class does not exist or file data cannot unserialized into: %s'), $class) ); } return $data; } }