Completed
Pull Request — master (#123)
by Roberto
13:32
created

Streams::_checkInfoCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
trait Streams
6
{
7
    /**
8
     * Acknowledge one or more pending messages.
9
     *
10
     * @param  string $stream
11
     * @param  string $group
12
     * @param  array  $messages
13
     *
14
     * @return int              The number of messages Redis reports as acknowledged.
15
     */
16
    public function xAck(string $stream, string $group, array $messages): int
17
    {
18
        return $this->redis->xAck($stream, $group, $messages);
0 ignored issues
show
Bug introduced by
The property redis does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
    }
20
21
22
    /**
23
     * Appends the specified stream entry to the stream at the specified key.
24
     * If the key does not exist, as a side effect of running this command the
25
     * key is created with a stream value.
26
     * See: https://redis.io/commands/xadd.
27
     *
28
     * @param  string      $key
29
     * @param  string      $id
30
     * @param  array       $message
31
     * @param  int|integer $maxLenght
32
     * @param  bool        $approximate
33
     *
34
     * @return string                   The added message ID
35
     */
36
    public function xAdd(
37
        string $key,
38
        string $id,
39
        array $message,
40
        ?int $maxLenght = null,
41
        ?bool $approximate = null
42
    ): string {
43
        if (is_null($maxLenght) && is_null($approximate)) {
44
            return $this->redis->xAdd($key, $id, $message);
45
        }
46
47
        return is_null($approximate) ?
48
            $this->redis->xAdd($key, $id, $message, $maxLenght) :
49
            $this->redis->xAdd($key, $id, $message, $maxLenght, $approximate);
50
    }
51
52
53
    /**
54
     * Claim ownership of one or more pending messages.
55
     *
56
     * @param  string     $stream
57
     * @param  string     $group
58
     * @param  string     $consumer
59
     * @param  int        $minIdleTime
60
     * @param  array      $messageIds
61
     * @param  array|null $options
62
     *
63
     * @return array                    Either an array of message IDs along with
64
     *                                  corresponding data, or just an array of
65
     *                                  IDs (if the 'JUSTID' option was passed).
66
     */
67
    public function xClaim(
68
        string $stream,
69
        string $group,
70
        string $consumer,
71
        int $minIdleTime,
72
        array $messageIds,
73
        ?array $options = null
74
    ): array {
75
        if (!is_null($options) && !$this->_checkClaimOptions($options)) {
76
            throw new \Exception("Bad Claim Options", 1);
77
        }
78
79
        return is_null($options) ?
80
            $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds):
81
            $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds, $options);
82
    }
83
84
85
    /**
86
     * Delete one or more messages from a stream.
87
     *
88
     * @param  string $stream
89
     * @param  array  $messageIds
90
     *
91
     * @return int                  The number of messages removed.
92
     */
93
    public function xDel(string $stream, array $messageIds): int
94
    {
95
        return $this->redis->xDel($stream, $messageIds);
96
    }
97
98
99
    /**
100
     * This command is used in order to create, destroy, or manage consumer groups.
101
     *
102
     * @param  string       $command                [description]
103
     * @param  string|null  $stream                 [description]
104
     * @param  string|null  $group                  [description]
105
     * @param  [type]       $messageId_consumerName [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
106
     * @param  bool|boolean $makeStream             [description]
107
     *
108
     * @return mixed                                This command returns different
109
     *                                              types depending on the specific
110
     *                                              XGROUP command executed.
111
     */
112
    public function xGroup(
113
        string $command,
114
        ?string $stream = null,
115
        ?string $group = null,
116
        $messageId_consumerName = null,
117
        bool $makeStream = false
118
    ) {
119
        return $this->redis->xGroup($command, $stream, $group, $messageId_consumerName, $makeStream);
120
    }
121
122
123
    /**
124
     * Get information about a stream or consumer groups.
125
     *
126
     * @param  string $command
127
     * @param  string $stream
128
     * @param  string $group
129
     *
130
     * @return mixed            This command returns different types depending on which subcommand is used.
131
     */
132
    public function xInfo(string $command, ?string $stream = null, ?string $group = null)
133
    {
134
        $command = strtoupper($command);
135
        
136
        if (!$this->_checkInfoCommands($command)) {
137
            throw new \Exception("Bad Info Command", 1);
138
        }
139
140
        if (is_null($stream) && is_null($group)) {
141
            return $this->redis->xInfo($command);
142
        }
143
144
        return is_null($group) ?
145
            $this->redis->xInfo($command, $stream) :
146
            $this->redis->xInfo($command, $stream, $group);
147
    }
148
149
    public function xLen(): bool
150
    {
151
        return false;
152
    }
153
154
    public function xPending(): bool
155
    {
156
        return false;
157
    }
158
159
    public function xRange(): bool
160
    {
161
        return false;
162
    }
163
164
    public function xRead(): bool
165
    {
166
        return false;
167
    }
168
169
    public function xReadGroup(): array
170
    {
171
        return [];
172
    }
173
174
    public function xRevRange(): bool
175
    {
176
        return false;
177
    }
178
179
    public function xTrim(): bool
180
    {
181
        return false;
182
    }
183
184
185
    /**
186
     * ************************************************************************
187
     * H E L P E R    F U N C T I O N S
188
     * ************************************************************************
189
     */
190
191
192
    /**
193
     * Claim Options available
194
     *
195
     * Note:  'TIME', and 'IDLE' are mutually exclusive
196
     *
197
     * 'IDLE' => $value, Set the idle time to $value ms
198
     * 'TIME' => $value, Set the idle time to now - $value
199
     * 'RETRYCOUNT' => $value, Update message retrycount to $value
200
     * 'FORCE', Claim the message(s) even if they're not pending anywhere
201
     * 'JUSTID',Instruct Redis to only return IDs
202
     *
203
     * @param  array  $options
204
     *
205
     * @return bool
206
     */
207
    private function _checkClaimOptions(array $options): bool
208
    {
209
        $available = ['IDLE', 'TIME', 'RETRYCOUNT', 'FORCE', 'JUSTID'];
210
        
211
        foreach ($options as $key => $value) {
212
            $check = is_numeric($key) ? $value : $key;
213
            if (!in_array($check, $available)) {
214
                return false;
215
            }
216
        }
217
218
        return !(array_key_exists('IDLE', $options) && array_key_exists('TIME', $options));
219
    }
220
221
222
    /**
223
     * [_checkInfoCommands description]
224
     *
225
     * @param  string $command
226
     *
227
     * @return bool
228
     */
229
    private function _checkInfoCommands(string $command) : bool
230
    {
231
        $available = ['CONSUMERS', 'GROUPS', 'STREAM', 'HELP'];
232
233
        return in_array($command, $available, true);
234
    }
235
}
236