Completed
Push — 2.0 ( a52f91...c9f4d8 )
by Marco
12:16
created

PhpRedis   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 253
Duplicated Lines 29.25 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 3
dl 74
loc 253
rs 8.6
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A test() 0 21 3
B get() 0 23 4
B set() 0 31 6
A delete() 19 19 3
B clear() 13 25 4
A getMultiple() 11 11 2
A setMultiple() 11 11 2
A deleteMultiple() 11 11 2
A has() 0 21 4
A stats() 0 18 2
A setNamespaceKey() 9 9 2
A getNamespaceKey() 0 5 1

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 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
    public function __construct(array $configuration) {
36
37
        if ( class_exists('Redis') === false ) throw new Exception("ext-redis not available");
38
39
        $instance = new Redis();
40
41
        $this->connection_parameters = [$configuration['server'], $configuration['port'], $configuration['timeout']];
42
43
        $instance->connect(...$this->connection_parameters);
44
45
        $this->setInstance($instance);
46
47
    }
48
49
    public function test() {
50
51
        $instance = $this->getInstance();
52
53
        try {
54
55
            $instance->ping();
56
            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
    public function get($key, $namespace) {
72
73
        try {
74
75
            $scope = $this->getNamespaceKey($namespace);
76
77
            if ( $scope === false ) return null;
78
79
            $shadowName = "$scope-$key";
80
81
            $instance = $this->getInstance();
82
83
            $item = $instance->get($shadowName);
84
85
        } catch (RedisException $re) {
86
87
            throw $re;
88
89
        }
90
91
        return $item === false ? null : $item;
92
93
    }
94
95
    public function set($key, $namespace, $value, $ttl = null) {
96
97
        try {
98
99
            if ( $ttl == null ) $ttl = 0;
100
101
            $scope = $this->getNamespaceKey($namespace);
102
103
            if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
104
105
            if ( $scope === false ) return false;
106
107
            $shadowName = "$scope-$key";
108
109
            $instance = $this->getInstance();
110
111
            if ( $ttl == 0 ) {
112
                $return = $instance->set($shadowName, $value);
113
            } else {
114
                $return = $instance->setex($shadowName, $ttl, $value);
115
            }
116
117
        } catch (RedisException $re) {
118
119
            throw $re;
120
121
        }
122
123
        return (bool) $return;
124
125
    }
126
127 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
            $scope = $this->getNamespaceKey($namespace);
132
133
            if ( $scope === false ) return false;
134
135
            $shadowName = "$scope-$key";
136
137
            return (bool) $this->getInstance()->delete($shadowName);
138
139
        } catch (RedisException $re) {
140
141
            throw $re;
142
143
        }
144
145
    }
146
147
    public function clear($namespace = null) {
148
149
        try {
150
151 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
                return (bool) $this->getInstance()->flushDB();
154
155
            } else {
156
157
                $scope = $this->getNamespaceKey($namespace);
158
159
                if ( $scope === false ) return false;
160
161
                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 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
        $result = [];
177
178
        foreach ($keys as $key) {
179
            $result[$key] = $this->get($key, $namespace);
180
        }
181
182
        return $result;
183
184
    }
185
186
    // TODO: write a better setMultiple using mSet
187 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
        $result = [];
190
191
        foreach ($key_values as $key => $value) {
192
            $result[] = $this->set($key, $namespace, $value, $ttl);
193
        }
194
195
        return !in_array(false, $result);
196
197
    }
198
199
    // TODO: write a better deleteMultiple using delete([])
200 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
        $result = [];
203
204
        foreach ($keys as $key) {
205
            $result[] = $this->delete($key, $namespace);
206
        }
207
208
        return !in_array(false, $result);
209
210
    }
211
212
    public function has($key, $namespace) {
213
214
        try {
215
216
            $scope = $this->getNamespaceKey($namespace);
217
218
            if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
219
220
            if ( $scope === false ) return false;
221
222
            $shadowName = "$scope-$key";
223
224
            return (bool) $this->getInstance()->exists($shadowName);
225
226
        } catch (RedisException $re) {
227
228
            throw $re;
229
230
        }
231
232
    }
233
234
    public function stats() {
235
236
        $instance = $this->getInstance();
237
238
        try {
239
240
            $objects = $instance->dbSize();
241
            $stats = $instance->info();
242
243
        } catch (RedisException $re ) {
244
245
            throw $re;
246
247
        }
248
249
        return ['objects' => $objects, 'stats' => $stats];
250
251
    }
252
253
    /**
254
     * Set namespace key
255
     *
256
     * @return  mixed
257
     */
258 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
        $uId = UniqueId::get();
261
262
        $return = $this->getInstance()->set($namespace, $uId);
263
264
        return $return === false ? false : $uId;
265
266
    }
267
268
    /**
269
     * Get namespace key
270
     *
271
     * @return  string
272
     */
273
    private function getNamespaceKey($namespace) {
274
275
        return $this->getInstance()->get($namespace);
276
277
    }
278
279
}
280