Completed
Pull Request — master (#5)
by Michal
02:47
created

RedisProxy::incrbyfloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace RedisProxy;
4
5
use Exception;
6
use Predis\Client;
7
use Predis\Response\Status;
8
use Redis;
9
10
/**
11
 * @method mixed config(string $command, $argument = null)
12
 * @method int dbsize() Return the number of keys in the selected database
13
 * @method boolean set(string $key, string $value) Set the string value of a key
14
 * @method boolean setex(string $key, int $seconds, string $value) Set the value and expiration of a key
15
 * @method int ttl(string $key) Get the time to live for a key, returns TTL in seconds, -2 if the key does not exist, -1 if the key exists but has no associated expire
16
 * @method int pttl(string $key) Get the time to live for a key in milliseconds, returns TTL in miliseconds, -2 if the key does not exist, -1 if the key exists but has no associated expire
17
 * @method array keys(string $pattern) Find all keys matching the given pattern
18
 * @method int hset(string $key, string $field, string $value) Set the string value of a hash field
19
 * @method array hkeys(string $key) Get all fields in a hash (without values)
20
 * @method array hgetall(string $key) Get all fields and values in a hash
21
 * @method int hlen(string $key) Get the number of fields in a hash
22
 * @method array smembers(string $key) Get all the members in a set
23
 * @method int scard(string $key) Get the number of members in a set
24
 * @method int llen(string $key) Get the length of a list
25
 * @method array lrange(string $key, int $start, int $stop) Get a range of elements from a list
26
 * @method int zcard(string $key) Get the number of members in a sorted set
27
 * @method boolean flushall() Remove all keys from all databases
28
 * @method boolean flushdb() Remove all keys from the current database
29
 */
30
class RedisProxy
31
{
32
    const DRIVER_REDIS = 'redis';
33
34
    const DRIVER_PREDIS = 'predis';
35
36
    const TYPE_STRING = 'string';
37
38
    const TYPE_SET = 'set';
39
40
    const TYPE_HASH = 'hash';
41
42
    const TYPE_LIST = 'list';
43
44
    const TYPE_SORTED_SET = 'sorted_set';
45
46
    private $driver;
47
48
    private $host;
49
50
    private $port;
51
52
    private $database = 0;
53
54
    private $selectedDatabase = 0;
55
56
    private $timeout;
57
58
    private $supportedDrivers = [
59
        self::DRIVER_REDIS,
60
        self::DRIVER_PREDIS,
61
    ];
62
63
    private $driversOrder = [];
64
65
    private $redisTypeMap = [
66
        Redis::REDIS_STRING => self::TYPE_STRING,
67
        Redis::REDIS_SET => self::TYPE_SET,
68
        Redis::REDIS_HASH => self::TYPE_HASH,
69
        Redis::REDIS_LIST => self::TYPE_LIST,
70
        Redis::REDIS_ZSET => self::TYPE_SORTED_SET,
71
    ];
72
73
    private $predisTypeMap = [
74
        'string' => self::TYPE_STRING,
75
        'set' => self::TYPE_SET,
76
        'hash' => self::TYPE_HASH,
77
        'list' => self::TYPE_LIST,
78
        'zset' => self::TYPE_SORTED_SET,
79
    ];
80
81 244
    public function __construct($host, $port, $database = 0, $timeout = null)
82
    {
83 244
        $this->host = $host;
84 244
        $this->port = $port;
85 244
        $this->database = $database;
86 244
        $this->timeout = $timeout;
87 244
        $this->driversOrder = $this->supportedDrivers;
88 244
    }
89
90
    /**
91
     * Set driver priorities - default is 1. redis, 2. predis
92
     * @param array $driversOrder
93
     * @return RedisProxy
94
     * @throws RedisProxyException if some driver is not supported
95
     */
96 244
    public function setDriversOrder(array $driversOrder)
97
    {
98 244
        foreach ($driversOrder as $driver) {
99 242
            if (!in_array($driver, $this->supportedDrivers)) {
100 2
                throw new RedisProxyException('Driver "' . $driver . '" is not supported');
101
            }
102 242
        }
103 242
        $this->driversOrder = $driversOrder;
104 242
        return $this;
105
    }
106
107 242
    private function init()
108
    {
109 242
        $this->prepareDriver();
110 240
        $this->select($this->database);
111 240
    }
112
113 242
    private function prepareDriver()
114
    {
115 242
        if ($this->driver !== null) {
116 240
            return;
117
        }
118
119 242
        foreach ($this->driversOrder as $preferredDriver) {
120 240
            if ($preferredDriver === self::DRIVER_REDIS && extension_loaded('redis')) {
121 120
                $this->driver = new Redis();
122 120
                return;
123
            }
124 120
            if ($preferredDriver === self::DRIVER_PREDIS && class_exists('Predis\Client')) {
125 120
                $this->driver = new Client();
126 120
                return;
127
            }
128 2
        }
129 2
        throw new RedisProxyException('No driver available');
130
    }
131
132
    /**
133
     * @return string|null
134
     */
135 242
    public function actualDriver()
136
    {
137 242
        if ($this->driver instanceof Redis) {
138 120
            return self::DRIVER_REDIS;
139
        }
140 126
        if ($this->driver instanceof Client) {
141 120
            return self::DRIVER_PREDIS;
142
        }
143 10
        return null;
144
    }
145
146 240
    private function connect($host, $port, $timeout = null)
147
    {
148 240
        return $this->driver->connect($host, $port, $timeout);
149
    }
150
151 240
    private function isConnected()
152
    {
153 240
        return $this->driver->isConnected();
154
    }
155
156 242
    public function __call($name, $arguments)
157
    {
158 242
        $this->init();
159 240
        $name = strtolower($name);
160
        try {
161 240
            $result = call_user_func_array([$this->driver, $name], $arguments);
162 240
        } catch (Exception $e) {
163 4
            throw new RedisProxyException("Error for command '$name', use getPrevious() for more info", 1484162284, $e);
164
        }
165 240
        return $this->transformResult($result);
166
    }
167
168
    /**
169
     * @param int $database
170
     * @return boolean true on success
171
     * @throws RedisProxyException on failure
172
     */
173 240
    public function select($database)
174
    {
175 240
        $this->prepareDriver();
176 240
        if (!$this->isConnected()) {
177 240
            $this->connect($this->host, $this->port, $this->timeout);
178 240
        }
179 240
        if ($database == $this->selectedDatabase) {
180 240
            return true;
181
        }
182
        try {
183 12
            $result = $this->driver->select($database);
184 12
        } catch (Exception $e) {
185 2
            throw new RedisProxyException('Invalid DB index');
186
        }
187 10
        $result = $this->transformResult($result);
188 10
        if ($result === false) {
189 2
            throw new RedisProxyException('Invalid DB index');
190
        }
191 8
        $this->database = $database;
192 8
        $this->selectedDatabase = $database;
193 8
        return $result;
194
    }
195
196
    /**
197
     * @param string $key
198
     * @return string|null
199
     */
200 4
    public function type($key)
201
    {
202 4
        $this->init();
203 4
        $result = $this->driver->type($key);
204 4
        if ($this->actualDriver() === self::DRIVER_REDIS) {
205 2
            if (isset($this->redisTypeMap[$result])) {
206 2
                return $this->redisTypeMap[$result];
207
            }
208 4
        } elseif ($this->actualDriver() === self::DRIVER_PREDIS && $result instanceof Status) {
209 2
            $result = $result->getPayload();
210 2
            if (isset($this->predisTypeMap[$result])) {
211 2
                return $this->predisTypeMap[$result];
212
            }
213 2
        }
214 4
        return null;
215
    }
216
217
    /**
218
     * @param string|null $section
219
     * @return array
220
     */
221 8
    public function info($section = null)
222
    {
223 8
        $this->init();
224 8
        $section = $section ? strtolower($section) : $section;
225 8
        $result = $section === null ? $this->driver->info() : $this->driver->info($section);
226
227 8
        $databases = $section === null || $section === 'keyspace' ? $this->config('get', 'databases')['databases'] : null;
228 8
        $groupedResult = InfoHelper::createInfoArray($this, $result, $databases);
229 8
        if ($section === null) {
230 4
            return $groupedResult;
231
        }
232 8
        if (isset($groupedResult[$section])) {
233 4
            return $groupedResult[$section];
234
        }
235 4
        throw new RedisProxyException('Info section "' . $section . '" doesn\'t exist');
236
    }
237
238
    /**
239
     * Get the value of a key
240
     * @param string $key
241
     * @return string|null null if key not set
242
     */
243 72
    public function get($key)
244
    {
245 72
        $this->init();
246 72
        $result = $this->driver->get($key);
247 72
        return $this->convertFalseToNull($result);
248
    }
249
250
    /**
251
     * Set the string value of a key and return its old value
252
     * @param string $key
253
     * @param string $value
254
     * @return string|null null if key was not set before
255
     */
256 4
    public function getset($key, $value)
257
    {
258 4
        $this->init();
259 4
        $result = $this->driver->getset($key, $value);
260 4
        return $this->convertFalseToNull($result);
261
    }
262
263
    /**
264
     * Set a key's time to live in seconds
265
     * @param string $key
266
     * @param int $seconds
267
     * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set
268
     */
269 8
    public function expire($key, $seconds)
270
    {
271 8
        $this->init();
272 8
        $result = $this->driver->expire($key, $seconds);
273 8
        return (bool)$result;
274
    }
275
276
    /**
277
     * Set the expiration for a key as a UNIX timestamp
278
     * @param string $key
279
     * @param int $timestamp
280
     * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set
281
     */
282 4
    public function expireat($key, $timestamp)
283
    {
284 4
        $this->init();
285 4
        $result = $this->driver->expireat($key, $timestamp);
286 4
        return (bool)$result;
287
    }
288
289
    /**
290
     * Set the expiration for a key as a UNIX timestamp specified in milliseconds
291
     * @param string $key
292
     * @param int $timestamp
293
     * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set
294
     */
295 4
    public function pexpireat($key, $timestamp)
296
    {
297 4
        $this->init();
298 4
        $result = $this->driver->pexpireat($key, $timestamp);
299 4
        return (bool)$result;
300
    }
301
302
    /**
303
     * Set the value and expiration in milliseconds of a key
304
     * @param string $key
305
     * @param int $miliseconds
306
     * @param string $value
307
     * @return boolean
308
     */
309 4
    public function psetex($key, $miliseconds, $value)
310
    {
311 4
        $this->init();
312 4
        $result = $this->driver->psetex($key, $miliseconds, $value);
313 4
        if ($result == '+OK') {
314 2
            return true;
315
        }
316 2
        return $this->transformResult($result);
317
    }
318
319
    /**
320
     * Set the value of a key, only if the key does not exist
321
     * @param string $key
322
     * @param string $value
323
     * @return boolean true if the key was set, false if the key was not set
324
     */
325 4
    public function setnx($key, $value)
326
    {
327 4
        $this->init();
328 4
        $result = $this->driver->setnx($key, $value);
329 4
        return (bool)$result;
330
    }
331
332
    /**
333
     * Delete a key(s)
334
     * @param array $keys
335
     * @return int number of deleted keys
336
     */
337 28
    public function del(...$keys)
338
    {
339 28
        $this->prepareArguments('del', ...$keys);
340 20
        $this->init();
341 20
        return $this->driver->del(...$keys);
342
    }
343
344
    /**
345
     * Delete a key(s)
346
     * @param array $keys
347
     * @return int number of deleted keys
348
     */
349 12
    public function delete(...$keys)
350
    {
351 12
        return $this->del(...$keys);
352
    }
353
354
    /**
355
     * Increment the integer value of a key by one
356
     * @param string $key
357
     * @return integer
358
     */
359 4
    public function incr($key)
360
    {
361 4
        $this->init();
362 4
        return $this->driver->incr($key);
363
    }
364
365
    /**
366
     * Increment the integer value of a key by the given amount
367
     * @param string $key
368
     * @param integer $increment
369
     * @return integer
370
     */
371 4
    public function incrby($key, $increment = 1)
372
    {
373 4
        $this->init();
374 4
        return $this->driver->incrby($key, (int)$increment);
375
    }
376
377
    /**
378
     * Increment the float value of a key by the given amount
379
     * @param string $key
380
     * @param float $increment
381
     * @return float
382
     */
383 8
    public function incrbyfloat($key, $increment = 1)
384
    {
385 8
        $this->init();
386 8
        return $this->driver->incrbyfloat($key, $increment);
387
    }
388
389
    /**
390
     * Decrement the integer value of a key by one
391
     * @param string $key
392
     * @return integer
393
     */
394 4
    public function decr($key)
395
    {
396 4
        $this->init();
397 4
        return $this->driver->decr($key);
398
    }
399
400
    /**
401
     * Decrement the integer value of a key by the given number
402
     * @param string $key
403
     * @param integer $decrement
404
     * @return integer
405
     */
406 4
    public function decrby($key, $decrement = 1)
407
    {
408 4
        $this->init();
409 4
        return $this->driver->decrby($key, (int)$decrement);
410
    }
411
412
    /**
413
     * Decrement the float value of a key by the given amount
414
     * @param string $key
415
     * @param float $decrement
416
     * @return float
417
     */
418 4
    public function decrbyfloat($key, $decrement = 1)
419
    {
420 4
        return $this->incrbyfloat($key, (-1) * $decrement);
421
    }
422
423
    /**
424
     * Set multiple values to multiple keys
425
     * @param array $dictionary
426
     * @return boolean true on success
427
     * @throws RedisProxyException if number of arguments is wrong
428
     */
429 12 View Code Duplication
    public function mset(...$dictionary)
430
    {
431 12
        $this->init();
432 12
        if (is_array($dictionary[0])) {
433 8
            $result = $this->driver->mset(...$dictionary);
434 8
            return $this->transformResult($result);
435
        }
436 8
        $dictionary = $this->prepareKeyValue($dictionary, 'mset');
437 4
        $result = $this->driver->mset($dictionary);
438 4
        return $this->transformResult($result);
439
    }
440
441
    /**
442
     * Multi get
443
     * @param array $keys
444
     * @return array Returns the values for all specified keys. For every key that does not hold a string value or does not exist, null is returned
445
     */
446 4
    public function mget(...$keys)
447
    {
448 4
        $keys = array_unique($this->prepareArguments('mget', ...$keys));
449 4
        $this->init();
450 4
        $values = [];
451 4
        foreach ($this->driver->mget($keys) as $value) {
452 4
            $values[] = $this->convertFalseToNull($value);
453 4
        }
454 4
        return array_combine($keys, $values);
455
    }
456
457
    /**
458
     * Incrementally iterate the keys space
459
     * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished
460
     * @param string $pattern pattern for keys, use * as wild card
461
     * @param int $count
462
     * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0'
463
     */
464 4 View Code Duplication
    public function scan(&$iterator, $pattern = null, $count = null)
465
    {
466 4
        if ((string)$iterator === '0') {
467 4
            return null;
468
        }
469 4
        $this->init();
470 4
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
471 2
            $returned = $this->driver->scan($iterator, ['match' => $pattern, 'count' => $count]);
472 2
            $iterator = $returned[0];
473 2
            return $returned[1];
474
        }
475 2
        return $this->driver->scan($iterator, $pattern, $count);
476
    }
477
478
    /**
479
     * Get the value of a hash field
480
     * @param string $key
481
     * @param string $field
482
     * @return string|null null if hash field is not set
483
     */
484 16
    public function hget($key, $field)
485
    {
486 16
        $this->init();
487 16
        $result = $this->driver->hget($key, $field);
488 16
        return $this->convertFalseToNull($result);
489
    }
490
491
    /**
492
     * Delete one or more hash fields, returns number of deleted fields
493
     * @param array $key
494
     * @param array $fields
495
     * @return int
496
     */
497 8
    public function hdel($key, ...$fields)
498
    {
499 8
        $fields = $this->prepareArguments('hdel', ...$fields);
500 8
        $this->init();
501 8
        return $this->driver->hdel($key, ...$fields);
502
    }
503
504
    /**
505
     * Increment the integer value of hash field by given number
506
     * @param string $key
507
     * @param string $field
508
     * @param int $increment
509
     * @return int
510
     */
511 4
    public function hincrby($key, $field, $increment = 1)
512
    {
513 4
        $this->init();
514 4
        return $this->driver->hincrby($key, $field, (int)$increment);
515
    }
516
517
    /**
518
     * Increment the float value of hash field by given amount
519
     * @param string $key
520
     * @param string $field
521
     * @param float $increment
522
     * @return float
523
     */
524 4
    public function hincrbyfloat($key, $field, $increment = 1)
525
    {
526 4
        $this->init();
527 4
        return $this->driver->hincrbyfloat($key, $field, $increment);
528
    }
529
530
    /**
531
     * Set multiple values to multiple hash fields
532
     * @param string $key
533
     * @param array $dictionary
534
     * @return boolean true on success
535
     * @throws RedisProxyException if number of arguments is wrong
536
     */
537 12 View Code Duplication
    public function hmset($key, ...$dictionary)
538
    {
539 12
        $this->init();
540 12
        if (is_array($dictionary[0])) {
541 8
            $result = $this->driver->hmset($key, ...$dictionary);
542 8
            return $this->transformResult($result);
543
        }
544 8
        $dictionary = $this->prepareKeyValue($dictionary, 'hmset');
545 4
        $result = $this->driver->hmset($key, $dictionary);
546 4
        return $this->transformResult($result);
547
    }
548
549
    /**
550
     * Multi hash get
551
     * @param string $key
552
     * @param array $fields
553
     * @return array Returns the values for all specified fields. For every field that does not hold a string value or does not exist, null is returned
554
     */
555 4
    public function hmget($key, ...$fields)
556
    {
557 4
        $fields = array_unique($this->prepareArguments('hmget', ...$fields));
558 4
        $this->init();
559 4
        $values = [];
560 4
        foreach ($this->driver->hmget($key, $fields) as $value) {
561 4
            $values[] = $this->convertFalseToNull($value);
562 4
        }
563 4
        return array_combine($fields, $values);
564
    }
565
566
    /**
567
     * Incrementally iterate hash fields and associated values
568
     * @param string $key
569
     * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished
570
     * @param string $pattern pattern for fields, use * as wild card
571
     * @param int $count
572
     * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0'
573
     */
574 4 View Code Duplication
    public function hscan($key, &$iterator, $pattern = null, $count = null)
575
    {
576 4
        if ((string)$iterator === '0') {
577 4
            return null;
578
        }
579 4
        $this->init();
580 4
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
581 2
            $returned = $this->driver->hscan($key, $iterator, ['match' => $pattern, 'count' => $count]);
582 2
            $iterator = $returned[0];
583 2
            return $returned[1];
584
        }
585 2
        return $this->driver->hscan($key, $iterator, $pattern, $count);
586
    }
