Passed
Push — master ( d8c2a1...59198c )
by Kevin
07:55
created

DoctrineCacheStorage::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Storage;
4
5
use Doctrine\Common\Cache\Cache;
6
use Kevinrob\GuzzleCache\CacheEntry;
7
8 View Code Duplication
class DoctrineCacheStorage implements CacheStorageInterface
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * @var Cache
12
     */
13
    protected $cache;
14
15
    /**
16
     * @param Cache $cache
17
     */
18 2
    public function __construct(Cache $cache)
19
    {
20 2
        $this->cache = $cache;
21 2
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function fetch($key)
27
    {
28
        try {
29 2
            $cache = unserialize($this->cache->fetch($key));
30 2
            if ($cache instanceof CacheEntry) {
31 2
                return $cache;
32
            }
33
        } catch (\Exception $ignored) {
34
            return;
35
        }
36
37
        return;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 2
    public function save($key, CacheEntry $data)
44
    {
45
        try {
46 2
            $lifeTime = $data->getTTL();
47 2
            if ($lifeTime >= 0) {
48 2
                return $this->cache->save(
49 2
                    $key,
50 2
                    serialize($data),
51
                    $lifeTime
52 2
                );
53
            }
54
        } catch (\Exception $ignored) {
55
            // No fail if we can't save it the storage
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function delete($key)
65
    {
66
        try {
67
            return $this->cache->delete($key);
68
        } catch (\Exception $ignored) {
69
            // Don't fail if we can't delete it
70
        }
71
72
        return false;
73
    }
74
}
75