Completed
Pull Request — master (#123)
by Roberto
29:50 queued 15:01
created

Streams::xAck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
trait Streams
6
{
7
    /**
8
     * Acknowledge one or more pending messages.
9
     *
10
     * See: https://redis.io/commands/xack.
11
     *
12
     * @param  string $stream
13
     * @param  string $group
14
     * @param  array  $messages
15
     *
16
     * @return int              The number of messages Redis reports as acknowledged.
17
     */
18
    public function xAck(string $stream, string $group, array $messages): int
19
    {
20
        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...
21
    }
22
23
24
    /**
25
     * Appends the specified stream entry to the stream at the specified key.
26
     * If the key does not exist, as a side effect of running this command the
27
     * key is created with a stream value.
28
     *
29
     * See: https://redis.io/commands/xadd.
30
     *
31
     * @param  string      $key
32
     * @param  string      $id
33
     * @param  array       $message
34
     * @param  int|integer $maxLenght
35
     * @param  bool        $approximate
36
     *
37
     * @return string                   The added message ID
38
     */
39
    public function xAdd(
40
        string $key,
41
        string $id,
42
        array $message,
43
        ?int $maxLenght = null,
44
        ?bool $approximate = null
45
    ): string {
46
        if (is_null($maxLenght) && is_null($approximate)) {
47
            return $this->redis->xAdd($key, $id, $message);
48
        }
49
50
        return is_null($approximate) ?
51
            $this->redis->xAdd($key, $id, $message, $maxLenght) :
52
            $this->redis->xAdd($key, $id, $message, $maxLenght, $approximate);
53
    }
54
55
56
    /**
57
     * Claim ownership of one or more pending messages.
58
     *
59
     * See: https://redis.io/commands/xclaim.
60
     *
61
     * @param  string     $stream
62
     * @param  string     $group
63
     * @param  string     $consumer
64
     * @param  int        $minIdleTime
65
     * @param  array      $messageIds
66
     * @param  array|null $options
67
     *
68
     * @return array                    Either an array of message IDs along with
69
     *                                  corresponding data, or just an array of
70
     *                                  IDs (if the 'JUSTID' option was passed).
71
     */
72
    public function xClaim(
73
        string $stream,
74
        string $group,
75
        string $consumer,
76
        int $minIdleTime,
77
        array $messageIds,
78
        ?array $options = null
79
    ): array {
80
        if (!is_null($options) && !$this->_checkClaimOptions($options)) {
81
            throw new \Exception("Bad Claim Options", 1);
82
        }
83
84
        return is_null($options) ?
85
            $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds) :
86
            $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds, $options);
87
    }
88
89
90
    /**
91
     * Delete one or more messages from a stream.
92
     *
93
     * See: https://redis.io/commands/xdel.
94
     *
95
     * @param  string $stream
96
     * @param  array  $messageIds
97
     *
98
     * @return int                  The number of messages removed.
99
     */
100
    public function xDel(string $stream, array $messageIds): int
101
    {
102
        return $this->redis->xDel($stream, $messageIds);
103
    }
104
105
106
    /**
107
     * This command is used in order to create, destroy, or manage consumer groups.
108
     *
109
     * See: https://redis.io/commands/xgroup.
110
     *
111
     * @param  string       $command                [description]
112
     * @param  string|null  $stream                 [description]
113
     * @param  string|null  $group                  [description]
114
     * @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...
115
     * @param  bool|boolean $makeStream             [description]
116
     *
117
     * @return mixed                                This command returns different
118
     *                                              types depending on the specific
119
     *                                              XGROUP command executed.
120
     */
121
    public function xGroup(
122
        string $command,
123
        ?string $stream = null,
124
        ?string $group = null,
125
        $messageId_consumerName = null,
126
        bool $makeStream = false
127
    ) {
128
        return $this->redis->xGroup($command, $stream, $group, $messageId_consumerName, $makeStream);
129
    }
130
131
132
    /**
133
     * Get information about a stream or consumer groups.
134
     *
135
     * See: https://redis.io/commands/xinfo.
136
     *
137
     * @param  string $command
138
     * @param  string $stream
139
     * @param  string $group
140
     *
141
     * @return mixed            This command returns different types depending on which subcommand is used.
142
     */
143
    public function xInfo(string $command, ?string $stream = null, ?string $group = null)