587
588
    /**
589
     * Add one or more members to a set
590
     * @param string $key
591
     * @param array $members
592
     * @return int number of new members added to set
593
     */
594 16
    public function sadd($key, ...$members)
595
    {
596 16
        $members = $this->prepareArguments('sadd', ...$members);
597 16
        $this->init();
598 16
        return $this->driver->sadd($key, ...$members);
599
    }
600
601
    /**
602
     * Remove and return one or multiple random members from a set
603
     * @param string $key
604
     * @param int $count number of members
605
     * @return mixed string if $count is null or 1 and $key exists, array if $count > 1 and $key exists, null if $key doesn't exist
606
     */
607 4
    public function spop($key, $count = 1)
608
    {
609 4
        $this->init();
610 4
        if ($count == 1 || $count === null) {
611 4
            $result = $this->driver->spop($key);
612 4
            return $this->convertFalseToNull($result);
613
        }
614
615 4
        $members = [];
616 4
        for ($i = 0; $i < $count; ++$i) {
617 4
            $member = $this->driver->spop($key);
618 4
            if (!$member) {
619 4
                break;
620
            }
621 4
            $members[] = $member;
622 4
        }
623 4
        return empty($members) ? null : $members;
624
    }
625
626
    /**
627
     * Incrementally iterate Set elements
628
     * @param string $key
629
     * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished
630
     * @param string $pattern pattern for member's values, use * as wild card
631
     * @param int $count
632
     * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0'
633
     */
