Completed
Push — master ( f006cb...03e5f6 )
by Frank
05:46
created

CachedAdapter::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace League\Flysystem\Cached;
4
5
use League\Flysystem\AdapterInterface;
6
use League\Flysystem\Config;
7
8
class CachedAdapter implements AdapterInterface
9
{
10
    /**
11
     * @var AdapterInterface
12
     */
13
    private $adapter;
14
15
    /**
16
     * @var CacheInterface
17
     */
18
    private $cache;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param AdapterInterface $adapter
24
     * @param CacheInterface   $cache
25
     */
26 144
    public function __construct(AdapterInterface $adapter, CacheInterface $cache)
27
    {
28 144
        $this->adapter = $adapter;
29 144
        $this->cache = $cache;
30 144
        $this->cache->load();
31 144
    }
32
33
    /**
34
     * Get the underlying Adapter implementation.
35
     *
36
     * @return AdapterInterface
37
     */
38 3
    public function getAdapter()
39
    {
40 3
        return $this->adapter;
41
    }
42
    
43
    /**
44
     * Get the used Cache implementation.
45
     *
46
     * @return CacheInterface
47
     */
48
    public function getCache()
49
    {
50
        return $this->cache;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 6
    public function write($path, $contents, Config $config)
57
    {
58 6
        $result = $this->adapter->write($path, $contents, $config);
59
60 6
        if ($result !== false) {
61 3
            $this->cache->updateObject($path, $result + compact('path', 'contents'), true);
62 3
        }
63
64 6
        return $result;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 6
    public function writeStream($path, $resource, Config $config)
71
    {
72 6
        $result = $this->adapter->writeStream($path, $resource, $config);
73
74 6
        if ($result !== false) {
75 3
            $contents = false;
76 3
            $this->cache->updateObject($path, $result + compact('path', 'contents'), true);
77 3
        }
78
79 6
        return $result;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 6
    public function update($path, $contents, Config $config)
86
    {
87 6
        $result = $this->adapter->update($path, $contents, $config);
88
89 6
        if ($result !== false) {
90 3
            $this->cache->updateObject($path, $result + compact('path', 'contents'), true);
91 3
        }
92
93 6
        return $result;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 6
    public function updateStream($path, $resource, Config $config)
100
    {
101 6
        $result = $this->adapter->updateStream($path, $resource, $config);
102
103 6
        if ($result !== false) {
104 3
            $contents = false;
105 3
            $this->cache->updateObject($path, $result + compact('path', 'contents'), true);
106 3
        }
107
108 6
        return $result;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 6
    public function rename($path, $newPath)
115
    {
116 6
        $result = $this->adapter->rename($path, $newPath);
117
118 6
        if ($result !== false) {
119 3
            $this->cache->rename($path, $newPath);
120 3
        }
121
122 6
        return $result;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 6
    public function copy($path, $newpath)
129
    {
130 6
        $result = $this->adapter->copy($path, $newpath);
131
132 6
        if ($result !== false) {
133 3
            $this->cache->copy($path, $newpath);
134 3
        }
135
136 6
        return $result;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 6
    public function delete($path)
143
    {
144 6
        $result = $this->adapter->delete($path);
145
146 6
        if ($result !== false) {
147 3
            $this->cache->delete($path);
148 3
        }
149
150 6
        return $result;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 6
    public function deleteDir($dirname)
157
    {
158 6
        $result = $this->adapter->deleteDir($dirname);
159
160 6
        if ($result !== false) {
161 3
            $this->cache->deleteDir($dirname);
162 3
        }
163
164 6
        return $result;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 6
    public function createDir($dirname, Config $config)
171
    {
172 6
        $result = $this->adapter->createDir($dirname, $config);
173
174 6
        if ($result !== false) {
175 3
            $type = 'dir';
176 3
            $path = $dirname;
177 3
            $this->cache->updateObject($dirname, compact('path', 'type'), true);
178 3
        }
179
180 6
        return $result;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 6
    public function setVisibility($path, $visibility)
187
    {
188 6
        $result = $this->adapter->setVisibility($path, $visibility);
189
190 6
        if ($result !== false) {
191 3
            $this->cache->updateObject($path, compact('path', 'visibility'), true);
192 3
        }
193
194 6
        return $result;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 12
    public function has($path)
201
    {
202 12
        $cacheHas = $this->cache->has($path);
203
204 12
        if ($cacheHas !== null) {
205 6
            return $cacheHas;
206
        }
207
208 6
        $adapterResponse = $this->adapter->has($path);
209
210 6
        if (! $adapterResponse) {
211 3
            $this->cache->storeMiss($path);
212 3
        } else {
213 3
            $cacheEntry = is_array($adapterResponse) ? $adapterResponse : compact('path');
214 3
            $this->cache->updateObject($path, $cacheEntry, true);
215
        }
216
217 6
        return $adapterResponse;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 9
    public function read($path)
224
    {
225 9
        return $this->callWithFallback('read', $path);
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 3
    public function readStream($path)
232
    {
233 3
        return $this->adapter->readStream($path);
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239 9
    public function listContents($directory = '', $recursive = false)
240
    {
241 9
        if ($this->cache->isComplete($directory, $recursive)) {
242 3
            return $this->cache->listContents($directory, $recursive);
243
        }
244
245 6
        $result = $this->adapter->listContents($directory, $recursive);
246
247 6
        if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
248 3
            $this->cache->storeContents($directory, $result, $recursive);
249 3
        }
250
251 6
        return $result;
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257 9
    public function getMetadata($path)
258
    {
259 9
        return $this->callWithFallback('getMetadata', $path);
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265 9
    public function getSize($path)
266
    {
267 9
        return $this->callWithFallback('getSize', $path);
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273 9
    public function getMimetype($path)
274
    {
275 9
        return $this->callWithFallback('getMimetype', $path);
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281 9
    public function getTimestamp($path)
282
    {
283 9
        return $this->callWithFallback('getTimestamp', $path);
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289 9
    public function getVisibility($path)
290
    {
291 9
        return $this->callWithFallback('getVisibility', $path);
292
    }
293
294
    /**
295
     * Call a method and cache the response.
296
     *
297
     * @param string $method
298
     * @param string $path
299
     *
300
     * @return mixed
301
     */
302 54
    protected function callWithFallback($method, $path)
303
    {
304 54
        $result = $this->cache->{$method}($path);
305
306 54
        if ($result !== false) {
307 18
            return $result;
308
        }
309
310 36
        $result = $this->adapter->{$method}($path);
311
312 36
        if ($result) {
313 18
            $object = $result + compact('path');
314 18
            $this->cache->updateObject($path, $object, true);
315 18
        }
316
317 36
        return $result;
318
    }
319
}
320