Completed
Push — master ( 94f5e8...a88b9e )
by Lars
01:57 queued 11s
created

CacheChain::existsItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.072
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
class CacheChain implements iCache
8
{
9
    /**
10
     * @var Cache[]
11
     */
12
    private $caches = [];
13
14
    /**
15
     * __construct
16
     *
17
     * @param array $caches
18
     */
19 9
    public function __construct(array $caches = [])
20
    {
21 9
        \array_map(
22
            [
23 9
                $this,
24 9
                'addCache',
25
            ],
26 9
            $caches
27
        );
28 9
    }
29
30
    /**
31
     * get caches
32
     *
33
     * @return array
34
     */
35
    public function getCaches(): array
36
    {
37
        return $this->caches;
38
    }
39
40
    /**
41
     * add cache
42
     *
43
     * @param Cache $cache
44
     * @param bool  $prepend
45
     */
46 9
    public function addCache(Cache $cache, $prepend = true)
47
    {
48 9
        if ($prepend) {
49 9
            \array_unshift($this->caches, $cache);
50
        } else {
51
            $this->caches[] = $cache;
52
        }
53 9
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 6
    public function getItem(string $key)
59
    {
60 6
        foreach ($this->caches as $cache) {
61 6
            if ($cache->existsItem($key)) {
62 6
                return $cache->getItem($key);
63
            }
64
        }
65
66 3
        return null;
67
    }
68
69
    /**
70
     * Get the "isReady" state.
71
     *
72
     * @return bool
73
     */
74 1
    public function getCacheIsReady(): bool
75
    {
76 1
        foreach ($this->caches as $cache) {
77 1
            if (!$cache->getCacheIsReady()) {
78
                return false;
79
            }
80
        }
81
82 1
        return true;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 3
    public function setItem(string $key, $value, $ttl = null): bool
89
    {
90
        // init
91 3
        $results = [];
92
93 3
        foreach ($this->caches as $cache) {
94 3
            $results[] = $cache->setItem($key, $value, $ttl);
95
        }
96
97 3
        return \in_array(true, $results, true);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 3
    public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool
104
    {
105
        // init
106 3
        $results = [];
107
108 3
        foreach ($this->caches as $cache) {
109 3
            $results[] = $cache->setItemToDate($key, $value, $date);
110
        }
111
112 3
        return \in_array(true, $results, true);
113
    }
114
115
    /**
116
     * !!! Set the prefix. !!!
117
     *
118
     * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix.
119
     *
120
     * @param string $prefix
121
     */
122 1
    public function setPrefix(string $prefix)
123
    {
124 1
        foreach ($this->caches as $cache) {
125 1
            $cache->setPrefix($prefix);
126
        }
127 1
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1 View Code Duplication
    public function removeItem(string $key): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        // init
135 1
        $results = [];
136
137 1
        foreach ($this->caches as $cache) {
138 1
            $results[] = $cache->removeItem($key);
139
        }
140
141 1
        return \in_array(true, $results, true);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function existsItem(string $key): bool
148
    {
149 1
        foreach ($this->caches as $cache) {
150 1
            if ($cache->existsItem($key)) {
151 1
                return true;
152
            }
153
        }
154
155
        return false;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1 View Code Duplication
    public function removeAll(): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        // init
164 1
        $results = [];
165
166 1
        foreach ($this->caches as $cache) {
167 1
            $results[] = $cache->removeAll();
168
        }
169
170 1
        return \in_array(true, $results, true);
171
    }
172
}
173