Completed
Push — master ( cd674c...612bb3 )
by Kamil
12s
created

ApiSetSortedTrait::zRangeByScore()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

Changes 0
Metric Value
dl 28
loc 28
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444
rs 5.3846
cc 8
eloc 19
nc 8
nop 6
crap 8.0109
1
<?php
2
3
namespace Dazzle\Redis\Command\Compose;
4
5
use Dazzle\Redis\Command\Builder;
6
use Dazzle\Redis\Command\Enum;
7
use Dazzle\Redis\Driver\Request;
8
9
trait ApiSetSortedTrait
10
{
11
    /**
12
     * @param Request $request
13
     * @return mixed
14
     */
15
    abstract function dispatch(Request $request);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
17
    /**
18
     * @override
19
     * @inheritDoc
20
     */
21 16 View Code Duplication
    public function zAdd($key, array $options = [], ...$scoreMembers)
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...
22
    {
23 16
        $command = Enum::ZADD;
24 16
        $args = array_merge([$key], $options, $scoreMembers);
25
26 16
        return $this->dispatch(Builder::build($command, $args));
27
    }
28
29
    /**
30
     * @override
31
     * @inheritDoc
32
     */
33 1 View Code Duplication
    public function zCard($key)
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...
34
    {
35 1
        $command = Enum::ZCARD;
36 1
        $args = [$key];
37
38 1
        return $this->dispatch(Builder::build($command, $args));
39
    }
40
41
    /**
42
     * @override
43
     * @inheritDoc
44
     */
45 1 View Code Duplication
    public function zCount($key, $min, $max)
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...
46
    {
47 1
        $command = Enum::ZCOUNT;
48 1
        $args = [$key, $min, $max];
49
50 1
        return $this->dispatch(Builder::build($command, $args));
51
    }
52
53
    /**
54
     * @override
55
     * @inheritDoc
56
     */
57 1
    public function zIncrBy($key, $increment, $member)
58
    {
59 1
        $command = Enum::ZINCRBY;
60 1
        $args = [$key, $increment, $member];
61
62 1
        return $this->dispatch(Builder::build($command, $args));
63
    }
64
65
    /**
66
     * @override
67
     * @inheritDoc
68
     */
69
    public function zInterStore($dst, $numKeys)
70
    {
71
        $command = Enum::ZINTERSTORE;
72
        $args = [$dst, $numKeys];
73
74
        return $this->dispatch(Builder::build($command, $args));
75
    }
76
77
    /**
78
     * @override
79
     * @inheritDoc
80
     */
81 1 View Code Duplication
    public function zLexCount($key, $min, $max)
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...
82
    {
83 1
        $command = Enum::ZLEXCOUNT;
84 1
        $args = [$key, $min, $max];
85
86 1
        return $this->dispatch(Builder::build($command, $args));
87
    }
88
89
    /**
90
     * @override
91
     * @inheritDoc
92
     */
93 1
    public function zRange($key, $star = 0, $stop = -1, $withScores = false)
94
    {
95 1
        $command = Enum::ZRANGE;
96 1
        $args = [$key, $star, $stop];
97 1
        if ($withScores) {
98 1
            $args[] = 'WITHSCORES';
99
            return $this->dispatch(Builder::build($command, $args))->then(function ($value) {
100 1
                $len = count($value);
101 1
                $ret = [];
102 1
                for ($i=0; $i<$len; $i+=2) {
103 1
                    $ret[$value[$i]] = $value[$i+1];
104
                }
105 1
                return $ret;
106 1
            });
107
        }
108
109
        return $this->dispatch(Builder::build($command, $args));
110
    }
111
112
    /**
113
     * @override
114
     * @inheritDoc
115
     */
116 1
    public function zRangeByLex($key, $min, $max, array $options = [])
117
    {
118 1
        $command = Enum::ZRANGEBYLEX;
119 1
        $args = [$key, $min, $max];
120 1
        $args = array_merge($args, $options);
121
122 1
        return $this->dispatch(Builder::build($command, $args));
123
    }
124
125
    /**
126
     * @override
127
     * @inheritDoc
128
     */
129 1
    public function zRevRangeByLex($key, $max, $min, array $options = [])
130
    {
131 1
        $command = Enum::ZREVRANGEBYLEX;
132 1
        $args = [$key, $max,$min];
133 1
        $args = array_merge($args,$options);
134
135 1
        return $this->dispatch(Builder::build($command, $args));
136
    }
137
138
    /**
139
     * @override
140
     * @inheritDoc
141
     */
142 1 View Code Duplication
    public function zRangeByScore($key, $min, $max, $withScores = false, $offset = 0, $count = 0)
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...
143
    {
144 1
        $command = Enum::ZRANGEBYSCORE;
145 1
        $args = [$key, $min, $max];
146 1
        if ($withScores === true) {
147 1
            $args[] = 'WITHSCORES';
148
        }
149 1
        if ($offset != 0 || $count != 0) {
150 1
            $args[] = 'LIMIT';
151 1
            $args[] = $offset;
152 1
            $args[] = $count;
153
        }
154 1
        $promise = $this->dispatch(Builder::build($command, $args));
155
156
        return $withScores ? $promise->then(function ($value) {
157 1
            $len = is_array($value) ? count($value) : 0;
158 1
            if ($len > 0) {
159 1
                $ret = [];
160 1
                for ($i=0; $i<$len; $i+=2) {
161 1
                    $ret[$value[$i]] = $value[$i+1];
162
                }
163
164 1
                return $ret;
165
            }
166
167
            return $value;
168 1
        } ) : $promise;
169
    }
170
171
    /**
172
     * @override
173
     * @inheritDoc
174
     */
175 1
    public function zRank($key, $member)
176
    {
177 1
        $command = Enum::ZRANK;
178 1
        $args = [$key,$member];
179
180 1
        return $this->dispatch(Builder::build($command, $args));
181
    }
182
183
    /**
184
     * @override
185
     * @inheritDoc
186
     */
187 1
    public function zRem($key, ...$members)
188
    {
189 1
        $command = Enum::ZREM;
190 1
        $args = [$key];
191 1
        $args = array_merge($args, $members);
192
193 1
        return $this->dispatch(Builder::build($command, $args));
194
    }
195
196
    /**
197
     * @override
198
     * @inheritDoc
199
     */
200 1
    public function zRemRangeByLex($key, $min, $max, array $options = [])
201
    {
202 1
        $command = Enum::ZREMRANGEBYLEX;
203 1
        $args = [$key, $min, $max];
204 1
        $args = array_merge($args, $options);
205
206 1
        return $this->dispatch(Builder::build($command, $args));
207
    }
208
209
    /**
210
     * @override
211
     * @inheritDoc
212
     */
213 1
    public function zRemRangeByRank($key, $start, $stop)
214
    {
215 1
        $command = Enum::ZREMRANGEBYRANK;
216 1
        $args = [$key, $start,$stop];
217
218 1
        return $this->dispatch(Builder::build($command, $args));
219
    }
220
221
    /**
222
     * @override
223
     * @inheritDoc
224
     */
225 1
    public function zRemRangeByScore($key, $min, $max, array $options = [])
226
    {
227 1
        $command = Enum::ZREMRANGEBYSCORE;
228 1
        $args = [$key, $min, $max];
229 1
        $args = array_merge($args, $options);
230
231 1
        return $this->dispatch(Builder::build($command, $args));
232
    }
233
234
    /**
235
     * @override
236
     * @inheritDoc
237
     */
238 1
    public function zRevRange($key, $start, $stop, $withScores = false)
239
    {
240 1
        $command = Enum::ZREVRANGE;
241 1
        $args = [$key, $start, $stop];
242
243 1
        if ($withScores === true) {
244 1
            $args[] = 'WITHSCORES';
245
            
246 1
            return $this->dispatch(Builder::build($command, $args))
247
            ->then(function ($value) {
248 1
                $len = is_array($value) ? count($value) : 0;
249 1
                if ($len > 0) {
250 1
                    $ret = [];
251 1
                    for ($i=0; $i<$len; $i+=2) {
252 1
                        $member = $value[$i];
253 1
                        $score = $value[$i+1];
254 1
                        $ret[$member] = $score;
255
                    }
256
257 1
                    return $ret;
258
                }
259
260
                return $value; 
261 1
            });
262
        } 
263
264
        return $promise = $this->dispatch(Builder::build($command, $args));
0 ignored issues
show
Unused Code introduced by
$promise is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
265
    }
266
267
    /**
268
     * @override
269
     * @inheritDoc
270
     */
271 1 View Code Duplication
    public function zRevRangeByScore($key, $max, $min, $withScores = false, $offset = 0, $count = 0)
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...
272
    {
273 1
        $command = Enum::ZREVRANGEBYSCORE;
274 1
        $args = [$key, $max, $min];
275 1
        if ($withScores === true) {
276 1
            $args[] = 'WITHSCORES';
277
        }
278 1
        if ($offset != 0 || $count != 0) {
279 1
            $args[] = 'LIMIT';
280 1
            $args[] = $offset;
281 1
            $args[] = $count;
282
        }
283 1
        $promise = $this->dispatch(Builder::build($command, $args));
284
285 1
        return $withScores ? $promise->then(function ($value) {
286 1
            $len = is_array($value) ? count($value) : 0;
287 1
            if ($len > 0) {
288 1
                $ret = [];
289 1
                for ($i=0; $i<$len; $i+=2) {
290 1
                    $ret[$value[$i]] = $value[$i+1];
291
                }
292
293 1
                return $ret;
294
            }
295
296
            return $value;
297 1
        } ) : $promise;
298
    }
299
300
    /**
301
     * @override
302
     * @inheritDoc
303
     */
304 1
    public function zRevRank($key, $member)
305
    {
306 1
        $command = Enum::ZREVRANK;
307 1
        $args = [$key,$member];
308
309 1
        return $this->dispatch(Builder::build($command, $args));
310
    }
311
312
    /**
313
     * @override
314
     * @inheritDoc
315
     */
316
    public function zScore($key, $member)
317
    {
318
        $command = Enum::ZSCORE;
319
        $args = [$key,$member];
320
321
        return $this->dispatch(Builder::build($command, $args));
322
    }
323
324
    /**
325
     * @override
326
     * @inheritDoc
327
     */
328
    public function zScan($key, $cursor, array $options = [])
329
    {
330
        $command = Enum::ZSCAN;
331
        $args = [$key , $cursor];
332
        $args = array_merge($args, $options);
333
334
        return $this->dispatch(Builder::build($command, $args));
335
    }
336
337
    /**
338
     * @inheritDoc
339
     */
340
    public function zUnionScore($dst, $numKeys)
341
    {
342
        $command = Enum::ZUNIIONSCORE;
343
        $args = [$dst, $numKeys];
344
345
        return $this->dispatch(Builder::build($command, $args));
346
    }
347
}