Completed
Push — master ( 325a8a...fd15a5 )
by Lars
01:40
created

CacheChain::getCacheIsReady()   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 0
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
     * @return void
47
     */
48 9
    public function addCache(Cache $cache, $prepend = true)
49
    {
50 9
        if ($prepend) {
51 9
            \array_unshift($this->caches, $cache);
52
        } else {
53
            $this->caches[] = $cache;
54
        }
55 9
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 6
    public function getItem(string $key)
61
    {
62 6
        foreach ($this->caches as $cache) {
63 6
            if ($cache->existsItem($key)) {
64 6
                return $cache->getItem($key);
65
            }
66
        }
67
68 3
        return null;
69
    }
70
71
    /**
72
     * Get the "isReady" state.
73
     *
74
     * @return bool
75
     */
76 1
    public function getCacheIsReady(): bool
77
    {
78 1
        foreach ($this->caches as $cache) {
79 1
            if (!$cache->getCacheIsReady()) {
80
                return false;
81
            }
82
        }
83
84 1
        return true;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 3
    public function setItem(string $key, $value, $ttl = null): bool
91
    {
92
        // init
93 3
        $results = [];
94
95 3
        foreach ($this->caches as $cache) {
96 3
            $results[] = $cache->setItem($key, $value, $ttl);
97
        }
98
99 3
        return \in_array(true, $results, true);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 3
    public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool
106
    {
107
        // init
108 3
        $results = [];
109
110 3
        foreach ($this->caches as $cache) {
111 3
            $results[] = $cache->setItemToDate($key, $value, $date);
112
        }
113
114 3
        return \in_array(true, $results, true);
115
    }
116
117
    /**
118
     * !!! Set the prefix. !!!
119
     *
120
     * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix.
121
     *
122
     * @param string $prefix
123
     *
124
     * @return void
125
     */
126 1
    public function setPrefix(string $prefix)
127
    {
128 1
        foreach ($this->caches as $cache) {
129 1
            $cache->setPrefix($prefix);
130
        }
131 1
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 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...
137
    {
138
        // init
139 1
        $results = [];
140
141 1
        foreach ($this->caches as $cache) {
142 1
            $results[] = $cache->removeItem($key);
143
        }
144
145 1
        return \in_array(true, $results, true);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 1
    public function existsItem(string $key): bool
152
    {
153 1
        foreach ($this->caches as $cache) {
154 1
            if ($cache->existsItem($key)) {
155 1
                return true;
156
            }
157
        }
158
159
        return false;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 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...
166
    {
167
        // init
168 1
        $results = [];
169
170 1
        foreach ($this->caches as $cache) {
171 1
            $results[] = $cache->removeAll();
172
        }
173
174 1
        return \in_array(true, $results, true);
175
    }
176
}
177