Completed
Pull Request — master (#130)
by
unknown
07:37
created

FlysystemStorage::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.5
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
use League\Flysystem\FileNotFoundException;
9
10
class FlysystemStorage implements CacheStorageInterface
11
{
12
13
    /**
14
     * @var Filesystem
15
     */
16
    protected $filesystem;
17
18 2
    public function __construct(AdapterInterface $adapter)
19
    {
20 2
        $this->filesystem = new Filesystem($adapter);
21 2
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 2
    public function fetch($key)
27
    {
28 2
        if ($this->filesystem->has($key)) {
29
            // The file exist, read it!
30 2
            $data = @unserialize(
31 2
                $this->filesystem->read($key)
32 2
            );
33
34 2
            if ($data instanceof CacheEntry) {
35 2
                return $data;
36
            }
37
        }
38
39 2
        return;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 2
    public function save($key, CacheEntry $data)
46
    {
47 2
        return $this->filesystem->put($key, serialize($data));
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function delete($key)
54
    {
55
        try {
56 2
            return $this->filesystem->delete($key);
57
        } catch (FileNotFoundException $ex){
58
            return true;
59
        }
60
    }
61
}
62