Completed
Pull Request — master (#40)
by Matthew
19:38 queued 15:57
created

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