ExtendedCacheItemPoolTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 104
rs 10
c 2
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A throwUnsupportedDriverReadAllPattern() 0 7 2
A saveMultiple() 0 11 3
A detachAllItems() 0 7 2
A getAllItems() 0 16 3
A getHelp() 0 3 1
A detachItem() 0 7 2
A getItemsAsJsonString() 0 9 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Core\Pool;
18
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Event\Event;
21
use Phpfastcache\Event\EventReferenceParameter;
22
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
23
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
24
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
25
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
26
use Psr\Cache\CacheItemInterface;
27
28
trait ExtendedCacheItemPoolTrait
29
{
30
    use CacheItemPoolTrait;
0 ignored issues
show
Bug introduced by
The type Phpfastcache\Core\Pool\CacheItemPoolTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
    use AggregatablePoolTrait;
32
33
    /**
34
     * @inheritDoc
35
     * @param string $pattern
36
     * @return array<string, mixed>
37
     */
38
    public function getAllItems(string $pattern = ''): iterable
39
    {
40
        $driverReadAllKeysCallback = fn (string $pattern): iterable => $this->driverReadAllKeys($pattern);
41
42
        /**
43
         * This event allow you to customize the callback and wrap it to an invoker
44
         * like SebastianBergmann\Invoke\Invoke, so you can set up custom timeouts.
45
         */
46
        $this->eventManager->dispatch(Event::CACHE_GET_ALL_ITEMS, $this, new EventReferenceParameter($driverReadAllKeysCallback));
47
        $keys = $driverReadAllKeysCallback($pattern);
48
49
        if (count($keys) > 0) {
50
            return $this->getItems($keys instanceof \Traversable ? iterator_to_array($keys) : $keys);
51
        }
52
53
        return [];
54
    }
55
56
    /**
57
     * @inheritDoc
58
     * @throws PhpfastcacheCoreException
59
     * @throws PhpfastcacheDriverException
60
     * @throws PhpfastcacheInvalidArgumentException
61
     * @throws PhpfastcacheLogicException
62
     */
63
    public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string
64
    {
65
        return \json_encode(
66
            \array_map(
67
                static fn(CacheItemInterface $item) => $item->get(),
68
                \array_values($this->getItems($keys))
69
            ),
70
            $options,
71
            $depth
72
        );
73
    }
74
75
    public function detachAllItems(): static
76
    {
77
        foreach ($this->itemInstances as $item) {
78
            $this->detachItem($item);
79
        }
80
81
        return $this;
82
    }
83
84
    public function detachItem(CacheItemInterface $item): static
85
    {
86
        if (isset($this->itemInstances[$item->getKey()])) {
87
            $this->deregisterItem($item->getKey());
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param ExtendedCacheItemInterface ...$items
95
     * @return bool
96
     * @throws PhpfastcacheCoreException
97
     * @throws PhpfastcacheDriverException
98
     * @throws PhpfastcacheInvalidArgumentException
99
     * @throws PhpfastcacheLogicException
100
     */
101
    public function saveMultiple(ExtendedCacheItemInterface ...$items): bool
102
    {
103
        $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items));
104
105
        if (\count($items)) {
106
            foreach ($items as $item) {
107
                $this->save($item);
108
            }
109
            return true;
110
        }
111
        return false;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getHelp(): string
118
    {
119
        return '';
120
    }
121
122
    /**
123
     * @throws PhpfastcacheInvalidArgumentException
124
     */
125
    public function throwUnsupportedDriverReadAllPattern(string $linkReference = ''): void
126
    {
127
        throw new PhpfastcacheInvalidArgumentException(
128
            sprintf(
129
                '%s does not support a pattern argument.%s',
130
                $this->getDriverName(),
131
                $linkReference ? " See $linkReference" : ''
132
            )
133
        );
134
    }
135
}
136