Files
sysPass/lib/SP/Infrastructure/File/FileCache.php
Rubén D cda692d4de chore: Relocate classes
Signed-off-by: Rubén D <nuxsmin@syspass.org>
2024-04-21 21:09:32 +02:00

77 lines
2.1 KiB
PHP

<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Infrastructure\File;
use SP\Domain\Core\Exceptions\InvalidClassException;
use SP\Domain\Storage\Ports\FileCacheService;
use function SP\__u;
/**
* Class FileCache
*/
class FileCache extends FileCacheBase
{
/**
* @throws FileException
*/
public function load(?string $path = null): mixed
{
$this->checkOrInitializePath($path);
return unserialize($this->path->checkIsReadable()->readToString(), ['allowed_classes' => false]);
}
/**
* @throws FileException
*/
public function save(mixed $data, ?string $path = null): FileCacheService
{
$this->checkOrInitializePath($path);
$this->createPath();
$this->path->write(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;
}
}