RedisStore::zRemRangeByScore()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 2
b 0
f 1
nc 2
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Ackintosh\Ganesha\Storage\Adapter;
4
5
use Ackintosh\Ganesha\Exception\StorageException;
6
use Exception;
7
8
class RedisStore
9
{
10
    /**
11
     * @var \Predis\Client|\Redis|\RedisArray|\RedisCluster
12
     */
13
    private $redis;
14
15
    /**
16
     * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
17
     *
18
     * @throws \InvalidArgumentException if redis client is not supported
19
     */
20
    public function __construct($redisClient)
21
    {
22
        if (
23
            !$redisClient instanceof \Redis
24
            && !$redisClient instanceof \RedisArray
25
            && !$redisClient instanceof \RedisCluster
26
            && !$redisClient instanceof \Predis\Client
0 ignored issues
show
introduced by
$redisClient is always a sub-type of Predis\Client.
Loading history...
27
        ) {
28
            throw new \InvalidArgumentException(sprintf(
29
                '%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given',
30
                __METHOD__,
31
                \is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient)
32
            ));
33
        }
34
35
        $this->redis = $redisClient;
36
    }
37
38
    /**
39
     * Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end].
40
     *
41
     * @param   string       $key
42
     * @param   float|string $start double or "+inf" or "-inf" string
43
     * @param   float|string $end   double or "+inf" or "-inf" string
44
     *
45
     * @return  int|false             The number of values deleted from the sorted set
46
     *
47
     * @throws StorageException
48
     */
49
    public function zRemRangeByScore(string $key, $start, $end)
50
    {
51
        try {
52
            return $this->redis->zRemRangeByScore($key, $start, $end);
0 ignored issues
show
Bug introduced by
The method zRemRangeByScore() does not exist on RedisArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            return $this->redis->/** @scrutinizer ignore-call */ zRemRangeByScore($key, $start, $end);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        } catch (Exception $exception) {
54
            throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
55
        }
56
    }
57
58
    /**
59
     * Returns the cardinality of an ordered set.
60
     *
61
     * @param   string $key
62
     *
63
     * @return  int     the set's cardinality
64
     *
65
     * @throws StorageException
66
     */
67
    public function zCard(string $key): int
68
    {
69
        try {
70
            $r = $this->redis->zCard($key);
0 ignored issues
show
Bug introduced by
The method zCard() does not exist on RedisArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            /** @scrutinizer ignore-call */ 
71
            $r = $this->redis->zCard($key);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
        } catch (Exception $exception) {
72
            throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
73
        }
74
75
        if ($r === false) {
76
            throw new StorageException(sprintf(
77
                "Failed to execute zCard command. key: %s",
78
                $key
79
            ));
80
        }
81
82
        return $r;
83
    }
84
85
    /**
86
     * Adds the specified member with a given score to the sorted set stored at key.
87
     *
88
     * @param   string $key    Required key
89
     * @param   float  $score1 Required score
90
     * @param   string $value1 Required value
91
     *
92
     * @return  int     Number of values added
93
     *
94
     * @throws StorageException
95
     */
96
    public function zAdd(string $key, float $score1, string $value1): int
97
    {
98
        try {
99
            $r = $this->redis->zAdd($key, $score1, $value1);
0 ignored issues
show
Bug introduced by
The method zAdd() does not exist on RedisArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
            /** @scrutinizer ignore-call */ 
100
            $r = $this->redis->zAdd($key, $score1, $value1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
$score1 of type double is incompatible with the type array expected by parameter $options of Redis::zAdd(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
            $r = $this->redis->zAdd($key, /** @scrutinizer ignore-type */ $score1, $value1);
Loading history...
Bug introduced by
$value1 of type string is incompatible with the type double expected by parameter $score1 of Redis::zAdd(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
            $r = $this->redis->zAdd($key, $score1, /** @scrutinizer ignore-type */ $value1);
Loading history...
Bug introduced by
The call to Redis::zAdd() has too few arguments starting with value1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
            /** @scrutinizer ignore-call */ 
100
            $r = $this->redis->zAdd($key, $score1, $value1);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
        } catch (Exception $exception) {
101
            throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
102
        }
103
104
        if ($r === false) {
105
            throw new StorageException(sprintf(
106
                "Failed to execute zAdd command. key: %s, score1: %s, value1: %s",
107
                $key,
108
                $score1,
109
                $value1
110
            ));
111
        }
112
113
        return $r;
114
    }
115
116
    /**
117
     * Returns a range of elements from the ordered set stored at the specified key,
118
     * with values in the range [start, end]. start and stop are interpreted as zero-based indices:
119
     * 0 the first element,
120
     * 1 the second ...
121
     * -1 the last element,
122
     * -2 the penultimate ...
123
     *
124
     * @param   string $key
125
     * @param   int    $start
126
     * @param   int    $end
127
     *
128
     * @return  array   Array containing the values in specified range.
129
     *
130
     * @throws StorageException
131
     */
132
    public function zRange(string $key, int $start, int $end): array
133
    {
134
        try {
135
            $elements = $this->redis->zRange($key, $start, $end);
0 ignored issues
show
Bug introduced by
The method zRange() does not exist on RedisArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
            /** @scrutinizer ignore-call */ 
136
            $elements = $this->redis->zRange($key, $start, $end);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
            return !$elements ? [] : $elements;
137
        } catch (Exception $exception) {
138
            throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
139
        }
140
    }
141
142
    /**
143
     * Set the string value in argument as value of the key.
144
     *
145
     * @param   string $key
146
     * @param   string $value
147
     *
148
     * @return  bool    TRUE if the command is successful.
149
     *
150
     * @throws StorageException
151
     */
152
    public function set(string $key, string $value): bool
153
    {
154
        try {
155
            $r = $this->redis->set($key, $value);
0 ignored issues
show
Bug introduced by
The method set() does not exist on RedisArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
            /** @scrutinizer ignore-call */ 
156
            $r = $this->redis->set($key, $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
            if (is_bool($r)) {
157
                return $r;
158
            } elseif ($r instanceof \Predis\Response\Status) {
159
                return $r->getPayload() === 'OK';
160
            } else {
161
                throw new \LogicException("Could not handle the response: " . serialize($r));
162
            }
163
        } catch (Exception $exception) {
164
            throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
165
        }
166
    }
167
168
    /**
169
     * Get the value related to the specified key
170
     *
171
     * @param   string $key
172
     *
173
     * @return  string|false  If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned.
174
     *
175
     * @throws StorageException
176
     */
177
    public function get(string $key)
178
    {
179
        try {
180
            $result = $this->redis->get($key);
0 ignored issues
show
Bug introduced by
The method get() does not exist on RedisArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

180
            /** @scrutinizer ignore-call */ 
181
            $result = $this->redis->get($key);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
            if ($this->redis instanceof \Predis\Client && $result === null) {
182
                return false;
183
            }
184
            return $result;
185
        } catch (Exception $exception) {
186
            throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
187
        }
188
    }
189
}
190