Completed
Push — master ( 165f6b...2c48ba )
by Matthew
07:36 queued 19s
created

Predis::hGetAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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