Refactor shared implementation methods into a trait, check keys in pool
Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2020-05-08 10:54:09 -04:00
parent f939e46c85
commit dc53bf5ccc
4 changed files with 103 additions and 117 deletions

View File

@ -9,7 +9,7 @@
<bootstrap />
<!-- A phpDox project to process, you can have multiple projects in one config file -->
<project name="Cache" source="../src" workdir="phpdox/xml">
<project name="Banker" source="../src" workdir="phpdox/xml">
<!-- @name - The name of the project -->
<!-- @source - The source directory of the application to process -->
<!-- @workdir - The directory to store the xml data files in -->
@ -35,7 +35,7 @@
-->
<!-- Additional configuration for the collecting process (parsing of php code, generation of xml data) -->
<collector publiconly="false" backend="parser" encoding="auto">
<collector publiconly="true" backend="parser" encoding="auto">
<!-- @publiconly - Flag to disable/enable processing of non public methods and members -->
<!-- @backend - The collector backend to use, currently only shipping with 'parser' -->
<!-- @encoding - Charset encoding of source files (overwrite default 'auto' if detection fails) -->
@ -78,15 +78,15 @@
</source>
<!-- git vcs information -->
<source type="git">
<!-- <source type="git">
<git binary="/usr/bin/git" />
<history enabled="true" limit="15" cache="${phpDox.project.workdir}/gitlog.xml" />
</source>
</source> -->
<!-- PHP Code Sniffer findings -->
<source type="checkstyle">
<!-- <source type="checkstyle">
<file name="phpcs.xml" />
</source>
</source> -->
<!-- PHPMessDetector -->
<!--
@ -96,16 +96,9 @@
-->
<!-- PHPUnit Coverage XML -->
<source type="phpunit">
<coverage path="coverage/clover.xml" />
<!-- <coverage path="clover.xml" />-->
<!-- @path - the directory where the xml code coverage report can be found -->
<!--<filter directory="${phpDox.project.source}" />-->
<!-- @directory - path of the phpunit config whitelist filter directory -->
</source>
<source type="phpunit">
<!-- <source type="phpunit">
<filter directory="${phpDox.project.source}" />
</source>
</source> -->
</enrich>

View File

@ -15,7 +15,6 @@
*/
namespace Aviat\Banker;
use Aviat\Banker\Driver\DriverInterface;
use Aviat\Banker\Exception\InvalidArgumentException;
use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface};
use Psr\Log\{LoggerAwareInterface, LoggerInterface};
@ -26,16 +25,9 @@ use function is_string;
* The main cache manager
*/
final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
use _Driver;
use LoggerTrait;
/**
* Driver class for handling the chosen caching backend
*
* @var DriverInterface
*/
protected DriverInterface $driver;
/**
* Cache Items to be saved
*
@ -77,10 +69,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
*/
public function getItem($key): CacheItemInterface
{
if ( ! is_string($key))
{
throw new InvalidArgumentException();
}
$this->validateKey($key);
// If a deferred item exists, return that
if (array_key_exists($key, $this->deferred))
@ -109,6 +98,8 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
*/
public function getItems(array $keys = [])
{
$this->validateKeys($keys);
if (empty($keys))
{
return new ItemCollection([]);
@ -150,10 +141,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
*/
public function hasItem($key): bool
{
if ( ! is_string($key))
{
throw new InvalidArgumentException();
}
$this->validateKey($key);
// See if there are any deferred items
if (array_key_exists($key, $this->deferred))
@ -190,10 +178,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
*/
public function deleteItem($key): bool
{
if ( ! is_string($key))
{
throw new InvalidArgumentException();
}
$this->validateKey($key);
if ( ! $this->hasItem($key))
{
@ -218,13 +203,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
*/
public function deleteItems(array $keys): bool
{
foreach ($keys as $key)
{
if ( ! is_string($key))
{
throw new InvalidArgumentException();
}
}
$this->validateKeys($keys);
return $this->driver->deleteMultiple($keys);
}
@ -287,21 +266,4 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
return $result;
}
/**
* Instantiate the appropriate cache backend based on the config
*
* @param array $driverConfig
* @return DriverInterface
*/
protected function loadDriver(array $driverConfig = []): DriverInterface
{
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
$driverConfig['options'] = $driverConfig['options'] ?? [];
return new $class($driverConfig['connection'], $driverConfig['options']);
}
}

View File

@ -15,17 +15,17 @@
*/
namespace Aviat\Banker;
use Aviat\Banker\Driver\DriverInterface;
use Aviat\Banker\Exception\InvalidArgumentException;
use Psr\Log\LoggerAwareInterface;
use Psr\SimpleCache;
use Psr\Log\LoggerInterface;
/**
* Implements PSR-16 (SimpleCache)
*/
class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
use _Driver;
use LoggerTrait;
private DriverInterface $driver;
/**
* Set up the cache backend
*
@ -194,57 +194,4 @@ class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
return $this->driver->exists($key);
}
/**
* @param $keys
* @param bool $hash
* @throws InvalidArgumentException
*/
private function validateKeys($keys, bool $hash = FALSE): void
{
// Check type of keys
if ( ! is_iterable($keys))
{
throw new InvalidArgumentException('Keys must be an array or a traversable object');
}
$keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
// Check each key
array_walk($keys, fn($key) => $this->validateKey($key));
}
/**
* @param string $key
* @throws InvalidArgumentException
*/
private function validateKey($key): void
{
if ( ! is_string($key))
{
throw new InvalidArgumentException('Cache key must be a string.');
}
if (is_string($key) && preg_match("`[{}()/@:\\\]`", $key) === 1)
{
throw new InvalidArgumentException('Invalid characters in cache key');
}
}
/**
* Instantiate the appropriate cache backend based on the config
*
* @param array $driverConfig
* @return DriverInterface
*/
protected function loadDriver(array $driverConfig = []): DriverInterface
{
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
$driverConfig['options'] = $driverConfig['options'] ?? [];
return new $class($driverConfig['connection'], $driverConfig['options']);
}
}

84
src/_Driver.php Normal file
View File

@ -0,0 +1,84 @@
<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
*
* PHP version 7.4
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 - 2020 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 3.0.0
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker;
use Aviat\Banker\Driver\DriverInterface;
use Aviat\Banker\Exception\InvalidArgumentException;
/**
* Private trait for shared driver-related functionality
*/
trait _Driver {
/**
* Driver class for handling the chosen caching backend
*
* @var DriverInterface
*/
private DriverInterface $driver;
/**
* Instantiate the appropriate cache backend based on the config
*
* @param array $driverConfig
* @return DriverInterface
*/
protected function loadDriver(array $driverConfig = []): DriverInterface
{
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
$driverConfig['options'] = $driverConfig['options'] ?? [];
return new $class($driverConfig['connection'], $driverConfig['options']);
}
/**
* @param $keys
* @param bool $hash
* @throws InvalidArgumentException
*/
private function validateKeys($keys, bool $hash = FALSE): void
{
// Check type of keys
if ( ! is_iterable($keys))
{
throw new InvalidArgumentException('Keys must be an array or a traversable object');
}
$keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
// Check each key
array_walk($keys, fn($key) => $this->validateKey($key));
}
/**
* @param string $key
* @throws InvalidArgumentException
*/
private function validateKey($key): void
{
if ( ! is_string($key))
{
throw new InvalidArgumentException('Cache key must be a string.');
}
if (is_string($key) && preg_match("`[{}()/@:\\\]`", $key) === 1)
{
throw new InvalidArgumentException('Invalid characters in cache key');
}
}
}