Cache::getIdPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
/**
3
 * Cache.php
4
 *
5
 * @category        AngryBytes
6
 * @package         Cache
7
 * @copyright       Copyright (c) 2010 Angry Bytes BV (http://www.angrybytes.com)
8
 */
9
10
namespace AngryBytes\Cache;
11
12
use \InvalidArgumentException as InvalidArgumentException;
13
14
/**
15
 * Cache
16
 *
17
 * Cache frontend, takes care of the forward facing interface for caching
18
 *
19
 * @category        AngryBytes
20
 * @package         Cache
21
 */
22
class Cache
23
{
24
    /**
25
     * Cache backend
26
     *
27
     * @var Adapter
28
     **/
29
    private $backend;
30
31
    /**
32
     * Default life time
33
     *
34
     * @var int
35
     **/
36
    private $defaultLifeTime = 3600;
37
38
    /**
39
     * Cache id prefix
40
     *
41
     * @var string
42
     **/
43
    private $idPrefix = '';
44
45
    /**
46
     * Constructor
47
     *
48
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
49
     **/
50
    public function __construct(Adapter $backend)
51
    {
52
        $this->setBackend($backend);
53
    }
54
55
    /**
56
     * Save something in the cache
57
     *
58
     * @param mixed  $data
59
     * @param string $id
60
     * @param int    $lifeTime
61
     * @access public
62
     * @return bool
63
     */
64
    public function save($data, $id, $lifeTime = -1)
65
    {
66
        return $this->getBackend()->save(
67
            $this->serialize($data),
68
            $this->getIdPrefixed($id),
69
            $this->getLifeTime($lifeTime)
70
        );
71
    }
72
73
    /**
74
     * Load something from the cache
75
     *
76
     * @param  string               $id
77
     * @return mixed|ResultNotFound
78
     **/
79
    public function load($id)
80
    {
81
        $value = $this->getBackend()->load(
82
            $this->getIdPrefixed($id)
83
        );
84
85
        if ($value instanceof ResultNotFound) {
86
            return $value;
87
        }
88
89
        return $this->unserialize($value);
90
    }
91
92
    /**
93
     * Remove something from the cache
94
     *
95
     * @param  string $id
96
     * @return bool
97
     **/
98
    public function delete($id)
99
    {
100
        return $this->getBackend()->delete(
101
            $this->getIdPrefixed($id)
102
        );
103
    }
104
105
    /**
106
     * Create a cache key based on a variable number of inputs
107
     *
108
     * This method accepts alpha numeric strings, but also serializable objects.
109
     * All of these will be concatenated into a single id string.
110
     *
111
     * @param mixed $keyPart1
0 ignored issues
show
Bug introduced by
There is no parameter named $keyPart1. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
112
     * @param mixed $keyPart2
0 ignored issues
show
Bug introduced by
There is no parameter named $keyPart2. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
113
     * ...
114
     * @param  mixed  $keyPartN
0 ignored issues
show
Bug introduced by
There is no parameter named $keyPartN. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     * @return string
116
     **/
117
    public static function key()
118
    {
119
        // Func accepts variable number of arguments
120
        $params = func_get_args();
121
122
        // Sanity check
123
        if (count($params) === 0) {
124
            throw new InvalidArgumentException('key() expects parameters');
125
        }
126
127
        // Start the key
128
        $key = '';
129
130
        // Parts of the key that need serialisation
131
        $serializedParts = array();
132
133
        foreach ($params as $param) {
134
            if (ctype_alnum(str_replace(array('-', '_'), '', $param))) {
135
                // If alpha numeric (with dashes and underscores), add to key
136
                $key .= $param . '-';
137
            } else {
138
                // All other parts are serialized at the end
139
                $serializedParts[] = $param;
140
            }
141
        }
142
143
        // Add hash of serialized part to result
144
        if (count($serializedParts) > 0) {
145
            // Serialize and hash parts
146
            $serialized = substr(
147
                md5($this->serialize($serializedParts)),
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
148
                0, 5
149
            );
150
151
            // Return with serialized included
152
            return $key . $serialized;
153
        }
154
155
        // Or return the alphanumeric key
156
        return rtrim($key, '-');
157
    }
158
159
    /**
160
     * Get the default life time
161
     *
162
     * @return int
163
     */
164
    public function getDefaultLifeTime()
165
    {
166
        return $this->defaultLifeTime;
167
    }
168
169
    /**
170
     * Set the default life time
171
     *
172
     * @param  int   $defaultLifeTime
173
     * @return Cache
174
     */
175
    public function setDefaultLifeTime($defaultLifeTime)
176
    {
177
        $this->defaultLifeTime = $defaultLifeTime;
178
179
        return $this;
180
    }
181
182
    /**
183
     * Get the cache id prefix
184
     *
185
     * @return string
186
     */
187
    public function getIdPrefix()
188
    {
189
        return $this->idPrefix;
190
    }
191
192
    /**
193
     * Set the cache id prefix
194
     *
195
     * @param  string $prefix
196
     * @return Cache
197
     */
198
    public function setIdPrefix($prefix)
199
    {
200
        $this->idPrefix = $prefix;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Add an id prefix
207
     *
208
     * @param  string $prefix
209
     * @return Cache
210
     **/
211
    public function addIdPrefix($prefix)
212
    {
213
        $this->idPrefix .= $prefix;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Get the cache backend adapter
220
     *
221
     * @return Adapter
222
     */
223
    public function getBackend()
224
    {
225
        return $this->backend;
226
    }
227
228
    /**
229
     * Set the cache backend adapter
230
     *
231
     * @param  Adapter $backend
232
     * @return Cache
233
     */
234
    public function setBackend(Adapter $backend)
235
    {
236
        $this->backend = $backend;
237
238
        return $this;
239
    }
240
241
    /**
242
     * Get the full, prefixed id
243
     *
244
     * @see setIdPrefix()
245
     *
246
     * @param  string $id
247
     * @return string
248
     */
249
    private function getIdPrefixed($id)
250
    {
251
        if (strlen($this->getIdPrefix()) > 0) {
252
            return $this->getIdPrefix() . '-' . $id;
253
        }
254
255
        return $id;
256
    }
257
258
    /**
259
     * Get the life time to use
260
     *
261
     * Will return default life time if $lifeTime < 0
262
     *
263
     * @see setDefaultLifeTime()
264
     *
265
     * @param  int $lifeTime
266
     * @return int
267
     **/
268
    private function getLifeTime($lifeTime)
269
    {
270
        if ($lifeTime < 0) {
271
            return $this->getDefaultLifeTime();
272
        }
273
274
        return $lifeTime;
275
    }
276
277
    /**
278
     * Serialize some data into string form
279
     *
280
     * @param  mixed  $data
281
     * @return string
282
     **/
283
    private function serialize($data)
284
    {
285
        return serialize($data);
286
    }
287
288
    /**
289
     * Unserialize some data
290
     *
291
     * @param  string $data
292
     * @return mixed
293
     **/
294
    private function unserialize($data)
295
    {
296
        return unserialize($data);
297
    }
298
}
299