Completed
Push — master ( b9da65...b16e40 )
by Marco
04:58
created

PhpRedis::clear()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 10

Duplication

Lines 13
Ratio 52 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 13
loc 25
ccs 6
cts 8
cp 0.75
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 10
nc 6
nop 1
crap 4.25
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \Comodojo\Cache\Traits\InstanceTrait;
4
use \Comodojo\Cache\Components\UniqueId;
5
use \RedisException;
6
use \Redis;
7
use \Exception;
8
9
/**
10
 * memcached provider
11
 *
12
 * @package     Comodojo Spare Parts
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @license     MIT
15
 *
16
 * LICENSE:
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
class PhpRedis extends AbstractDriver {
28
29
    use InstanceTrait;
30
31
    const DRIVER_NAME = "php-redis";
32
33
    private $connection_parameters;
34
35 47
    public function __construct(array $configuration) {
36
37 47
        if ( class_exists('Redis') === false ) throw new Exception("ext-redis not available");
38
39 47
        $instance = new Redis();
40
41 47
        $this->connection_parameters = [$configuration['server'], $configuration['port'], $configuration['timeout']];
42
43 47
        $instance->connect(...$this->connection_parameters);
44
45 47
        $this->setInstance($instance);
46
47 47
    }
48
49 47
    public function test() {
50
51 47
        $instance = $this->getInstance();
52
53
        try {
54
55 47
            $instance->ping();
56 47
            return true;
57
58
        } catch (RedisException $re) {
59
60
            // may be a connection error, try to reconnect first
61
            if ( $instance->connect(...$this->connection_parameters) === false ) {
62
                return false;
63
            } else {
64
                return true;
65
            }
66
67
        }
68
69
    }
70
71 35
    public function get($key, $namespace) {
72
73
        try {
74
75 35
            $scope = $this->getNamespaceKey($namespace);
76
77 35
            if ( $scope === false ) return null;
78
79 27
            $shadowName = "$scope-$key";
80
81 27
            $instance = $this->getInstance();
82
83 27
            $item = $instance->get($shadowName);
84
85 27
        } catch (RedisException $re) {
86
87
            throw $re;
88
89
        }
90
91 27
        return $item === false ? null : $item;
92
93
    }
94
95 28
    public function set($key, $namespace, $value, $ttl = null) {
96
97
        try {
98
99 28
            if ( $ttl == null ) $ttl = 0;
100
101 28
            $scope = $this->getNamespaceKey($namespace);
102
103 28
            if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
104
105 28
            if ( $scope === false ) return false;
106
107 28
            $shadowName = "$scope-$key";
108
109 28
            $instance = $this->getInstance();
110
111 28
            if ( $ttl == 0 ) {
112 25
                $return = $instance->set($shadowName, $value);
113 25
            } else {
114 4
                $return = $instance->setex($shadowName, $ttl, $value);
115
            }
116
117 28
        } catch (RedisException $re) {
118
119
            throw $re;
120
121
        }
122
123 28
        return (bool) $return;
124
125
    }
126
127 6 View Code Duplication
    public function delete($key, $namespace) {
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...
128
129
        try {
130
131 6
            $scope = $this->getNamespaceKey($namespace);
132
133 6
            if ( $scope === false ) return false;
134
135 6
            $shadowName = "$scope-$key";
136
137 6
            return (bool) $this->getInstance()->delete($shadowName);
138
139
        } catch (RedisException $re) {
140
141
            throw $re;
142
143
        }
144
145
    }
146
147 11
    public function clear($namespace = null) {
148
149
        try {
150
151 11 View Code Duplication
            if ( $namespace == null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
153 9
                return (bool) $this->getInstance()->flushDB();
154
155
            } else {
156
157 2
                $scope = $this->getNamespaceKey($namespace);
158
159 2
                if ( $scope === false ) return false;
160
161 2
                return (bool) $this->getInstance()->delete($namespace);
162
163
            }
164
165
        } catch (RedisException $re) {
166
167
            throw $re;
168
169
        }
170
171
    }
172
173
    // TODO: write a better getMultiple using mGet
174 2 View Code Duplication
    public function getMultiple(array $keys, $namespace) {
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...
175
176 2
        $result = [];
177
178 2
        foreach ( $keys as $key ) {
179 2
            $result[$key] = $this->get($key, $namespace);
180 2
        }
181
182 2
        return $result;
183
184
    }
185
186
    // TODO: write a better setMultiple using mSet
187 1 View Code Duplication
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
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...
188
189 1
        $result = [];
190
191 1
        foreach ( $key_values as $key => $value ) {
192 1
            $result[] = $this->set($key, $namespace, $value, $ttl);
193 1
        }
194
195 1
        return !in_array(false, $result);
196
197
    }
198
199
    // TODO: write a better deleteMultiple using delete([])
200 3 View Code Duplication
    public function deleteMultiple(array $keys, $namespace) {
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...
201
202 3
        $result = [];
203
204 3
        foreach ( $keys as $key ) {
205 3
            $result[] = $this->delete($key, $namespace);
206 3
        }
207
208 3
        return !in_array(false, $result);
209
210
    }
211
212 5
    public function has($key, $namespace) {
213
214
        try {
215
216 5
            $scope = $this->getNamespaceKey($namespace);
217
218 5
            if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
219
220 5
            if ( $scope === false ) return false;
221
222 5
            $shadowName = "$scope-$key";
223
224 5
            return (bool) $this->getInstance()->exists($shadowName);
225
226
        } catch (RedisException $re) {
227
228
            throw $re;
229
230
        }
231
232
    }
233
234 2
    public function stats() {
235
236 2
        $instance = $this->getInstance();
237
238
        try {
239
240 2
            $objects = $instance->dbSize();
241 2
            $stats = $instance->info();
242
243 2
        } catch (RedisException $re) {
244
245
            throw $re;
246
247
        }
248
249 2
        return ['objects' => $objects, 'stats' => $stats];
250
251
    }
252
253
    /**
254
     * Set namespace key
255
     *
256
     * @return  mixed
257
     */
258 5 View Code Duplication
    private function setNamespaceKey($namespace) {
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...
259
260 5
        $uId = UniqueId::get();
261
262 5
        $return = $this->getInstance()->set($namespace, $uId);
263
264 5
        return $return === false ? false : $uId;
265
266
    }
267
268
    /**
269
     * Get namespace key
270
     *
271
     * @return  string
272
     */
273 37
    private function getNamespaceKey($namespace) {
274
275 37
        return $this->getInstance()->get($namespace);
276
277
    }
278
279
}
280