Completed
Push — master ( dfda1d...2f160c )
by Lars
04:17 queued 18s
created

CacheChain::setPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 1
    public function __construct(array $caches = [])
20
    {
21 1
        \array_map(
22
            [
23 1
                $this,
24 1
                'addCache',
25
            ],
26 1
            $caches
27
        );
28 1
    }
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 1
    public function addCache(Cache $cache, $prepend = true)
47
    {
48 1
        if ($prepend) {
49 1
            \array_unshift($this->caches, $cache);
50
        } else {
51
            $this->caches[] = $cache;
52
        }
53 1
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getItem(string $key)
59
    {
60
        foreach ($this->caches as $cache) {
61
            if ($cache->existsItem($key)) {
62
                return $cache->getItem($key);
63
            }
64
        }
65
66
        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 1
                return false;
79
            }
80
        }
81
82 1
        return true;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function setItem(string $key, $value, $ttl = null): bool
89
    {
90
        // init
91
        $results = [];
92
93
        foreach ($this->caches as $cache) {
94
            $results[] = $cache->setItem($key, $value, $ttl);
95
        }
96
97
        return !\in_array(false, $results, true);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool
104
    {
105
        // init
106
        $results = [];
107
108
        foreach ($this->caches as $cache) {
109
            $results[] = $cache->setItemToDate($key, $value, $date);
110
        }
111
112
        return !\in_array(false, $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
    public function setPrefix(string $prefix)
123
    {
124
        foreach ($this->caches as $cache) {
125
            $cache->setPrefix($prefix);
126
        }
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 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
        $results = [];
136
137
        foreach ($this->caches as $cache) {
138
            $results[] = $cache->removeItem($key);
139
        }
140
141
        return !\in_array(false, $results, true);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function existsItem(string $key): bool
148
    {
149
        foreach ($this->caches as $cache) {
150
            if ($cache->existsItem($key)) {
151
                return true;
152
            }
153
        }
154
155
        return false;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 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
        $results = [];
165
166
        foreach ($this->caches as $cache) {
167
            $results[] = $cache->removeAll();
168
        }
169
170
        return !\in_array(false, $results, true);
171
    }
172
}
173