634 4 View Code Duplication
    public function sscan($key, &$iterator, $pattern = null, $count = null)
635
    {
636 4
        if ((string)$iterator === '0') {
637 4
            return null;
638
        }
639 4
        $this->init();
640 4
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
641 2
            $returned = $this->driver->sscan($key, $iterator, ['match' => $pattern, 'count' => $count]);
642 2
            $iterator = $returned[0];
643 2
            return $returned[1];
644
        }
645 2
        return $this->driver->sscan($key, $iterator, $pattern, $count);
646
    }
647
648
    /**
649
     * Prepend one or multiple values to a list
650
     * @param string $key
651
     * @param array $elements
652
     * @return int the length of the list after the push operations
653
     */
654 28
    public function lpush($key, ...$elements)
655
    {
656 28
        $elements = $this->prepareArguments('lpush', ...$elements);
657 28
        $this->init();
658 28
        return $this->driver->lpush($key, ...$elements);
659
    }
660
661
    /**
662
     * Append one or multiple values to a list
663
     * @param string $key
664
     * @param array $elements
665
     * @return int the length of the list after the push operations
666
     */
667 12
    public function rpush($key, ...$elements)
668
    {
669 12
        $elements = $this->prepareArguments('rpush', ...$elements);
670 12
        $this->init();
671 12
        return $this->driver->rpush($key, ...$elements);
672
    }
