Completed
Pull Request — master (#30)
by Matthew
23:29 queued 08:10
created

Predis::zPopByMaxScore()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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