Completed
Pull Request — master (#30)
by Matthew
07:24
created

Predis   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 106
ccs 37
cts 47
cp 0.7872
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 4 1
A get() 0 4 1
A setEx() 0 4 1
A lRem() 0 4 1
A lPush() 0 4 1
A lRange() 0 4 1
A del() 0 4 1
A __construct() 0 5 1
A zAdd() 0 4 1
A zRem() 0 4 1
B zPop() 0 24 3
B zPopByMaxScore() 0 24 3
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
        ];
71
72
        try {
73 12
            $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
                }
80 12
            });
81
        } 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
        ];
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
                }
105 12
            });
106
        } catch (\Exception $exception) {
107
            return null;
108
        }
109
110
        return $element;
111
    }
112
}
113