| Total Complexity | 80 |
| Total Lines | 469 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ExtendedCacheItemPoolTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExtendedCacheItemPoolTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | trait ExtendedCacheItemPoolTrait |
||
| 31 | { |
||
| 32 | use CacheItemPoolTrait, AbstractDriverPoolTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @inheritdoc |
||
| 36 | */ |
||
| 37 | public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512): string |
||
| 38 | { |
||
| 39 | $callback = function (CacheItemInterface $item) { |
||
| 40 | return $item->get(); |
||
| 41 | }; |
||
| 42 | return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @inheritdoc |
||
| 47 | */ |
||
| 48 | public function getItemsByTag($tagName) |
||
| 49 | { |
||
| 50 | if (\is_string($tagName)) { |
||
| 51 | $driverResponse = $this->getItem($this->getTagKey($tagName)); |
||
|
|
|||
| 52 | if ($driverResponse->isHit()) { |
||
| 53 | $items = (array)$driverResponse->get(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * getItems() may provides expired item(s) |
||
| 57 | * themselves provided by a cache of item |
||
| 58 | * keys based stored the tag item. |
||
| 59 | * Therefore we pass a filter callback |
||
| 60 | * to remove the expired Item(s) provided by |
||
| 61 | * the item keys passed through getItems() |
||
| 62 | * |
||
| 63 | * #headache |
||
| 64 | */ |
||
| 65 | return \array_filter($this->getItems(\array_unique(\array_keys($items))), function (ExtendedCacheItemInterface $item) { |
||
| 66 | return $item->isHit(); |
||
| 67 | }); |
||
| 68 | } |
||
| 69 | return []; |
||
| 70 | } |
||
| 71 | |||
| 72 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | */ |
||
| 78 | public function getItemsByTags(array $tagNames) |
||
| 79 | { |
||
| 80 | $items = []; |
||
| 81 | foreach (\array_unique($tagNames) as $tagName) { |
||
| 82 | if (\is_string($tagName)) { |
||
| 83 | $items = \array_merge($items, $this->getItemsByTag($tagName)); |
||
| 84 | } else { |
||
| 85 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return $items; |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * @inheritdoc |
||
| 95 | */ |
||
| 96 | public function getItemsByTagsAll(array $tagNames) |
||
| 97 | { |
||
| 98 | $items = $this->getItemsByTags($tagNames); |
||
| 99 | |||
| 100 | foreach ($items as $key => $item) { |
||
| 101 | if (\array_diff($tagNames, $item->getTags())) { |
||
| 102 | unset($items[$key]); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return $items; |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * @inheritdoc |
||
| 112 | */ |
||
| 113 | public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
||
| 114 | { |
||
| 115 | $callback = function (CacheItemInterface $item) { |
||
| 116 | return $item->get(); |
||
| 117 | }; |
||
| 118 | |||
| 119 | return \json_encode(\array_map($callback, \array_values($this->getItemsByTags($tagNames))), $option, $depth); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | public function deleteItemsByTag($tagName) |
||
| 126 | { |
||
| 127 | if (\is_string($tagName)) { |
||
| 128 | $return = null; |
||
| 129 | foreach ($this->getItemsByTag($tagName) as $item) { |
||
| 130 | $result = $this->deleteItem($item->getKey()); |
||
| 131 | if ($return !== false) { |
||
| 132 | $return = $result; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return $return; |
||
| 137 | } |
||
| 138 | |||
| 139 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @inheritdoc |
||
| 144 | */ |
||
| 145 | public function deleteItemsByTags(array $tagNames) |
||
| 146 | { |
||
| 147 | $return = null; |
||
| 148 | foreach ($tagNames as $tagName) { |
||
| 149 | $result = $this->deleteItemsByTag($tagName); |
||
| 150 | if ($return !== false) { |
||
| 151 | $return = $result; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $return; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | public function deleteItemsByTagsAll(array $tagNames) |
||
| 162 | { |
||
| 163 | $return = null; |
||
| 164 | $items = $this->getItemsByTagsAll($tagNames); |
||
| 165 | |||
| 166 | foreach ($items as $key => $item) { |
||
| 167 | $result = $this->deleteItem($item->getKey()); |
||
| 168 | if ($return !== false) { |
||
| 169 | $return = $result; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | return $return; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | public function incrementItemsByTag($tagName, $step = 1) |
||
| 180 | { |
||
| 181 | if (\is_string($tagName) && \is_int($step)) { |
||
| 182 | foreach ($this->getItemsByTag($tagName) as $item) { |
||
| 183 | $item->increment($step); |
||
| 184 | $this->saveDeferred($item); |
||
| 185 | } |
||
| 186 | |||
| 187 | return $this->commit(); |
||
| 188 | } |
||
| 189 | |||
| 190 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | public function incrementItemsByTags(array $tagNames, $step = 1) |
||
| 197 | { |
||
| 198 | $return = null; |
||
| 199 | foreach ($tagNames as $tagName) { |
||
| 200 | $result = $this->incrementItemsByTag($tagName, $step); |
||
| 201 | if ($return !== false) { |
||
| 202 | $return = $result; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | return $return; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @inheritdoc |
||
| 211 | */ |
||
| 212 | public function incrementItemsByTagsAll(array $tagNames, $step = 1) |
||
| 213 | { |
||
| 214 | if (\is_int($step)) { |
||
| 215 | $items = $this->getItemsByTagsAll($tagNames); |
||
| 216 | |||
| 217 | foreach ($items as $key => $item) { |
||
| 218 | $item->increment($step); |
||
| 219 | $this->saveDeferred($item); |
||
| 220 | } |
||
| 221 | return $this->commit(); |
||
| 222 | } |
||
| 223 | |||
| 224 | throw new PhpfastcacheInvalidArgumentException('$step must be an integer'); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritdoc |
||
| 229 | */ |
||
| 230 | public function decrementItemsByTag($tagName, $step = 1) |
||
| 231 | { |
||
| 232 | if (\is_string($tagName) && \is_int($step)) { |
||
| 233 | foreach ($this->getItemsByTag($tagName) as $item) { |
||
| 234 | $item->decrement($step); |
||
| 235 | $this->saveDeferred($item); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $this->commit(); |
||
| 239 | } |
||
| 240 | |||
| 241 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritdoc |
||
| 246 | */ |
||
| 247 | public function decrementItemsByTags(array $tagNames, $step = 1) |
||
| 248 | { |
||
| 249 | $return = null; |
||
| 250 | foreach ($tagNames as $tagName) { |
||
| 251 | $result = $this->decrementItemsByTag($tagName, $step); |
||
| 252 | if ($return !== false) { |
||
| 253 | $return = $result; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | return $return; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @inheritdoc |
||
| 262 | */ |
||
| 263 | public function decrementItemsByTagsAll(array $tagNames, $step = 1) |
||
| 264 | { |
||
| 265 | if (\is_int($step)) { |
||
| 266 | $items = $this->getItemsByTagsAll($tagNames); |
||
| 267 | |||
| 268 | foreach ($items as $key => $item) { |
||
| 269 | $item->decrement($step); |
||
| 270 | $this->saveDeferred($item); |
||
| 271 | } |
||
| 272 | return $this->commit(); |
||
| 273 | } |
||
| 274 | |||
| 275 | throw new PhpfastcacheInvalidArgumentException('$step must be an integer'); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritdoc |
||
| 280 | */ |
||
| 281 | public function appendItemsByTag($tagName, $data) |
||
| 282 | { |
||
| 283 | if (\is_string($tagName)) { |
||
| 284 | foreach ($this->getItemsByTag($tagName) as $item) { |
||
| 285 | $item->append($data); |
||
| 286 | $this->saveDeferred($item); |
||
| 287 | } |
||
| 288 | |||
| 289 | return $this->commit(); |
||
| 290 | } |
||
| 291 | |||
| 292 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @inheritdoc |
||
| 297 | */ |
||
| 298 | public function appendItemsByTags(array $tagNames, $data) |
||
| 299 | { |
||
| 300 | $return = null; |
||
| 301 | foreach ($tagNames as $tagName) { |
||
| 302 | $result = $this->appendItemsByTag($tagName, $data); |
||
| 303 | if ($return !== false) { |
||
| 304 | $return = $result; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return $return; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @inheritdoc |
||
| 313 | */ |
||
| 314 | public function appendItemsByTagsAll(array $tagNames, $data) |
||
| 315 | { |
||
| 316 | if (is_scalar($data)) { |
||
| 317 | $items = $this->getItemsByTagsAll($tagNames); |
||
| 318 | |||
| 319 | foreach ($items as $key => $item) { |
||
| 320 | $item->append($data); |
||
| 321 | $this->saveDeferred($item); |
||
| 322 | } |
||
| 323 | return $this->commit(); |
||
| 324 | } |
||
| 325 | |||
| 326 | throw new PhpfastcacheInvalidArgumentException('$data must be scalar'); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @inheritdoc |
||
| 331 | */ |
||
| 332 | public function prependItemsByTag($tagName, $data) |
||
| 333 | { |
||
| 334 | if (\is_string($tagName)) { |
||
| 335 | foreach ($this->getItemsByTag($tagName) as $item) { |
||
| 336 | $item->prepend($data); |
||
| 337 | $this->saveDeferred($item); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $this->commit(); |
||
| 341 | } |
||
| 342 | |||
| 343 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @inheritdoc |
||
| 348 | */ |
||
| 349 | public function prependItemsByTags(array $tagNames, $data) |
||
| 350 | { |
||
| 351 | $return = null; |
||
| 352 | foreach ($tagNames as $tagName) { |
||
| 353 | $result = $this->prependItemsByTag($tagName, $data); |
||
| 354 | if ($return !== false) { |
||
| 355 | $return = $result; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | return $return; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @inheritdoc |
||
| 364 | */ |
||
| 365 | public function prependItemsByTagsAll(array $tagNames, $data) |
||
| 366 | { |
||
| 367 | if (\is_scalar($data)) { |
||
| 368 | $items = $this->getItemsByTagsAll($tagNames); |
||
| 369 | |||
| 370 | foreach ($items as $key => $item) { |
||
| 371 | $item->prepend($data); |
||
| 372 | $this->saveDeferred($item); |
||
| 373 | } |
||
| 374 | return $this->commit(); |
||
| 375 | } |
||
| 376 | |||
| 377 | throw new PhpfastcacheInvalidArgumentException('$data must be scalar'); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | public function detachItem(CacheItemInterface $item) |
||
| 385 | { |
||
| 386 | if (isset($this->itemInstances[$item->getKey()])) { |
||
| 387 | $this->deregisterItem($item->getKey()); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @inheritdoc |
||
| 393 | */ |
||
| 394 | public function detachAllItems() |
||
| 395 | { |
||
| 396 | foreach ($this->itemInstances as $item) { |
||
| 397 | $this->detachItem($item); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @inheritdoc |
||
| 403 | */ |
||
| 404 | public function attachItem(CacheItemInterface $item) |
||
| 405 | { |
||
| 406 | if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
||
| 407 | throw new PhpfastcacheLogicException('The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.'); |
||
| 408 | } |
||
| 409 | |||
| 410 | $this->itemInstances[$item->getKey()] = $item; |
||
| 411 | } |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @internal This method de-register an item from $this->itemInstances |
||
| 416 | * @param string $item |
||
| 417 | */ |
||
| 418 | protected function deregisterItem(string $item) |
||
| 419 | { |
||
| 420 | unset($this->itemInstances[$item]); |
||
| 421 | |||
| 422 | if (\gc_enabled()) { |
||
| 423 | \gc_collect_cycles(); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param ExtendedCacheItemInterface $item |
||
| 429 | */ |
||
| 430 | protected function cleanItemTags(ExtendedCacheItemInterface $item) |
||
| 431 | { |
||
| 432 | $this->driverWriteTags($item->removeTags($item->getTags())); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Returns true if the item exists, is attached and the Spl Hash matches |
||
| 437 | * Returns false if the item exists, is attached and the Spl Hash mismatches |
||
| 438 | * Returns null if the item does not exists |
||
| 439 | * |
||
| 440 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 441 | * @return bool|null |
||
| 442 | */ |
||
| 443 | public function isAttached(CacheItemInterface $item) |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Set the EventManager instance |
||
| 453 | * |
||
| 454 | * @param EventInterface $em |
||
| 455 | * @return ExtendedCacheItemPoolInterface |
||
| 456 | */ |
||
| 457 | public function setEventManager(EventInterface $em): ExtendedCacheItemPoolInterface |
||
| 458 | { |
||
| 459 | $this->eventManager = $em; |
||
| 460 | |||
| 461 | return $this; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @inheritdoc |
||
| 466 | */ |
||
| 467 | public function saveMultiple(...$items): bool |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @inheritdoc |
||
| 487 | */ |
||
| 488 | public static function isUsableInAutoContext(): bool |
||
| 489 | { |
||
| 490 | return true; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function getHelp(): string |
||
| 497 | { |
||
| 499 | } |
||
| 500 | } |