Simplify exception handling for Framework Cache classses

This commit is contained in:
Matias Griese 2021-12-07 12:52:42 +02:00
parent 042d4a4603
commit 627a1510dc
3 changed files with 25 additions and 7 deletions

View File

@ -33,11 +33,15 @@ class ChainCache extends AbstractCache
* Chain Cache constructor.
* @param array $caches
* @param null|int|DateInterval $defaultLifetime
* @throws \Psr\SimpleCache\InvalidArgumentException|InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct(array $caches, $defaultLifetime = null)
{
parent::__construct('', $defaultLifetime);
try {
parent::__construct('', $defaultLifetime);
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
if (!$caches) {
throw new InvalidArgumentException('At least one cache must be specified');

View File

@ -29,12 +29,16 @@ class DoctrineCache extends AbstractCache
* @param CacheProvider $doctrineCache
* @param string $namespace
* @param null|int|DateInterval $defaultLifetime
* @throws \Psr\SimpleCache\InvalidArgumentException|InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct(CacheProvider $doctrineCache, $namespace = '', $defaultLifetime = null)
{
// Do not use $namespace or $defaultLifetime directly, store them with constructor and fetch with methods.
parent::__construct($namespace, $defaultLifetime);
try {
parent::__construct($namespace, $defaultLifetime);
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
// Set namespace to Doctrine Cache provider if it was given.
$namespace = $this->getNamespace();

View File

@ -42,9 +42,13 @@ class FileCache extends AbstractCache
*/
public function __construct($namespace = '', $defaultLifetime = null, $folder = null)
{
parent::__construct($namespace, $defaultLifetime ?: 31557600); // = 1 year
try {
parent::__construct($namespace, $defaultLifetime ?: 31557600); // = 1 year
$this->initFileCache($namespace, $folder ?? '');
$this->initFileCache($namespace, $folder ?? '');
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
}
/**
@ -103,7 +107,13 @@ class FileCache extends AbstractCache
{
$file = $this->getFile($key);
return (!file_exists($file) || @unlink($file) || !file_exists($file));
$result = false;
if (file_exists($file)) {
$result = @unlink($file);
$result &= !file_exists($file);
}
return $result;
}
/**