Completed
Pull Request — master (#30)
by Matthew
19:54
created

Predis   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 106
rs 10
c 1
b 0
f 0

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
    public function zAdd($zkey, $score, $value)
19
    {
20
        return $this->predis->zadd($zkey, [$value => $score]);
21
    }
22
23
    public function set($key, $value)
24
    {
25
        return $this->predis->set($key, $value);
26
    }
27
28
    public function get($key)
29
    {
30
        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
    public function lRem($lKey, $count, $value)
39
    {
40
        return $this->predis->lrem($lKey, $count, $value);
41
    }
42
43
    public function lPush($lKey, array $values)
44
    {
45
        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
    public function del(array $keys)
54
    {
55
        return $this->predis->del($keys);
56
    }
57
58
    public function zRem($zkey, $value)
59
    {
60
        return $this->predis->zrem($zkey, $value);
61
    }
62
63
    public function zPop($key)
64
    {
65
        $element = null;
66
        $options = [
67
            'cas' => true,
68
            'watch' => $key,
69
            'retry' => $this->maxRetries,
70
        ];
71
72
        try {
73
            $this->predis->transaction($options, function ($tx) use ($key, &$element) {
74
                @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
                if (isset($element)) {
77
                    $tx->multi();
78
                    $tx->zrem($key, $element);
79
                }
80
            });
81
        } catch (\Exception $exception) {
82
            return null;
83
        }
84
85
        return $element;
86
    }
87
88
    public function zPopByMaxScore($key, $max)
89
    {
90
        $element = null;
91
        $options = [
92
            'cas' => true,
93
            'watch' => $key,
94
            'retry' => $this->maxRetries,
95
        ];
96
97
        try {
98
            $this->predis->transaction($options, function ($tx) use ($key, $max, &$element) {
99
                @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
                if (isset($element)) {
102
                    $tx->multi();
103
                    $tx->zrem($key, $element);
104
                }
105
            });
106
        } catch (\Exception $exception) {
107
            return null;
108
        }
109
110
        return $element;
111
    }
112
}
113