Completed
Push — master ( 4e7427...4a94bb )
by Georges
12s
created

ExtendedCacheItemPoolTrait   F

Complexity

Total Complexity 80

Size/Duplication

Total Lines 469
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 469
rs 2
c 0
b 0
f 0
wmc 80

30 Methods

Rating   Name   Duplication   Size   Complexity  
A isAttached() 0 6 2
A deleteItemsByTagsAll() 0 13 3
A getItemsByTagsAsJsonString() 0 7 1
A incrementItemsByTags() 0 11 3
A incrementItemsByTag() 0 12 4
A prependItemsByTagsAll() 0 13 3
A incrementItemsByTagsAll() 0 13 3
A decrementItemsByTag() 0 12 4
A decrementItemsByTags() 0 11 3
A deleteItemsByTag() 0 15 4
A deregisterItem() 0 6 2
A getItemsByTag() 0 25 3
A prependItemsByTag() 0 12 3
A saveMultiple() 0 16 6
A getItemsByTagsAll() 0 11 3
A deleteItemsByTags() 0 11 3
A appendItemsByTags() 0 11 3
A decrementItemsByTagsAll() 0 13 3
A cleanItemTags() 0 3 1
A detachAllItems() 0 4 2
A prependItemsByTags() 0 11 3
A appendItemsByTag() 0 12 3
A getItemsByTags() 0 12 3
A setEventManager() 0 5 1
A isUsableInAutoContext() 0 3 1
A attachItem() 0 7 3
A appendItemsByTagsAll() 0 13 3
A detachItem() 0 4 2
A getHelp() 0 3 1
A getItemsAsJsonString() 0 6 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Core\Pool;
17
18
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
19
use Phpfastcache\Event\EventInterface;
20
use Phpfastcache\Exceptions\{
21
    PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException
22
};
23
use Psr\Cache\CacheItemInterface;
24
25
/**
26
 * Trait ExtendedCacheItemPoolTrait
27
 * @package Phpfastcache\Core\Pool
28
 * @method bool driverWriteTags(ExtendedCacheItemInterface $item) Imported from DriverBaseTrait
29
 */
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));
0 ignored issues
show
Bug introduced by
It seems like getTagKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            $driverResponse = $this->getItem($this->/** @scrutinizer ignore-call */ getTagKey($tagName));
Loading history...
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)
444
    {
445
        if (isset($this->itemInstances[$item->getKey()])) {
446
            return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]);
447
        }
448
        return null;
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Phpfastcache\Core\Pool\ExtendedCacheItemPoolTrait which is incompatible with the type-hinted return Phpfastcache\Core\Pool\E...dCacheItemPoolInterface.
Loading history...
462
    }
463
464
    /**
465
     * @inheritdoc
466
     */
467
    public function saveMultiple(...$items): bool
468
    {
469
        if (isset($items[0]) && \is_array($items[0])) {
470
            foreach ($items[0] as $item) {
471
                $this->save($item);
472
            }
473
            return true;
474
        }
475
476
        if (\is_array($items)) {
477
            foreach ($items as $item) {
478
                $this->save($item);
479
            }
480
            return true;
481
        }
482
        return false;
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
    {
498
        return '';
499
    }
500
}