673
674
    /**
675
     * Remove and get the first element in a list
676
     * @param string $key
677
     * @return string|null
678
     */
679 4
    public function lpop($key)
680
    {
681 4
        $this->init();
682 4
        $result = $this->driver->lpop($key);
683 4
        return $this->convertFalseToNull($result);
684
    }
685
686
    /**
687
     * Remove and get the last element in a list
688
     * @param string $key
689
     * @return string|null
690
     */
691 4
    public function rpop($key)
692
    {
693 4
        $this->init();
694 4
        $result = $this->driver->rpop($key);
695 4
        return $this->convertFalseToNull($result);
696
    }
697
698
    /**
699
     * Get an element from a list by its index
700
     * @param string $key
701
     * @param int $index zero-based, so 0 means the first element, 1 the second element and so on. -1 means the last element, -2 means the penultimate and so forth
702
     * @return string|null
703
     */
704 12
    public function lindex($key, $index)
705
    {
706 12
        $this->init();
707 12
        $result = $this->driver->lindex($key, $index);
708 12
        return $this->convertFalseToNull($result);
709
    }
710
711
    /**
712
     * Add one or more members to a sorted set, or update its score if it already exists
713
     * @param string $key
714
     * @param array $dictionary (score1, member1[, score2, member2]) or associative array: [member1 => score1, member2 => score2]
715
     * @return int
716
     */
