Passed
Pull Request — master (#335)
by
unknown
30:34
created

setDefaultLifeTimeForDownstreamCacheProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use Traversable;
6
use function array_values;
7
use function count;
8
use function iterator_to_array;
9
10
/**
11
 * Cache provider that allows to easily chain multiple cache providers
12
 */
13
class ChainCache extends CacheProvider
14
{
15
    /** @var CacheProvider[] */
16
    private $cacheProviders = [];
17
18
    /** @var int */
19
    private $defaultLifeTimeForDownstreamCacheProviders = 0;
20
21 85
    /**
22
     * @param CacheProvider[] $cacheProviders
23 85
     */
24 1
    public function __construct($cacheProviders = [])
25 84
    {
26 85
        $this->cacheProviders = $cacheProviders instanceof Traversable
0 ignored issues
show
introduced by
$cacheProviders is never a sub-type of Traversable.
Loading history...
27
            ? iterator_to_array($cacheProviders, false)
28
            : array_values($cacheProviders);
29
    }
30
31 3
    public function setDefaultLifeTimeForDownstreamCacheProviders(int $defaultLifeTimeForDownstreamCacheProviders) : void
32
    {
33 3
        $this->defaultLifeTimeForDownstreamCacheProviders = $defaultLifeTimeForDownstreamCacheProviders;
34
    }
35 3
36 3
    /**
37
     * {@inheritDoc}
38 3
     */
39
    public function setNamespace($namespace)
40
    {
41
        parent::setNamespace($namespace);
42
43 80
        foreach ($this->cacheProviders as $cacheProvider) {
44
            $cacheProvider->setNamespace($namespace);
45 80
        }
46 80
    }
47 63
48
    /**
49
     * {@inheritDoc}
50 63
     */
51 1
    protected function doFetch($id)
52
    {
53
        foreach ($this->cacheProviders as $key => $cacheProvider) {
54 80
            if ($cacheProvider->doContains($id)) {
55
                $value = $cacheProvider->doFetch($id);
56
57
                // We populate all the previous cache layers (that are assumed to be faster)
58 80
                for ($subKey = $key - 1; $subKey >= 0; $subKey--) {
59
                    $this->cacheProviders[$subKey]->doSave($id, $value, $this->defaultLifeTimeForDownstreamCacheProviders);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false; however, parameter $data of Doctrine\Common\Cache\CacheProvider::doSave() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
                    $this->cacheProviders[$subKey]->doSave($id, /** @scrutinizer ignore-type */ $value, $this->defaultLifeTimeForDownstreamCacheProviders);
Loading history...
60
                }
61
62
                return $value;
63
            }
64 4
        }
65
66
        return false;
67 4
    }
68 4
69 4
    /**
70
     * {@inheritdoc}
71 4
     */
72 4
    protected function doFetchMultiple(array $keys)
73
    {
74
        /** @var CacheProvider[] $traversedProviders */
75 4
        $traversedProviders = [];
76 4
        $keysCount          = count($keys);
77 1
        $fetchedValues      = [];
78
79
        foreach ($this->cacheProviders as $key => $cacheProvider) {
80 4
            $fetchedValues = $cacheProvider->doFetchMultiple($keys);
81
82
            // We populate all the previous cache layers (that are assumed to be faster)
83 2
            if (count($fetchedValues) === $keysCount) {
84
                foreach ($traversedProviders as $previousCacheProvider) {
85
                    $previousCacheProvider->doSaveMultiple($fetchedValues, $this->defaultLifeTimeForDownstreamCacheProviders);
86 1
                }
87
88
                return $fetchedValues;
89
            }
90
91
            $traversedProviders[] = $cacheProvider;
92 69
        }
93
94 69
        return $fetchedValues;
95 69
    }
96 69
97
    /**
98
     * {@inheritDoc}
99
     */
100 50
    protected function doContains($id)
101
    {
102
        foreach ($this->cacheProviders as $cacheProvider) {
103
            if ($cacheProvider->doContains($id)) {
104
                return true;
105
            }
106 73
        }
107
108 73
        return false;
109
    }
110 73
111 73
    /**
112
     * {@inheritDoc}
113
     */
114 73
    protected function doSave($id, $data, $lifeTime = 0)
115
    {
116
        $stored = true;
117
118
        foreach ($this->cacheProviders as $cacheProvider) {
119
            $stored = $cacheProvider->doSave($id, $data, $lifeTime) && $stored;
120 2
        }
121
122 2
        return $stored;
123
    }
124 2
125 2
    /**
126
     * {@inheritdoc}
127
     */
128 2
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
129
    {
130
        $stored = true;
131
132
        foreach ($this->cacheProviders as $cacheProvider) {
133
            $stored = $cacheProvider->doSaveMultiple($keysAndValues, $lifetime) && $stored;
134 45
        }
135
136 45
        return $stored;
137
    }
138 45
139 45
    /**
140
     * {@inheritDoc}
141
     */
142 45
    protected function doDelete($id)
143
    {
144
        $deleted = true;
145
146
        foreach ($this->cacheProviders as $cacheProvider) {
147
            $deleted = $cacheProvider->doDelete($id) && $deleted;
148 2
        }
149
150 2
        return $deleted;
151
    }
152 2
153 2
    /**
154
     * {@inheritdoc}
155
     */
156 2
    protected function doDeleteMultiple(array $keys)
157
    {
158
        $deleted = true;
159
160
        foreach ($this->cacheProviders as $cacheProvider) {
161
            $deleted = $cacheProvider->doDeleteMultiple($keys) && $deleted;
162 3
        }
163
164 3
        return $deleted;
165
    }
166 3
167 3
    /**
168
     * {@inheritDoc}
169
     */
170 3
    protected function doFlush()
171
    {
172
        $flushed = true;
173
174
        foreach ($this->cacheProviders as $cacheProvider) {
175
            $flushed = $cacheProvider->doFlush() && $flushed;
176 1
        }
177
178
        return $flushed;
179 1
    }
180
181 1
    /**
182 1
     * {@inheritDoc}
183
     */
184
    protected function doGetStats()
185 1
    {
186
        // We return all the stats from all adapters
187
        $stats = [];
188
189
        foreach ($this->cacheProviders as $cacheProvider) {
190
            $stats[] = $cacheProvider->doGetStats();
191
        }
192
193
        return $stats;
194
    }
195
}
196