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

Predis::zAdd()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Dtc\QueueBundle\Redis;
4
5
use Predis\Client;
6
7
class Predis implements RedisInterface
8
{
9
    protected $predis;
10
    protected $maxRetries;
11
12
    public function __construct(Client $predis, $maxRetries = 5)
13
    {
14
        $this->predis = $predis;
15
        $this->maxRetries = $maxRetries;
16
    }
17
18 16
    public function zAdd($zkey, $score, $value)
19
    {
20 16
        return $this->predis->zadd($zkey, [$value => $score]);
21
    }
22
23 16
    public function set($key, $value)
24
    {
25 16
        return $this->predis->set($key, $value);
26
    }
27
28 10
    public function get($key)
29
    {
30 10
        return $this->predis->get($key);
31
    }
32
33
    public function setEx($key, $seconds, $value)
34
    {
35
        return $this->predis->setex($key, $seconds, $value);
36
    }
37
38 12
    public function lRem($lKey, $count, $value)
39
    {
40 12
        return $this->predis->lrem($lKey, $count, $value);
41
    }
42
43 16
    public function lPush($lKey, array $values)
44
    {
45 16
        return $this->predis->lpush($lKey, $values);
46
    }
47
48
    public function lRange($lKey, $start, $stop)
49
    {
50
        return $this->predis->lrange($lKey, $start, $stop);
51
    }
52
53 12
    public function del(array $keys)
54
    {
55 12
        return $this->predis->del($keys);
56
    }
57
58 2
    public function zRem($zkey, $value)
59
    {
60 2
        return $this->predis->zrem($zkey, $value);
61
    }
62
63 12
    public function zPop($key)
64
    {
65 12
        $element = null;
66
        $options = [
67 12
            'cas' => true,
68 12
            'watch' => $key,
69 12
            'retry' => $this->maxRetries,
70 12
        ];
71
72
        try {
73
            $this->predis->transaction($options, function ($tx) use ($key, &$element) {
74 12
                @list($element) = $tx->zrange($key, 0, 0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
75
76 12
                if (isset($element)) {
77 10
                    $tx->multi();
78 10
                    $tx->zrem($key, $element);
79 10
                }
80 12
            });
81 12
        } catch (\Exception $exception) {
82
            return null;
83
        }
84
85 12
        return $element;
86
    }
87
88 12
    public function zPopByMaxScore($key, $max)
89
    {
90 12
        $element = null;
91
        $options = [
92 12
            'cas' => true,
93 12
            'watch' => $key,
94 12
            'retry' => $this->maxRetries,
95 12
        ];
96
97
        try {
98 12
            $this->predis->transaction($options, function ($tx) use ($key, $max, &$element) {
99 12
                @list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
100
101 12
                if (isset($element)) {
102 10
                    $tx->multi();
103 10
                    $tx->zrem($key, $element);
104 10
                }
105 12
            });
106 12
        } catch (\Exception $exception) {
107
            return null;
108
        }
109
110 12
        return $element;
111
    }
112
}
113