1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * TechDivision\Import\Cache\Collection\ConfigurableCacheAdapter |
||||
5 | * |
||||
6 | * PHP version 7 |
||||
7 | * |
||||
8 | * @author Tim Wagner <[email protected]> |
||||
9 | * @copyright 2021 TechDivision GmbH <[email protected]> |
||||
10 | * @license https://opensource.org/licenses/MIT |
||||
11 | * @link https://github.com/techdivision/import-cache-collection |
||||
12 | * @link http://www.techdivision.com |
||||
13 | */ |
||||
14 | |||||
15 | namespace TechDivision\Import\Cache\Collection; |
||||
16 | |||||
17 | use TechDivision\Import\Cache\Utils\CacheTypes; |
||||
18 | use TechDivision\Import\Cache\CacheAdapterInterface; |
||||
19 | use TechDivision\Import\Cache\Configuration\ConfigurationInterface; |
||||
20 | |||||
21 | /** |
||||
22 | * Configurable cache adapter implementation. |
||||
23 | * |
||||
24 | * @author Tim Wagner <[email protected]> |
||||
25 | * @copyright 2021 TechDivision GmbH <[email protected]> |
||||
26 | * @license https://opensource.org/licenses/MIT |
||||
27 | * @link https://github.com/techdivision/import-cache-collection |
||||
28 | * @link http://www.techdivision.com |
||||
29 | */ |
||||
30 | class ConfigurableCacheAdapter implements CacheAdapterInterface |
||||
31 | { |
||||
32 | |||||
33 | /** |
||||
34 | * The cache adatper instance. |
||||
35 | * |
||||
36 | * @var \TechDivision\Import\Cache\CacheAdapterInterface |
||||
37 | */ |
||||
38 | protected $cacheAdapter; |
||||
39 | |||||
40 | /** |
||||
41 | * The TTL used to cache items. |
||||
42 | * |
||||
43 | * @var integer |
||||
44 | */ |
||||
45 | protected $time = null; |
||||
46 | |||||
47 | /** |
||||
48 | * The flag if the cache is anabled or not. |
||||
49 | * |
||||
50 | * @var boolean |
||||
51 | */ |
||||
52 | protected $enabled = true; |
||||
53 | |||||
54 | /** |
||||
55 | * The array with the default tags. |
||||
56 | * |
||||
57 | * @var string |
||||
58 | */ |
||||
59 | protected $tags = array(); |
||||
60 | |||||
61 | /** |
||||
62 | * Initialize the cache handler with the passed cache and configuration instances. |
||||
63 | * . |
||||
64 | * @param \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter The cache instance |
||||
65 | * @param \TechDivision\Import\Cache\Configuration\ConfigurationInterface $configuration The configuration instance |
||||
66 | * @param string $type The cache type to use |
||||
67 | */ |
||||
68 | public function __construct( |
||||
69 | CacheAdapterInterface $cacheAdapter, |
||||
70 | ConfigurationInterface $configuration, |
||||
71 | $type = CacheTypes::TYPE_STATIC |
||||
72 | ) { |
||||
73 | |||||
74 | // set the cache adapter to use |
||||
75 | $this->cacheAdapter = $cacheAdapter; |
||||
76 | |||||
77 | // append the serial to the array with the default tags |
||||
78 | $this->tags = array_merge(array($configuration->getSerial())); |
||||
0 ignored issues
–
show
|
|||||
79 | |||||
80 | // load the configuration for the passed cache type |
||||
81 | if ($cacheConfiguration = $configuration->getCacheByType($type)) { |
||||
82 | // initialize the ttl and the enabled flag |
||||
83 | $this->time = $cacheConfiguration->getTime(); |
||||
84 | $this->enabled = $cacheConfiguration->isEnabled(); |
||||
85 | } else { |
||||
86 | $this->enabled = ($configuration->isCacheEnabled() || $type === CacheTypes::TYPE_STATIC); |
||||
87 | } |
||||
88 | } |
||||
89 | |||||
90 | /** |
||||
91 | * Creates a unique cache key from the passed data. |
||||
92 | * |
||||
93 | * @param mixed $data The date to create the cache key from |
||||
94 | * @param boolean $usePrefix Flag to signal using the prefix or not |
||||
95 | * |
||||
96 | * @return string The generated cache key |
||||
97 | */ |
||||
98 | public function cacheKey($data, $usePrefix = true) |
||||
99 | { |
||||
100 | return $this->cacheAdapter->cacheKey($data, $usePrefix); |
||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Inversion of the isCached() method. |
||||
105 | * |
||||
106 | * @param string $key The cache key to query for |
||||
107 | * |
||||
108 | * @return boolean TRUE if the value is not available, else FALSE |
||||
109 | */ |
||||
110 | public function notCached($key) |
||||
111 | { |
||||
112 | return $this->cacheAdapter->notCached($key); |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * Add's a cache reference from one key to another. |
||||
117 | * |
||||
118 | * @param string $from The key to reference from |
||||
119 | * @param string $to The key to reference to |
||||
120 | * |
||||
121 | * @return void |
||||
122 | */ |
||||
123 | public function addReference($from, $to) |
||||
124 | { |
||||
125 | $this->cacheAdapter->addReference($from, $to); |
||||
126 | } |
||||
127 | |||||
128 | /** |
||||
129 | * Returns a new cache item for the passed key |
||||
130 | * |
||||
131 | * @param string $key The cache key to return the item for |
||||
132 | * |
||||
133 | * @return mixed The value for the passed key |
||||
134 | */ |
||||
135 | public function fromCache($key) |
||||
136 | { |
||||
137 | return $this->cacheAdapter->fromCache($key); |
||||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * Flush the cache and remove the references. |
||||
142 | * |
||||
143 | * @return void |
||||
144 | */ |
||||
145 | public function flushCache() |
||||
146 | { |
||||
147 | $this->cacheAdapter->flushCache(); |
||||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * Invalidate the cache entries for the passed tags. |
||||
152 | * |
||||
153 | * @param array $tags The tags to invalidate the cache for |
||||
154 | * |
||||
155 | * @return void |
||||
156 | */ |
||||
157 | public function invalidateTags(array $tags) |
||||
158 | { |
||||
159 | $this->cacheAdapter->invalidateTags($tags); |
||||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * Remove the item with the passed key and all its references from the cache. |
||||
164 | * |
||||
165 | * @param string $key The key of the cache item to Remove |
||||
166 | * @param bool $cleanUpReferences TRUE if the references has to be cleaned-up, else FALSE (default) |
||||
167 | * |
||||
168 | * @return void |
||||
169 | */ |
||||
170 | public function removeCache($key, $cleanUpReferences = false) |
||||
171 | { |
||||
172 | $this->cacheAdapter->removeCache($key, $cleanUpReferences); |
||||
173 | } |
||||
174 | |||||
175 | /** |
||||
176 | * Raises the value for the attribute with the passed key by one. |
||||
177 | * |
||||
178 | * @param mixed $key The key of the attribute to raise the value for |
||||
179 | * @param mixed $counterName The name of the counter to raise |
||||
180 | * |
||||
181 | * @return integer The counter's new value |
||||
182 | */ |
||||
183 | public function raiseCounter($key, $counterName) |
||||
184 | { |
||||
185 | return $this->cacheAdapter->raiseCounter($key, $counterName); |
||||
186 | } |
||||
187 | |||||
188 | /** |
||||
189 | * Lowers the value for the attribute with the passed key by one. |
||||
190 | * |
||||
191 | * @param mixed $key The key of the attribute to lower the value for |
||||
192 | * @param mixed $counterName The name of the counter to lower |
||||
193 | * |
||||
194 | * @return integer The counter's new value |
||||
195 | */ |
||||
196 | public function lowerCounter($key, $counterName) |
||||
197 | { |
||||
198 | return $this->cacheAdapter->lowerCounter($key, $counterName); |
||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * This method merges the passed attributes with an array that |
||||
203 | * has already been added under the passed key. |
||||
204 | * |
||||
205 | * If no value will be found under the passed key, the attributes |
||||
206 | * will simply be registered. |
||||
207 | * |
||||
208 | * @param mixed $key The key of the attributes that has to be merged with the passed ones |
||||
209 | * @param array $attributes The attributes that has to be merged with the exising ones |
||||
210 | * |
||||
211 | * @return void |
||||
212 | * @throws \Exception Is thrown, if the already registered value is no array |
||||
213 | * @link http://php.net/array_replace_recursive |
||||
214 | */ |
||||
215 | public function mergeAttributesRecursive($key, array $attributes) |
||||
216 | { |
||||
217 | $this->cacheAdapter->mergeAttributesRecursive($key, $attributes); |
||||
218 | } |
||||
219 | |||||
220 | /** |
||||
221 | * Query whether or not a cache value for the passed cache key is available. |
||||
222 | * |
||||
223 | * @param string $key The cache key to query for |
||||
224 | * |
||||
225 | * @return boolean TRUE if the a value is available, else FALSE |
||||
226 | */ |
||||
227 | public function isCached($key) |
||||
228 | { |
||||
229 | |||||
230 | // query whether or not the item has been cached, and if yes if the cache is valid |
||||
231 | if ($this->enabled) { |
||||
232 | return $this->cacheAdapter->isCached($key); |
||||
233 | } |
||||
234 | |||||
235 | // return FALSE in all other cases |
||||
236 | return false; |
||||
237 | } |
||||
238 | |||||
239 | /** |
||||
240 | * Add the passed item to the cache. |
||||
241 | * |
||||
242 | * @param string $key The cache key to use |
||||
243 | * @param mixed $value The value that has to be cached |
||||
244 | * @param array $references An array with references to add |
||||
245 | * @param array $tags An array with tags to add |
||||
246 | * @param boolean $override Flag that allows to override an exising cache entry |
||||
247 | * @param integer $time The TTL in seconds for the passed item |
||||
248 | * |
||||
249 | * @return void |
||||
250 | */ |
||||
251 | public function toCache($key, $value, array $references = array(), array $tags = array(), $override = true, $time = null) |
||||
252 | { |
||||
253 | |||||
254 | // query whether or not the cache is enabled |
||||
255 | if ($this->enabled) { |
||||
256 | $this->cacheAdapter->toCache($key, $value, $references, array_merge($this->tags, $tags), $override, $time ? $time : $this->time); |
||||
0 ignored issues
–
show
$this->tags of type string is incompatible with the type array expected by parameter $arrays of array_merge() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
257 | } |
||||
258 | } |
||||
259 | } |
||||
260 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..