Completed
Pull Request — master (#39)
by Jérôme
02:40
created

FlysystemStorage::fetch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Storage;
4
5
use Kevinrob\GuzzleCache\CacheEntry;
6
use League\Flysystem\AdapterInterface;
7
use League\Flysystem\Filesystem;
8
9
class FlysystemStorage implements CacheStorageInterface
10
{
11
12
    /**
13
     * @var Filesystem
14
     */
15
    protected $filesystem;
16
17
    public function __construct(AdapterInterface $adapter)
18
    {
19
        $this->filesystem = new Filesystem($adapter);
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function fetch($key)
26
    {
27
        if ($this->filesystem->has($key)) {
28
            // The file exist, read it!
29
            $data = @unserialize(
30
                $this->filesystem->read($key)
31
            );
32
33
            if ($data instanceof CacheEntry) {
34
                return $data;
35
            }
36
        }
37
38
        return;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function save($key, CacheEntry $data)
45
    {
46
        return $this->filesystem->put($key, serialize($data));
47
    }
48
49
    public function delete($key)
50
    {
51
        return $this->filesystem->delete($key);
52
    }
53
}
54