Passed
Push — develop ( 2893dc...c61bef )
by Michał
02:37
created

SimpleCache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 11.57 %

Test Coverage

Coverage 87.8%

Importance

Changes 0
Metric Value
dl 14
loc 121
ccs 36
cts 41
cp 0.878
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 2
A deleteMultiple() 13 13 3
A delete() 0 3 1
A getMultiple() 0 9 2
A set() 0 9 2
A clear() 0 3 1
A has() 0 3 1
A setMultiple() 0 13 3

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 BlueCache;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
class SimpleCache implements CacheInterface
8
{
9
    use Common;
10
11
    /**
12
     * @param string $key
13
     * @param mixed $default
14
     * @return array|null|\Psr\Cache\CacheItemInterface
15
     */
16 2
    public function get($key, $default = null)
17
    {
18 2
        $cacheItem = $this->storage->restore($key);
19
20 2
        if (\is_null($cacheItem)) {
21
            return $default;
22
        }
23
24 2
        return $cacheItem->get();
25
    }
26
27
    /**
28
     * @param string $key
29
     * @param mixed $value
30
     * @param \DateTimeInterface|\DateInterval|null|int $ttl
31
     * @return bool
32
     * @throws \BlueCache\CacheException
33
     */
34 8
    public function set($key, $value, $ttl = null)
35
    {
36 8
        $item = (new CacheItem($key))->set($value);
37
38 8
        if (!\is_null($ttl)) {
39 1
            $item->expiresAfter($ttl);
40 1
        }
41
42 8
        return $this->storage->store($item);
43
    }
44
45
    /**
46
     * @param string $key
47
     * @return bool
48
     * @throws \BlueCache\CacheException
49
     */
50 2
    public function delete($key)
51
    {
52 2
        return $this->storage->clear($key);
53
    }
54
55
    /**
56
     * @return bool
57
     * @throws \BlueCache\CacheException
58
     */
59 1
    public function clear()
60
    {
61 1
        return $this->storage->clear();
62
    }
63
64
    /**
65
     * @param iterable $keys
66
     * @param null|mixed $default
67
     * @return array
68
     */
69 2
    public function getMultiple($keys, $default = null)
70
    {
71 2
        $list = [];
72
73 2
        foreach ($keys as $key) {
74 2
            $list[$key] = $this->get($key, $default);
75 2
        }
76
77 2
        return $list;
78
    }
79
80
    /**
81
     * @param iterable $values
82
     * @param null|int $ttl
83
     * @return bool
84
     * @throws \BlueCache\CacheException
85
     */
86 5
    public function setMultiple($values, $ttl = null)
87
    {
88 5
        $flag = true;
89
90 5
        foreach ($values as $key => $data) {
91 5
            $isSet = $this->set($key, $data, $ttl);
92
93 5
            if (!$isSet) {
94
                $flag = false;
95
            }
96 5
        }
97
98 5
        return $flag;
99
    }
100
101
    /**
102
     * @param iterable $keys
103
     * @return bool
104
     * @throws \BlueCache\CacheException
105
     */
106 1 View Code Duplication
    public function deleteMultiple($keys)
0 ignored issues
show
Duplication introduced by
This method 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...
107
    {
108 1
        $flag = true;
109
110 1
        foreach ($keys as $key) {
111 1
            $deleted = $this->delete($key);
112
113 1
            if (!$deleted) {
114
                $flag = false;
115
            }
116 1
        }
117
118 1
        return $flag;
119
    }
120
121
    /**
122
     * @param string $key
123
     * @return bool
124
     */
125 8
    public function has($key)
126
    {
127 8
        return $this->storage->exists($key);
128
    }
129
}
130