Predis::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Dtc\QueueBundle\Redis;
4
5
use Predis\Client;
0 ignored issues
show
Bug introduced by
The type Predis\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Predis\Response\Status;
0 ignored issues
show
Bug introduced by
The type Predis\Response\Status was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Predis\Transaction\MultiExec;
0 ignored issues
show
Bug introduced by
The type Predis\Transaction\MultiExec was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public function set($key, $value)
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
    public function setEx($key, $seconds, $value)
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
    public function zScan($key, &$cursor, $pattern = '', $count = 0)
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
    public function hScan($key, &$cursor, $pattern = '', $count = 0)
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
                $zRange = $tx->zrange($key, 0, 0);
164 1
                $element = isset($zRange[0]) ? $zRange[0] : null;
165
166 1
                if (isset($element)) {
167 1
                    $tx->multi();
168 1
                    $tx->zrem($key, $element);
169
                }
170 1
            });
171
        } catch (\Exception $exception) {
172
            return null;
173
        }
174
175 1
        return $element;
176
    }
177
178 1
    public function zPopByMaxScore($key, $max)
179
    {
180 1
        $element = null;
181
        $options = [
182 1
            'cas' => true,
183 1
            'watch' => $key,
184 1
            'retry' => $this->maxRetries,
185
        ];
186
187
        try {
188 1
            $this->predis->transaction($options, function (MultiExec $tx) use ($key, $max, &$element) {
189 1
                $zRangeByScore = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]);
190 1
                $element = isset($zRangeByScore[0]) ? $zRangeByScore[0] : null;
191
192 1
                if (isset($element)) {
193 1
                    $tx->multi();
194 1
                    $tx->zrem($key, $element);
195
                }
196 1
            });
197
        } catch (\Exception $exception) {
198
            return null;
199
        }
200
201 1
        return $element;
202
    }
203
}
204