Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
CRAP | |
41.18% |
14 / 34 |
| AssertTrait | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
53.89 | |
41.18% |
14 / 34 |
| assertArgumentString | |
0.00% |
0 / 1 |
3.19 | |
33.33% |
2 / 6 |
|||
| assertArgumentIterable | |
0.00% |
0 / 1 |
3.19 | |
33.33% |
2 / 6 |
|||
| assertValidTypeOfTtl | |
0.00% |
0 / 1 |
6.00 | |
50.00% |
4 / 8 |
|||
| assertDirectoryWritable | |
0.00% |
0 / 1 |
4.68 | |
42.86% |
3 / 7 |
|||
| assertSettingFields | |
0.00% |
0 / 1 |
4.68 | |
42.86% |
3 / 7 |
|||
| <?php | |
| /* | |
| * This file is part of the Shieldon Simple Cache package. | |
| * | |
| * (c) Terry L. <contact@terryl.in> | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| declare(strict_types=1); | |
| namespace Shieldon\SimpleCache; | |
| use Shieldon\SimpleCache\Exception\CacheArgumentException; | |
| use Shieldon\SimpleCache\Exception\CacheException; | |
| use DateInterval; | |
| use function gettype; | |
| /** | |
| * This assert trait provides methods to check conditions. | |
| */ | |
| Trait AssertTrait | |
| { | |
| /** | |
| * Check if the variable is string or not. | |
| * | |
| * @param string $var The variable will be checked. | |
| * | |
| * @return void | |
| * | |
| * @throws CacheArgumentException | |
| */ | |
| protected function assertArgumentString($value): void | |
| { | |
| if (!is_string($value)) { | |
| throw new CacheArgumentException( | |
| sprintf( | |
| 'The type of value must be string, but "%s" provided.', | |
| gettype($value) | |
| ) | |
| ); | |
| } | |
| } | |
| /** | |
| * Check if the variable is iterable or not. | |
| * | |
| * @param iterable $value The variable will be checked. | |
| * | |
| * @return void | |
| * | |
| * @throws CacheArgumentException | |
| */ | |
| protected function assertArgumentIterable($value): void | |
| { | |
| if (!is_iterable($value)) { | |
| throw new CacheArgumentException( | |
| sprintf( | |
| 'The type of value must be iterable, but "%s" provided.', | |
| gettype($value) | |
| ) | |
| ); | |
| } | |
| } | |
| /** | |
| * Check if the TTL is valid type or not. | |
| * | |
| * @param int|null|DateInterval $ttl The time to live of a cached data. | |
| * | |
| * @return void | |
| * | |
| * @throws CacheArgumentException | |
| */ | |
| protected function assertValidTypeOfTtl($ttl): void | |
| { | |
| if ( | |
| !is_null($ttl) && | |
| !is_integer($ttl) && | |
| !($ttl instanceof DateInterval) | |
| ) { | |
| throw new CacheArgumentException( | |
| sprintf( | |
| 'The TTL only accetps int, null and DateInterval instance, but "%s" provided.', | |
| gettype($ttl) | |
| ) | |
| ); | |
| } | |
| } | |
| /** | |
| * Check if a directory exists and is writable. | |
| * | |
| * @param string $directory The path of a directory. | |
| * | |
| * @return void | |
| * | |
| * @throws CacheException | |
| */ | |
| protected function assertDirectoryWritable(string $directory): void | |
| { | |
| if (!is_dir($directory)) { | |
| throw new CacheException( | |
| 'The directory of the storage does not exist.' | |
| ); | |
| } | |
| if (!is_writable($directory)) { | |
| throw new CacheException( | |
| 'The directory of the storage must be wriable' | |
| ); | |
| } | |
| } | |
| /** | |
| * Check if a setting field is empty or not. | |
| * | |
| * @param array $settings The array of the settings. | |
| * | |
| * @return void | |
| * | |
| * @throws CacheArgumentException | |
| */ | |
| protected function assertSettingFields($settings): void | |
| { | |
| foreach ($settings as $k => $v) { | |
| if (empty($v)) { | |
| throw new CacheArgumentException( | |
| sprintf( | |
| 'The setting field "%s" cannot be empty or null', | |
| $k | |
| ) | |
| ); | |
| } | |
| } | |
| } | |
| } |