Passed
Push — master ( 59198c...cdc699 )
by Kevin
02:26
created

CompressedDoctrineCacheStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.26%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 67
loc 67
ccs 18
cts 23
cp 0.7826
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A save() 17 17 3
A fetch() 13 13 3
A delete() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 CompressedDoctrineCacheStorage 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(gzuncompress($this->cache->fetch($key)));
30 2
            if ($cache instanceof CacheEntry) {
31 2
                return $cache;
32
            }
33 2
        } catch (\Exception $ignored) {
34 2
            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
                    gzcompress(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 2
    public function delete($key)
65
    {
66
        try {
67 2
            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