Completed
Pull Request — master (#30)
by Matthew
14:09 queued 11:28
created

PhpRedis   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 105
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

12 Methods

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