Completed
Pull Request — master (#40)
by Matthew
07:31
created

Predis   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 130
Duplicated Lines 14.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.72%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 19
loc 130
ccs 59
cts 61
cp 0.9672
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A zCount() 0 4 1
A zAdd() 0 4 1
A set() 10 10 2
A lRem() 0 4 1
A lPush() 0 4 1
A lRange() 0 4 1
A del() 0 4 1
A zRem() 0 4 1
B zPop() 0 24 3
A get() 0 12 2
A setEx() 9 9 2
B zPopByMaxScore() 0 24 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        ];
97
98
        try {
99 15
            $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
                }
106 15
            });
107 10
        } 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
        ];
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
                }
131 15
            });
132
        } catch (\Exception $exception) {
133
            return null;
134
        }
135
136
        return $element;
137
    }
138
}
139