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

PhpRedis::mGet()   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
class PhpRedis implements RedisInterface
6
{
7
    protected $redis;
8
    protected $maxRetries;
9
10 1
    public function __construct(\Redis $redis, $maxRetries = 5)
11
    {
12 1
        $this->redis = $redis;
13 1
        $this->maxRetries = $maxRetries;
14 1
    }
15
16 1
    public function mGet(array $keys)
17
    {
18 1
        return $this->redis->mGet($keys);
19
    }
20
21 1
    public function hScan($key, &$cursor, $pattern = '', $count = 0)
22
    {
23 1
        return $this->redis->hScan($key, $cursor, $pattern, $count);
24
    }
25
26 1
    public function zScan($key, &$cursor, $pattern = '', $count = 0)
27
    {
28 1
        return $this->redis->zScan($key, $cursor, $pattern, $count);
29
    }
30
31 1
    public function zCount($key, $min, $max)
32
    {
33 1
        return $this->redis->zCount($key, $min, $max);
34
    }
35
36 1
    public function zAdd($zkey, $score, $value)
37
    {
38 1
        return $this->redis->zadd($zkey, $score, $value);
39
    }
40
41 1
    public function set($key, $value)
42
    {
43 1
        return $this->redis->set($key, $value);
44
    }
45
46 1
    public function get($key)
47
    {
48 1
        return $this->redis->get($key);
49
    }
50
51 1
    public function hIncrBy($key, $hashKey, $value)
52
    {
53 1
        return $this->redis->hIncrBy($key, $hashKey, $value);
54
    }
55
56 1
    public function hGetAll($key)
57
    {
58 1
        return $this->redis->hGetAll($key);
59
    }
60
61 1
    public function setEx($key, $seconds, $value)
62
    {
63 1
        return $this->redis->setex($key, $seconds, $value);
64
    }
65
66 1
    public function lRem($lKey, $count, $value)
67
    {
68 1
        return $this->redis->lrem($lKey, $value, $count);
69
    }
70
71 1
    public function lPush($lKey, array $values)
72
    {
73 1
        $args = $values;
74 1
        array_unshift($args, $lKey);
75
76 1
        return call_user_func_array([$this->redis, 'lPush'], $args);
77
    }
78
79 1
    public function lRange($lKey, $start, $stop)
80
    {
81 1
        return $this->redis->lrange($lKey, $start, $stop);
82
    }
83
84 1
    public function del(array $keys)
85
    {
86 1
        return $this->redis->del($keys);
87
    }
88
89 1
    public function zRem($zkey, $value)
90
    {
91 1
        return $this->redis->zrem($zkey, $value);
92
    }
93
94 1
    public function zPop($key)
95
    {
96 1
        $retries = 0;
97
        do {
98 1
            $this->redis->watch($key);
99 1
            $elements = $this->redis->zrange($key, 0, 0);
100 1
            if (empty($elements)) {
101
                $this->redis->unwatch();
102
103
                return null;
104
            }
105 1
            $result = $this->redis->multi()
106 1
                ->zrem($key, $elements[0])
107 1
                ->exec();
108 1
            if (false !== $result) {
109 1
                return $elements[0];
110
            }
111
            ++$retries;
112
        } while ($retries < $this->maxRetries);
113
114
        return null;
115
    }
116
117 1
    public function zPopByMaxScore($key, $max)
118
    {
119 1
        $retries = 0;
120
        do {
121 1
            $this->redis->watch($key);
122 1
            $elements = $this->redis->zrangebyscore($key, 0, $max, ['limit' => [0, 1]]);
123 1
            if (empty($elements)) {
124 1
                $this->redis->unwatch();
125
126 1
                return null;
127
            }
128 1
            $result = $this->redis->multi()
129 1
                ->zrem($key, $elements[0])
130 1
                ->exec();
131 1
            if (false !== $result) {
132 1
                return $elements[0];
133
            }
134
            ++$retries;
135
        } while ($retries < $this->maxRetries);
136
137
        return null;
138
    }
139
}
140