Completed
Push — master ( c76232...ce4950 )
by Matthew
27:26 queued 01:28
created

Predis::lRem()   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 3
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 19
    public function zAdd($zkey, $score, $value)
21
    {
22 19
        return $this->predis->zadd($zkey, [$value => $score]);
23
    }
24
25 19 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...
26
    {
27
        /** @var Status $result */
28 19
        $result = $this->predis->set($key, $value);
29 19
        if ('OK' == $result->getPayload()) {
30 19
            return true;
31
        }
32
33
        return false;
34
    }
35
36 13
    public function get($key)
37
    {
38 13
        $this->predis->multi();
39 13
        $this->predis->exists($key);
40 13
        $this->predis->get($key);
41 13
        list($exists, $result) = $this->predis->exec();
42 13
        if (!$exists) {
43 1
            return false;
44
        }
45
46 13
        return $result;
47
    }
48
49 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...
50
    {
51 1
        $result = $this->predis->setex($key, $seconds, $value);
52 1
        if ('OK' == $result->getPayload()) {
53 1
            return true;
54
        }
55
56
        return false;
57
    }
58
59 15
    public function lRem($lKey, $count, $value)
60
    {
61 15
        return $this->predis->lrem($lKey, $count, $value);
62
    }
63
64 19
    public function lPush($lKey, array $values)
65
    {
66 19
        return $this->predis->lpush($lKey, $values);
67
    }
68
69 3
    public function lRange($lKey, $start, $stop)
70
    {
71 3
        return $this->predis->lrange($lKey, $start, $stop);
72
    }
73
74 15
    public function del(array $keys)
75
    {
76 15
        return $this->predis->del($keys);
77
    }
78
79 5
    public function zRem($zkey, $value)
80
    {
81 5
        return $this->predis->zrem($zkey, $value);
82
    }
83
84 15
    public function zPop($key)
85
    {
86 15
        $element = null;
87
        $options = [
88 15
            'cas' => true,
89 15
            'watch' => $key,
90 15
            'retry' => $this->maxRetries,
91
        ];
92
93
        try {
94 15
            $this->predis->transaction($options, function (MultiExec $tx) use ($key, &$element) {
95 15
                list($element) = $tx->zrange($key, 0, 0);
96
97 13
                if (isset($element)) {
98 13
                    $tx->multi();
99 13
                    $tx->zrem($key, $element);
100
                }
101 15
            });
102 10
        } catch (\Exception $exception) {
103 10
            return null;
104
        }
105
106 13
        return $element;
107
    }
108
109 15
    public function zPopByMaxScore($key, $max)
110
    {
111 15
        $element = null;
112
        $options = [
113 15
            'cas' => true,
114 15
            'watch' => $key,
115 15
            'retry' => $this->maxRetries,
116
        ];
117
118
        try {
119 15
            $this->predis->transaction($options, function (MultiExec $tx) use ($key, $max, &$element) {
120 15
                list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]);
121
122 13
                if (isset($element)) {
123 13
                    $tx->multi();
124 13
                    $tx->zrem($key, $element);
125
                }
126 15
            });
127
        } catch (\Exception $exception) {
128
            return null;
129
        }
130
131
        return $element;
132
    }
133
}
134