. */ namespace SP\Infrastructure\File; use RuntimeException; use SP\Domain\Storage\Ports\FileCacheInterface; /** * Class FileCachePacked * * @package SP\Infrastructure\File; */ final class FileCachePacked extends FileCacheBase { /** * @throws RuntimeException * @throws FileException */ public function load(?string $path = null): mixed { $this->checkOrInitializePath($path); $this->path->checkIsReadable(); $dataUnpacked = gzuncompress($this->path->readToString()); if ($dataUnpacked === false) { throw new FileException( sprintf( __('Error while decompressing the file data (%s)'), $this->path->getFile() ) ); } $data = unserialize($dataUnpacked); if ($data === false) { throw new FileException(__('Error while retrieving the data')); } return $data; } /** * @throws FileException */ public function save(mixed $data, ?string $path = null): FileCacheInterface { $this->checkOrInitializePath($path); $this->createPath(); $data = gzcompress(serialize($data)); if ($data === false) { throw new FileException( sprintf( __('Error while compressing the file data (%s)'), $this->path->getFile() ) ); } $this->path->checkIsWritable()->write($data)->close(); return $this; } }