Completed
Pull Request — master (#40)
by Matthew
07:31
created

PhpRedis   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 110
ccs 48
cts 56
cp 0.8571
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A zCount() 0 4 1
A zAdd() 0 4 1
A set() 0 4 1
A get() 0 4 1
A setEx() 0 4 1
A lRange() 0 4 1
A del() 0 4 1
A zRem() 0 4 1
A lRem() 0 4 1
A lPush() 0 7 1
B zPop() 0 22 4
B zPopByMaxScore() 0 22 4
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 zCount($key, $min, $max)
17
    {
18 1
        return $this->redis->zCount($key, $min, $max);
19
    }
20
21 1
    public function zAdd($zkey, $score, $value)
22
    {
23 1
        return $this->redis->zadd($zkey, $score, $value);
24
    }
25
26 1
    public function set($key, $value)
27
    {
28 1
        return $this->redis->set($key, $value);
29
    }
30
31 1
    public function get($key)
32
    {
33 1
        return $this->redis->get($key);
34
    }
35
36 1
    public function setEx($key, $seconds, $value)
37
    {
38 1
        return $this->redis->setex($key, $seconds, $value);
39
    }
40
41 1
    public function lRem($lKey, $count, $value)
42
    {
43 1
        return $this->redis->lrem($lKey, $value, $count);
44
    }
45
46 1
    public function lPush($lKey, array $values)
47
    {
48 1
        $args = $values;
49 1
        array_unshift($args, $lKey);
50
51 1
        return call_user_func_array([$this->redis, 'lPush'], $args);
52
    }
53
54 1
    public function lRange($lKey, $start, $stop)
55
    {
56 1
        return $this->redis->lrange($lKey, $start, $stop);
57
    }
58
59 1
    public function del(array $keys)
60
    {
61 1
        return $this->redis->del($keys);
62
    }
63
64 1
    public function zRem($zkey, $value)
65
    {
66 1
        return $this->redis->zrem($zkey, $value);
67
    }
68
69 1
    public function zPop($key)
70
    {
71 1
        $retries = 0;
72
        do {
73 1
            $this->redis->watch($key);
74 1
            $elements = $this->redis->zrange($key, 0, 0);
75 1
            if (empty($elements)) {
76
                $this->redis->unwatch();
77
78
                return null;
79
            }
80 1
            $result = $this->redis->multi()
81 1
                ->zrem($key, $elements[0])
82 1
                ->exec();
83 1
            if (false !== $result) {
84 1
                return $elements[0];
85
            }
86
            ++$retries;
87
        } while ($retries < $this->maxRetries);
88
89
        return null;
90
    }
91
92 1
    public function zPopByMaxScore($key, $max)
93
    {
94 1
        $retries = 0;
95
        do {
96 1
            $this->redis->watch($key);
97 1
            $elements = $this->redis->zrangebyscore($key, 0, $max, ['limit' => [0, 1]]);
98 1
            if (empty($elements)) {
99 1
                $this->redis->unwatch();
100
101 1
                return null;
102
            }
103 1
            $result = $this->redis->multi()
104 1
                ->zrem($key, $elements[0])
105 1
                ->exec();
106 1
            if (false !== $result) {
107 1
                return $elements[0];
108
            }
109
            ++$retries;
110
        } while ($retries < $this->maxRetries);
111
112
        return null;
113
    }
114
}
115