Completed
Pull Request — master (#44)
by Matthew
16:45
created

Predis::zPop()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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