717 20
    public function zadd($key, ...$dictionary)
718
    {
719 20
        $this->init();
720 20
        if (is_array($dictionary[0])) {
721 12
            $return = 0;
722 12
            foreach ($dictionary[0] as $member => $score) {
723 12
                $res = $this->zadd($key, $score, $member);
724 12
                $return += $res;
725 12
            }
726 12
            return $return;
727
        }
728 20
        return $this->driver->zadd($key, ...$dictionary);
729
    }
730
731
    /**
732
     * Return a range of members in a sorted set, by index
733
     * @param string $key
734
     * @param int $start
735
     * @param int $stop
736
     * @param boolean $withscores
737
     * @return array
738
     */
739 4 View Code Duplication
    public function zrange($key, $start, $stop, $withscores = false)
1 ignored issue
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...
740
    {
741 4
        $this->init();
742 4
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
743 2
            return $this->driver->zrange($key, $start, $stop, ['WITHSCORES' => $withscores]);
744
        }
745 2
        return $this->driver->zrange($key, $start, $stop, $withscores);
746
    }
747
748
    /**
749
     * Return a range of members in a sorted set, by index, with scores ordered from high to low
750
     * @param string $key
751
     * @param int $start
752
     * @param int $stop
753
     * @param boolean $withscores
754
     * @return array
755
     */