144
    {
145
        $command = strtoupper($command);
146
147
        if (!$this->_checkInfoCommands($command)) {
148
            throw new \Exception("Bad Info Command", 1);
149
        }
150
151
        if (is_null($stream) && is_null($group)) {
152
            return $this->redis->xInfo($command);
153
        }
154
155
        return is_null($group) ?
156
            $this->redis->xInfo($command, $stream) :
157
            $this->redis->xInfo($command, $stream, $group);
158
    }
159
160
161
    /**
162
     * Get the length of a given stream.
163
     *
164
     * See: https://redis.io/commands/xlen.
165
     *
166
     * @param  string $stream
167
     *
168
     * @return The number of messages in the stream.
169
     */
170
    public function xLen(string $stream): int
171
    {
172
        return $this->redis->xLen($stream);
173
    }
174
175
176
    /**
177
     * Get information about pending messages in a given stream.
178
     *
179
     * See: https://redis.io/commands/xpending.
180
     *
181
     * @param  string      $stream
182
     * @param  string      $group
183
     * @param  string|null $start
184
     * @param  string|null $end
185
     * @param  int|null    $count
186
     * @param  string|null $consumer
187
     *
188
     * @return array                    Information about the pending messages,
189
     *                                  in various forms depending on the specific
190
     *                                  invocation of XPENDING.
191
     */
192
    public function xPending(
193
        string $stream,
194
        string $group,
195
        ?string $start = null,
196
        ?string $end = null,
197
        ?int $count = null,
198
        ?string $consumer = null
199
    ): array {
200
        return is_null($start) || is_null($end) || is_null($count) || is_null($consumer) ?
201
            $this->redis->xPending($stream, $group) :
202
            $this->redis->xPending($stream, $group, $start, $end, $count, $consumer);
203
    }
204
205
206
    /**
207
     * Get a range of messages from a given stream.
208
     *
209
     * @param  string   $stream
210
     * @param  string   $start
211
     * @param  string   $end
212
     * @param  int|null $count
213
     *
214
     * @return array            The messages in the stream within the requested range.
215
     */
216
    public function xRange(string $stream, string $start, string $end, ?int $count = null): array
217
    {
218
        return is_null($count) ?
219
            $this->redis->xRange($stream, $start, $end) :
220
            $this->redis->xRange($stream, $start, $end, $count);
221
    }
222
223
    public function xRead(): bool
224
    {
225
        return false;
226
    }
227
228
    public function xReadGroup(): array
229
    {
230
        return [];
231
    }
232
233
    public function xRevRange(): bool
234
    {
235
        return false;
236
    }
237
238
    public function xTrim(): bool
239
    {
240
        return false;
241
    }
242
243
244
    /**
245
     * ************************************************************************
246
     * H E L P E R    F U N C T I O N S
247
     * ************************************************************************
248
     */
249
250
251
    /**
252
     * Claim Options available
253
     *
254
     * Note:  'TIME', and 'IDLE' are mutually exclusive
255
     *
256
     * 'IDLE' => $value, Set the idle time to $value ms
257
     * 'TIME' => $value, Set the idle time to now - $value
258
     * 'RETRYCOUNT' => $value, Update message retrycount to $value
259
     * 'FORCE', Claim the message(s) even if they're not pending anywhere
260
     * 'JUSTID',Instruct Redis to only return IDs
261
     *
262
     * @param  array  $options
263
     *
264
     * @return bool
265
     */
266
    private function _checkClaimOptions(array $options): bool
267
    {
268
        $available = ['IDLE', 'TIME', 'RETRYCOUNT', 'FORCE', 'JUSTID'];
269
270
        foreach ($options as $key => $value) {
271
            $check = is_numeric($key) ? $value : $key;
272
            if (!in_array($check, $available)) {
273
                return false;
274
            }
275
        }
276
277
        return !(array_key_exists('IDLE', $options) && array_key_exists('TIME', $options));
278
    }
279
280
281
    /**
282
     * [_checkInfoCommands description]
283
     *
284
     * @param  string $command
285
     *
286
     * @return bool
287
     */
288
    private function _checkInfoCommands(string $command): bool
289
    {
290
        $available = ['CONSUMERS', 'GROUPS', 'STREAM', 'HELP'];
291
292
        return in_array($command, $available, true);
293
    }
294
}
295