Cache::inc()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 16
loc 16
rs 8.8571
cc 5
eloc 9
nc 4
nop 2
1
<?php
2
/**
3
 * Cache Class.
4
 *
5
 * @package SugiPHP.Cache
6
 * @author  Plamen Popov <[email protected]>
7
 * @license http://opensource.org/licenses/mit-license.php (MIT License)
8
 */
9
10
namespace SugiPHP\Cache;
11
12
class Cache
13
{
14
    /**
15
     * The driver used by a cache.
16
     * @var StoreInterface
17
     */
18
    protected $driver;
19
20
    /**
21
     * Key prefix to use.
22
     *
23
     * @var string
24
     */
25
    protected $prefix = "";
26
27
    /**
28
     * Class constructor.
29
     *
30
     * @param StoreInterface $driver
31
     */
32
    public function __construct(StoreInterface $driver)
33
    {
34
        $this->driver = $driver;
35
    }
36
37
    /**
38
     * Sets key prefix.
39
     *
40
     * @param string $prefix
41
     */
42
    public function setPrefix($prefix)
43
    {
44
        $this->prefix = $prefix;
45
    }
46
47
    /**
48
     * Returns key prefix.
49
     *
50
     * @return string
51
     */
52
    public function getPrefix()
53
    {
54
        return $this->prefix;
55
    }
56
57
    /**
58
     * Stores an item in the cache for a specified period of time only if it is not already stored.
59
     * Cache::add() is similar to Cache::set(), but the operation fails if the key already exists.
60
     *
61
     * @param string  $key
62
     * @param mixed  $value The value to be stored.
63
     * @param integer $ttl Time to live in seconds. 0 means to store it for a maximum time possible
64
     *
65
     * @return boolean TRUE if the value is set, FALSE on failure
66
     */
67
    public function add($key, $value, $ttl = 0)
68
    {
69
        $key = $this->prefix.$key;
70
71
        return $this->driver->add($key, $value, $ttl);
72
    }
73
74
    /**
75
     * Stores an item in the data store
76
     * Cache::set() is similar to Cache::add(), but the operation will not fail if the key already exist.
77
     *
78
     * @param string $key The key under which to store the value
79
     * @param mixed $value The value to store
80
     * @param integer $ttl Expiration time in seconds, after which the value is invalidated (deleted)
81
     *
82
     * @return boolean TRUE on success or FALSE on failure
83
     */
84
    public function set($key, $value, $ttl = 0)
85
    {
86
        $key = $this->prefix.$key;
87
88
        return $this->driver->set($key, $value, $ttl);
89
    }
90
91
    /**
92
     * Fetches a stored variable from the cache
93
     *
94
     * @param string $key The key used to store the value
95
     *
96
     * @return mixed Returns NULL if the key does not exist in the store or the value was expired (see $ttl)
97
     */
98
    public function get($key, $defaultValue = null)
99
    {
100
        $key = $this->prefix.$key;
101
        $result = $this->driver->get($key);
102
103
        return is_null($result) ? $defaultValue : $result;
104
    }
105
106
    /**
107
     * Checks if the key exists
108
     *
109
     * @param string $key
110
     *
111
     * @return boolean TRUE if the key exists, otherwise FALSE
112
     */
113
    public function has($key)
114
    {
115
        $key = $this->prefix.$key;
116
117
        return $this->driver->has($key);
118
    }
119
120
    /**
121
     * Removes a stored variable from the cache
122
     *
123
     * @param string $key
124
     */
125
    public function delete($key)
126
    {
127
        $key = $this->prefix.$key;
128
129
        $this->driver->delete($key);
130
    }
131
132
    /**
133
     * Invalidate all items in the cache
134
     */
135
    public function flush()
136
    {
137
        $this->driver->flush();
138
    }
139
140
    /**
141
     * Increment numeric item's value.
142
     * If there is no such key or the stored value is not numeric FALSE is returned
143
     *
144
     * @param string $key
145
     * @param integer $step
146
     *
147
     * @return integer|false Returns the new incremented value or FALSE on failure
148
     */
149 View Code Duplication
    public function inc($key, $step = 1)
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...
150
    {
151
        $key = $this->prefix.$key;
152
153
        if ($this->driver instanceof IncrementorInterface) {
154
            return $this->driver->inc($key, $step);
155
        }
156
157
        $value = $this->driver->get($key);
158
        if (is_null($value) || !is_numeric($value)) {
159
            return false;
160
        }
161
        $newValue = $value + $step;
162
163
        return ($this->driver->set($key, $newValue)) ? $newValue : false;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->driver->set($key,...e) ? $newValue : false; of type integer|double|false adds the type double to the return on line 163 which is incompatible with the return type documented by SugiPHP\Cache\Cache::inc of type integer|false.
Loading history...
164
    }
165
166
    /**
167
     * Decrements numeric item's value.
168
     * If there is no such key or the stored value is not numeric FALSE is returned
169
     *
170
     * @param string $key
171
     * @param integer $step
172
     *
173
     * @return integer|false Returns the new decremented value or FALSE on failure
174
     */
175 View Code Duplication
    public function dec($key, $step = 1)
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...
176
    {
177
        $key = $this->prefix.$key;
178
179
        if ($this->driver instanceof IncrementorInterface) {
180
            return $this->driver->dec($key, $step);
181
        }
182
183
        $value = $this->driver->get($key);
184
        if (is_null($value) || !is_numeric($value)) {
185
            return false;
186
        }
187
        $newValue = $value - $step;
188
189
        return ($this->driver->set($key, $newValue)) ? $newValue : false;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->driver->set($key,...e) ? $newValue : false; of type integer|double|false adds the type double to the return on line 189 which is incompatible with the return type documented by SugiPHP\Cache\Cache::dec of type integer|false.
Loading history...
190
    }
191
}
192