CacheDriverAbstract::checkKey()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 2
nop 1
1
<?php
2
/**
3
 * Class CacheDriverAbstract
4
 *
5
 * @filesource   CacheDriverAbstract.php
6
 * @created      25.05.2017
7
 * @package      chillerlan\SimpleCache
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 *
12
 * @phan-file-suppress PhanTypeInvalidThrowsIsInterface
13
 */
14
15
namespace chillerlan\SimpleCache;
16
17
use chillerlan\Settings\SettingsContainerInterface;
18
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
19
use Psr\SimpleCache\CacheInterface;
20
use DateInterval, DateTime, Traversable;
21
22
use function  is_array, is_int, is_string, iterator_to_array, time;
23
24
abstract class CacheDriverAbstract implements CacheInterface, LoggerAwareInterface{
25
	use LoggerAwareTrait;
26
27
	/**
28
	 * @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\SimpleCache\CacheOptions
29
	 */
30
	protected SettingsContainerInterface $options;
31
32
	/**
33
	 * CacheDriverAbstract constructor.
34
	 */
35
	public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){
36
		$this->options = $options ?? new CacheOptions;
37
		$this->logger  = $logger ?? new NullLogger;
38
	}
39
40
	/** @inheritdoc */
41
	public function has($key):bool{
42
		return $this->get($key) !== null;
43
	}
44
45
	/** @inheritdoc */
46
	public function getMultiple($keys, $default = null):array{
47
		$data = [];
48
49
		foreach($this->getData($keys) as $key){
50
			$data[$key] = $this->get($key, $default);
51
		}
52
53
		return $data;
54
	}
55
56
	/** @inheritdoc */
57
	public function setMultiple($values, $ttl = null):bool{
58
		$return = [];
59
60
		foreach($this->getData($values) as $key => $value){
61
			$return[] = $this->set($key, $value, $ttl);
62
		}
63
64
		return $this->checkReturn($return);
65
	}
66
67
	/** @inheritdoc */
68
	public function deleteMultiple($keys):bool{
69
		$return = [];
70
71
		foreach($this->getData($keys) as $key){
72
			$return[] = $this->delete($key);
73
		}
74
75
		return $this->checkReturn($return);
76
	}
77
78
	/**
79
	 * @param string|mixed $key
80
	 *
81
	 * @return string
82
	 * @throws \Psr\SimpleCache\InvalidArgumentException
83
	 */
84
	protected function checkKey($key):string{
85
86
		if(!is_string($key) || empty($key)){
87
			throw new InvalidArgumentException('invalid cache key: "'.$key.'"');
88
		}
89
90
		return $key;
91
	}
92
93
	/**
94
	 * @param array $keys
95
	 *
96
	 * @return void
97
	 */
98
	protected function checkKeyArray(array $keys):void{
99
100
		foreach($keys as $key){
101
			$this->checkKey($key);
102
		}
103
104
	}
105
106
	/**
107
	 * @param mixed $data
108
	 *
109
	 * @return array
110
	 * @throws \Psr\SimpleCache\InvalidArgumentException
111
	 */
112
	protected function getData($data):array{
113
114
		if(is_array($data)){
115
			return $data;
116
		}
117
		elseif($data instanceof Traversable){
118
			return iterator_to_array($data); // @codeCoverageIgnore
119
		}
120
121
		throw new InvalidArgumentException('invalid data');
122
	}
123
124
	/**
125
	 * @param mixed $ttl
126
	 *
127
	 * @return int|null
128
	 * @throws \Psr\SimpleCache\InvalidArgumentException
129
	 */
130
	protected function getTTL($ttl):?int{
131
132
		if($ttl instanceof DateInterval){
133
			return (new DateTime)->add($ttl)->getTimeStamp() - time();
134
		}
135
		else if((is_int($ttl) && $ttl > 0) || $ttl === null){
136
			return $ttl;
137
		}
138
139
		throw new InvalidArgumentException('invalid ttl');
140
	}
141
142
	/**
143
	 * @param bool[] $booleans
144
	 *
145
	 * @return bool
146
	 */
147
	protected function checkReturn(array $booleans):bool{
148
149
		foreach($booleans as $boolean){
150
151
			if(!(bool)$boolean){
152
				return false; // @codeCoverageIgnore
153
			}
154
155
		}
156
157
		return true;
158
	}
159
160
}
161