756 4 View Code Duplication
    public function zrevrange($key, $start, $stop, $withscores = false)
1 ignored issue
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...
757
    {
758 4
        $this->init();
759 4
        if ($this->actualDriver() === self::DRIVER_PREDIS) {
760 2
            return $this->driver->zrevrange($key, $start, $stop, ['WITHSCORES' => $withscores]);
761
        }
762 2
        return $this->driver->zrevrange($key, $start, $stop, $withscores);
763
    }
764
765
    /**
766
     * Returns null instead of false for Redis driver
767
     * @param mixed $result
768
     * @return mixed
769
     */
770 112
    private function convertFalseToNull($result)
771
    {
772 112
        return $this->actualDriver() === self::DRIVER_REDIS && $result === false ? null : $result;
773
    }
774
775
    /**
776
     * Transforms Predis result Payload to boolean
777
     * @param mixed $result
778
     * @return mixed
779
     */
780 240
    private function transformResult($result)
781
    {
782 240
        if ($this->actualDriver() === self::DRIVER_PREDIS && $result instanceof Status) {
783 120
            $result = $result->getPayload() === 'OK';
784 120
        }
785 240
        return $result;
786
    }
787
788
    /**
789
     * Create array from input array - odd keys are used as keys, even keys are used as values
790
     * @param array $dictionary
791
     * @param string $command
792
     * @return array
793
     * @throws RedisProxyException if number of keys is not the same as number of values
794
     */
795 16
    private function prepareKeyValue(array $dictionary, $command)
796
    {
797
        $keys = array_values(array_filter($dictionary, function ($key) {
798 16
            return $key % 2 == 0;
799 16
        }, ARRAY_FILTER_USE_KEY));
800 16
        $values = array_values(array_filter($dictionary, function ($key) {
801 16
            return $key % 2 == 1;
802 16
        }, ARRAY_FILTER_USE_KEY));
803
804 16
        if (count($keys) != count($values)) {
805 8
            throw new RedisProxyException("Wrong number of arguments for $command command");
806
        }
807 8
        return array_combine($keys, $values);
808
    }
809
810 84
    private function prepareArguments($command, ...$params)
811
    {
812 84
        if (!isset($params[0])) {
813 8
            throw new RedisProxyException("Wrong number of arguments for $command command");
814
        }
815 76
        if (is_array($params[0])) {
816 32
            $params = $params[0];
817 32
        }
818 76
        return $params;
819
    }
820
}
821