PrefixKeys::cas()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MatthiasMullie\Scrapbook\Adapters\Collections\Utils;
4
5
use MatthiasMullie\Scrapbook\KeyValueStore;
6
7
/**
8
 * @author Matthias Mullie <[email protected]>
9
 * @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved
10
 * @license LICENSE MIT
11
 */
12
class PrefixKeys implements KeyValueStore
13
{
14
    /**
15
     * @var KeyValueStore
16
     */
17
    protected $cache;
18
19
    /**
20
     * @var string
21
     */
22
    protected $prefix;
23
24
    /**
25
     * @param string $prefix
26
     */
27
    public function __construct(KeyValueStore $cache, $prefix)
28
    {
29
        $this->cache = $cache;
30
        $this->setPrefix($prefix);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function get($key, &$token = null)
37
    {
38
        $key = $this->prefix($key);
39
40
        return $this->cache->get($key, $token);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getMulti(array $keys, array &$tokens = null)
47
    {
48
        $keys = array_map(array($this, 'prefix'), $keys);
49
        $results = $this->cache->getMulti($keys, $tokens);
50
        $keys = array_map(array($this, 'unfix'), array_keys($results));
51
        $tokens = array_combine($keys, $tokens);
52
53
        return array_combine($keys, $results);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function set($key, $value, $expire = 0)
60
    {
61
        $key = $this->prefix($key);
62
63
        // Note: I have no idea why, but it seems to happen in some cases that
64
        // `$value` is `null`, but func_get_arg(1) returns the correct value.
65
        // Makes no sense, probably a very obscure edge case, but it happens.
66
        // (it didn't seem to happen if `$value` was another variable name...)
67
        return $this->cache->set($key, func_get_arg(1), $expire);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function setMulti(array $items, $expire = 0)
74
    {
75
        $keys = array_map(array($this, 'prefix'), array_keys($items));
76
        $items = array_combine($keys, $items);
77
        $results = $this->cache->setMulti($items, $expire);
78
        $keys = array_map(array($this, 'unfix'), array_keys($results));
79
80
        return array_combine($keys, $results);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function delete($key)
87
    {
88
        $key = $this->prefix($key);
89
90
        return $this->cache->delete($key);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function deleteMulti(array $keys)
97
    {
98
        $keys = array_map(array($this, 'prefix'), $keys);
99
        $results = $this->cache->deleteMulti($keys);
100
        $keys = array_map(array($this, 'unfix'), array_keys($results));
101
102
        return array_combine($keys, $results);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function add($key, $value, $expire = 0)
109
    {
110
        $key = $this->prefix($key);
111
112
        // Note: I have no idea why, but it seems to happen in some cases that
113
        // `$value` is `null`, but func_get_arg(1) returns the correct value.
114
        // Makes no sense, probably a very obscure edge case, but it happens.
115
        // (it didn't seem to happen if `$value` was another variable name...)
116
        return $this->cache->add($key, func_get_arg(1), $expire);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function replace($key, $value, $expire = 0)
123
    {
124
        $key = $this->prefix($key);
125
126
        // Note: I have no idea why, but it seems to happen in some cases that
127
        // `$value` is `null`, but func_get_arg(1) returns the correct value.
128
        // Makes no sense, probably a very obscure edge case, but it happens.
129
        // (it didn't seem to happen if `$value` was another variable name...)
130
        return $this->cache->replace($key, func_get_arg(1), $expire);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function cas($token, $key, $value, $expire = 0)
137
    {
138
        $key = $this->prefix($key);
139
140
        // Note: I have no idea why, but it seems to happen in some cases that
141
        // `$value` is `null`, but func_get_arg(2) returns the correct value.
142
        // Makes no sense, probably a very obscure edge case, but it happens.
143
        // (it didn't seem to happen if `$value` was another variable name...)
144
        return $this->cache->cas($token, $key, func_get_arg(2), $expire);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function increment($key, $offset = 1, $initial = 0, $expire = 0)
151
    {
152
        $key = $this->prefix($key);
153
154
        return $this->cache->increment($key, $offset, $initial, $expire);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function decrement($key, $offset = 1, $initial = 0, $expire = 0)
161
    {
162
        $key = $this->prefix($key);
163
164
        return $this->cache->decrement($key, $offset, $initial, $expire);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function touch($key, $expire)
171
    {
172
        $key = $this->prefix($key);
173
174
        return $this->cache->touch($key, $expire);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function flush()
181
    {
182
        return $this->cache->flush();
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function getCollection($name)
189
    {
190
        return $this->cache->getCollection($name);
191
    }
192
193
    /**
194
     * @param string $prefix
195
     */
196
    protected function setPrefix($prefix)
197
    {
198
        $this->prefix = $prefix;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    protected function prefix($key)
205
    {
206
        return $this->prefix.$key;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    protected function unfix($key)
213
    {
214
        return preg_replace('/^'.preg_quote($this->prefix, '/').'/', '', $key);
215
    }
216
}
217