Properly use PHP argument types instead of the silly Exception type juggling
This commit is contained in:
parent
f3b958d17f
commit
0f46dcb2ee
205
src/Teller.php
205
src/Teller.php
@ -15,11 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
namespace Aviat\Banker;
|
namespace Aviat\Banker;
|
||||||
|
|
||||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use Psr\Log\{LoggerInterface, LoggerAwareInterface};
|
use Psr\Log\{LoggerInterface, LoggerAwareInterface};
|
||||||
use Psr\SimpleCache;
|
use Psr\SimpleCache;
|
||||||
use TypeError;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements PSR-16 (SimpleCache)
|
* Implements PSR-16 (SimpleCache)
|
||||||
@ -65,16 +63,11 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
* @throws SimpleCache\InvalidArgumentException
|
* @throws SimpleCache\InvalidArgumentException
|
||||||
* MUST be thrown if the $key string is not a legal value.
|
* MUST be thrown if the $key string is not a legal value.
|
||||||
*/
|
*/
|
||||||
public function get($key, $default = null): mixed
|
public function get(string $key, mixed $default = null): mixed
|
||||||
{
|
{
|
||||||
try
|
$this->validateKey($key);
|
||||||
{
|
|
||||||
return $this->_get($key, $default);
|
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,16 +84,11 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
* @throws SimpleCache\InvalidArgumentException
|
* @throws SimpleCache\InvalidArgumentException
|
||||||
* MUST be thrown if the $key string is not a legal value.
|
* MUST be thrown if the $key string is not a legal value.
|
||||||
*/
|
*/
|
||||||
public function set($key, mixed $value, $ttl = null): bool
|
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
|
||||||
{
|
{
|
||||||
try
|
$this->validateKey($key);
|
||||||
{
|
|
||||||
return $this->_set($key, $value, $ttl);
|
return $this->driver->set($key, $value, $ttl);
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,16 +101,11 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
* @throws SimpleCache\InvalidArgumentException
|
* @throws SimpleCache\InvalidArgumentException
|
||||||
* MUST be thrown if the $key string is not a legal value.
|
* MUST be thrown if the $key string is not a legal value.
|
||||||
*/
|
*/
|
||||||
public function delete($key): bool
|
public function delete(string $key): bool
|
||||||
{
|
{
|
||||||
try
|
$this->validateKey($key);
|
||||||
{
|
|
||||||
return $this->_delete($key);
|
return $this->driver->delete($key);
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,131 +120,7 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
* MUST be thrown if $keys is neither an array nor a Traversable,
|
* MUST be thrown if $keys is neither an array nor a Traversable,
|
||||||
* or if any of the $keys are not a legal value.
|
* or if any of the $keys are not a legal value.
|
||||||
*/
|
*/
|
||||||
public function getMultiple($keys, mixed $default = null): iterable
|
public function getMultiple(iterable $keys, mixed $default = null): iterable
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return $this->_getMultiple($keys, $default);
|
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Persists a set of key => value pairs in the cache, with an optional TTL.
|
|
||||||
*
|
|
||||||
* @param iterable $values A list of key => value pairs for a multiple-set operation.
|
|
||||||
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
|
|
||||||
* the driver supports TTL then the library may set a default value
|
|
||||||
* for it or let the driver take care of that.
|
|
||||||
*
|
|
||||||
* @return bool True on success and false on failure.
|
|
||||||
*
|
|
||||||
* @throws SimpleCache\InvalidArgumentException
|
|
||||||
* MUST be thrown if $values is neither an array nor a Traversable,
|
|
||||||
* or if any of the $values are not a legal value.
|
|
||||||
*/
|
|
||||||
public function setMultiple($values, $ttl = null): bool
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return $this->_setMultiple($values, $ttl);
|
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes multiple cache items in a single operation.
|
|
||||||
*
|
|
||||||
* @param iterable $keys A list of string-based keys to be deleted.
|
|
||||||
*
|
|
||||||
* @return bool True if the items were successfully removed. False if there was an error.
|
|
||||||
*
|
|
||||||
* @throws SimpleCache\InvalidArgumentException
|
|
||||||
* MUST be thrown if $keys is neither an array nor a Traversable,
|
|
||||||
* or if any of the $keys are not a legal value.
|
|
||||||
*/
|
|
||||||
public function deleteMultiple($keys): bool
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return $this->_deleteMultiple($keys);
|
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines whether an item is present in the cache.
|
|
||||||
*
|
|
||||||
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
|
|
||||||
* and not to be used within your live applications operations for get/set, as this method
|
|
||||||
* is subject to a race condition where your has() will return true and immediately after,
|
|
||||||
* another script can remove it making the state of your app out of date.
|
|
||||||
*
|
|
||||||
* @param string $key The cache item key.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* @throws SimpleCache\InvalidArgumentException
|
|
||||||
* MUST be thrown if the $key string is not a legal value.
|
|
||||||
*/
|
|
||||||
public function has($key): bool
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return $this->_has($key);
|
|
||||||
}
|
|
||||||
catch (TypeError $e)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches a value from the cache.
|
|
||||||
* @throws Exception\InvalidArgumentException
|
|
||||||
*/
|
|
||||||
protected function _get(string $key, mixed $default = null): mixed
|
|
||||||
{
|
|
||||||
$this->validateKey($key);
|
|
||||||
|
|
||||||
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
|
|
||||||
* @throws Exception\InvalidArgumentException
|
|
||||||
*/
|
|
||||||
protected function _set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
|
|
||||||
{
|
|
||||||
$this->validateKey($key);
|
|
||||||
|
|
||||||
return $this->driver->set($key, $value, $ttl);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete an item from the cache by its unique key.
|
|
||||||
* @throws Exception\InvalidArgumentException
|
|
||||||
*/
|
|
||||||
protected function _delete(string $key): bool
|
|
||||||
{
|
|
||||||
$this->validateKey($key);
|
|
||||||
|
|
||||||
return $this->driver->delete($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Obtains multiple cache items by their unique keys.
|
|
||||||
*/
|
|
||||||
protected function _getMultiple(iterable $keys, mixed $default = null): iterable
|
|
||||||
{
|
{
|
||||||
$keys = (array)$keys;
|
$keys = (array)$keys;
|
||||||
$this->validateKeys($keys);
|
$this->validateKeys($keys);
|
||||||
@ -286,8 +145,19 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Persists a set of key => value pairs in the cache, with an optional TTL.
|
* Persists a set of key => value pairs in the cache, with an optional TTL.
|
||||||
|
*
|
||||||
|
* @param iterable $values A list of key => value pairs for a multiple-set operation.
|
||||||
|
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
|
||||||
|
* the driver supports TTL then the library may set a default value
|
||||||
|
* for it or let the driver take care of that.
|
||||||
|
*
|
||||||
|
* @return bool True on success and false on failure.
|
||||||
|
*
|
||||||
|
* @throws SimpleCache\InvalidArgumentException
|
||||||
|
* MUST be thrown if $values is neither an array nor a Traversable,
|
||||||
|
* or if any of the $values are not a legal value.
|
||||||
*/
|
*/
|
||||||
protected function _setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
|
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
|
||||||
{
|
{
|
||||||
$this->validateKeys($values, TRUE);
|
$this->validateKeys($values, TRUE);
|
||||||
|
|
||||||
@ -298,8 +168,16 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes multiple cache items in a single operation.
|
* Deletes multiple cache items in a single operation.
|
||||||
|
*
|
||||||
|
* @param iterable $keys A list of string-based keys to be deleted.
|
||||||
|
*
|
||||||
|
* @return bool True if the items were successfully removed. False if there was an error.
|
||||||
|
*
|
||||||
|
* @throws SimpleCache\InvalidArgumentException
|
||||||
|
* MUST be thrown if $keys is neither an array nor a Traversable,
|
||||||
|
* or if any of the $keys are not a legal value.
|
||||||
*/
|
*/
|
||||||
protected function _deleteMultiple(iterable $keys): bool
|
public function deleteMultiple(iterable $keys): bool
|
||||||
{
|
{
|
||||||
$this->validateKeys($keys);
|
$this->validateKeys($keys);
|
||||||
|
|
||||||
@ -308,9 +186,20 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines whether an item is present in the cache.
|
* Determines whether an item is present in the cache.
|
||||||
* @throws Exception\InvalidArgumentException
|
*
|
||||||
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
|
||||||
|
* and not to be used within your live applications operations for get/set, as this method
|
||||||
|
* is subject to a race condition where your has() will return true and immediately after,
|
||||||
|
* another script can remove it making the state of your app out of date.
|
||||||
|
*
|
||||||
|
* @param string $key The cache item key.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @throws SimpleCache\InvalidArgumentException
|
||||||
|
* MUST be thrown if the $key string is not a legal value.
|
||||||
*/
|
*/
|
||||||
protected function _has(string $key): bool
|
public function has(string $key): bool
|
||||||
{
|
{
|
||||||
$this->validateKey($key);
|
$this->validateKey($key);
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
namespace Aviat\Banker\Tests;
|
namespace Aviat\Banker\Tests;
|
||||||
|
|
||||||
use Aviat\Banker\Pool;
|
|
||||||
use Aviat\Banker\Teller;
|
use Aviat\Banker\Teller;
|
||||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
||||||
use Monolog\Handler\SyslogHandler;
|
use Monolog\Handler\SyslogHandler;
|
||||||
@ -210,51 +209,6 @@ class TellerTest extends TestCase {
|
|||||||
array_walk($hasKeys, fn ($key) => $this->assertTrue($this->teller->has($key)));
|
array_walk($hasKeys, fn ($key) => $this->assertTrue($this->teller->has($key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testBadKeyType(): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$this->teller->get(546567);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBadKeyTypeSet(): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$this->teller->set(546567, 'foo');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBadKeyTypeDelete(): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$this->teller->delete(546567);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBadKeyTypeHas(): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$this->teller->has(546567);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBadKeysType (): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$keys = (object)[];
|
|
||||||
$this->teller->getMultiple($keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBadKeysTypeSet (): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$keys = (object)[];
|
|
||||||
$this->teller->setMultiple($keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBadKeysTypeDelete (): void
|
|
||||||
{
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
|
||||||
$keys = (object)[];
|
|
||||||
$this->teller->deleteMultiple($keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider keyValidationTests
|
* @dataProvider keyValidationTests
|
||||||
* @param string $key
|
* @param string $key
|
||||||
|
Loading…
Reference in New Issue
Block a user