Completed
Push — master ( 7fae80...8ffdf8 )
by Matthew
07:25
created

Predis   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 193
Duplicated Lines 17.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.39%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 3
dl 33
loc 193
ccs 85
cts 92
cp 0.9239
rs 9.3999
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A zCount() 0 4 1
A zAdd() 0 4 1
A hGetAll() 0 4 1
A lRem() 0 4 1
A lPush() 0 4 1
A lRange() 0 4 1
A del() 0 4 1
A zRem() 0 4 1
A hIncrBy() 0 4 1
A set() 10 10 2
A get() 0 12 2
A setEx() 9 9 2
A zScan() 7 7 1
A mGet() 0 4 1
A getResults() 0 11 3
A setCursor() 0 6 2
A hScan() 7 7 1
B zPopByMaxScore() 0 24 3
A getOptions() 0 12 3
B zPop() 0 24 3

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
2
3
namespace Dtc\QueueBundle\Redis;
4
5
use Predis\Client;
6
use Predis\Response\Status;
7
use Predis\Transaction\MultiExec;
8
9
class Predis implements RedisInterface
10
{
11
    protected $predis;
12
    protected $maxRetries;
13
14 1
    public function __construct(Client $predis, $maxRetries = 5)
15
    {
16 1
        $this->predis = $predis;
17 1
        $this->maxRetries = $maxRetries;
18 1
    }
19
20 1
    public function zCount($key, $min, $max)
21
    {
22 1
        return $this->predis->zcount($key, $min, $max);
23
    }
24
25 1
    public function zAdd($zkey, $score, $value)
26
    {
27 1
        return $this->predis->zadd($zkey, [$value => $score]);
28
    }
29
30 1
    public function hGetAll($key)
31
    {
32 1
        return $this->predis->hGetAll($key);
33
    }
34
35 1
    public function hIncrBy($key, $hashKey, $value)
36
    {
37 1
        return $this->predis->hIncrBy($key, $hashKey, $value);
38
    }
39
40 1 View Code Duplication
    public function set($key, $value)
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...
41
    {
42
        /** @var Status $result */
43 1
        $result = $this->predis->set($key, $value);
44 1
        if ('OK' == $result->getPayload()) {
45 1
            return true;
46
        }
47
48
        return false;
49
    }
50
51 1
    public function get($key)
52
    {
53 1
        $this->predis->multi();
54 1
        $this->predis->exists($key);
55 1
        $this->predis->get($key);
56 1
        list($exists, $result) = $this->predis->exec();
57 1
        if (!$exists) {
58 1
            return false;
59
        }
60
61 1
        return $result;
62
    }
63
64 1 View Code Duplication
    public function setEx($key, $seconds, $value)
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...
65
    {
66 1
        $result = $this->predis->setex($key, $seconds, $value);
67 1
        if ('OK' == $result->getPayload()) {
68 1
            return true;
69
        }
70
71
        return false;
72
    }
73
74 1
    public function lRem($lKey, $count, $value)
75
    {
76 1
        return $this->predis->lrem($lKey, $count, $value);
77
    }
78
79 1
    public function lPush($lKey, array $values)
80
    {
81 1
        return $this->predis->lpush($lKey, $values);
82
    }
83
84 1
    public function lRange($lKey, $start, $stop)
85
    {
86 1
        return $this->predis->lrange($lKey, $start, $stop);
87
    }
88
89 1
    public function del(array $keys)
90
    {
91 1
        return $this->predis->del($keys);
92
    }
93
94 1
    public function zRem($zkey, $value)
95
    {
96 1
        return $this->predis->zrem($zkey, $value);
97
    }
98
99 1
    protected function getOptions($pattern = '', $count = 0)
100
    {
101 1
        $options = [];
102 1
        if ('' !== $pattern) {
103
            $options['MATCH'] = $pattern;
104
        }
105 1
        if (0 !== $count) {
106
            $options['COUNT'] = $count;
107
        }
108
109 1
        return $options;
110
    }
111
112 1 View Code Duplication
    public function zScan($key, &$cursor, $pattern = '', $count = 0)
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...
113
    {
114 1
        $this->setCursor($cursor);
115 1
        $results = $this->predis->zscan($key, $cursor, $this->getOptions($pattern, $count));
116
117 1
        return $this->getResults($results, $cursor);
118
    }
119
120 1
    public function mGet(array $keys)
121
    {
122 1
        return $this->predis->mget($keys);
123
    }
124
125 1
    protected function getResults(&$results, &$cursor)
126
    {
127 1
        if (isset($results[0])) {
128 1
            $cursor = intval($results[0]);
129
        }
130 1
        if (isset($results[1])) {
131 1
            return $results[1];
132
        }
133
134
        return [];
135
    }
136
137 1
    protected function setCursor(&$cursor)
138
    {
139 1
        if (null === $cursor) {
140 1
            $cursor = 0;
141
        }
142 1
    }
143
144 1 View Code Duplication
    public function hScan($key, &$cursor, $pattern = '', $count = 0)
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...
145
    {
146 1
        $this->setCursor($cursor);
147 1
        $results = $this->predis->hscan($key, $cursor, $this->getOptions($pattern, $count));
148
149 1
        return $this->getResults($results, $cursor);
150
    }
151
152 1
    public function zPop($key)
153
    {
154 1
        $element = null;
155
        $options = [
156 1
            'cas' => true,
157 1
            'watch' => $key,
158 1
            'retry' => $this->maxRetries,
159
        ];
160
161
        try {
162 1
            $this->predis->transaction($options, function (MultiExec $tx) use ($key, &$element) {
163 1
                list($element) = $tx->zrange($key, 0, 0);
164
165 1
                if (isset($element)) {
166 1
                    $tx->multi();
167 1
                    $tx->zrem($key, $element);
168
                }
169 1
            });
170
        } catch (\Exception $exception) {
171
            return null;
172
        }
173
174 1
        return $element;
175
    }
176
177 1
    public function zPopByMaxScore($key, $max)
178
    {
179 1
        $element = null;
180
        $options = [
181 1
            'cas' => true,
182 1
            'watch' => $key,
183 1
            'retry' => $this->maxRetries,
184
        ];
185
186
        try {
187 1
            $this->predis->transaction($options, function (MultiExec $tx) use ($key, $max, &$element) {
188 1
                list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]);
189
190 1
                if (isset($element)) {
191 1
                    $tx->multi();
192 1
                    $tx->zrem($key, $element);
193
                }
194 1
            });
195
        } catch (\Exception $exception) {
196
            return null;
197
        }
198
199
        return $element;
200
    }
201
}
202