Completed
Pull Request — master (#123)
by Roberto
18:46 queued 03:49
created

Streams::xPending()   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 0
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
150
    /**
151
     * Get the length of a given stream.
152
     *
153
     * @param  string $stream
154
     *
155
     * @return The number of messages in the stream.
156
     */
157
    public function xLen(string $stream): int
158
    {
159
        return $this->redis->xLen($stream);
160
    }
161
162
    public function xPending(): bool
163
    {
164
        return false;
165
    }
166
167
    public function xRange(): bool
168
    {
169
        return false;
170
    }
171
172
    public function xRead(): bool
173
    {
174
        return false;
175
    }
176
177
    public function xReadGroup(): array
178
    {
179
        return [];
180
    }
181
182
    public function xRevRange(): bool
183
    {
184
        return false;
185
    }
186
187
    public function xTrim(): bool
188
    {
189
        return false;
190
    }
191
192
193
    /**
194
     * ************************************************************************
195
     * H E L P E R    F U N C T I O N S
196
     * ************************************************************************
197
     */
198
199
200
    /**
201
     * Claim Options available
202
     *
203
     * Note:  'TIME', and 'IDLE' are mutually exclusive
204
     *
205
     * 'IDLE' => $value, Set the idle time to $value ms
206
     * 'TIME' => $value, Set the idle time to now - $value
207
     * 'RETRYCOUNT' => $value, Update message retrycount to $value
208
     * 'FORCE', Claim the message(s) even if they're not pending anywhere
209
     * 'JUSTID',Instruct Redis to only return IDs
210
     *
211
     * @param  array  $options
212
     *
213
     * @return bool
214
     */
215
    private function _checkClaimOptions(array $options): bool
216
    {
217
        $available = ['IDLE', 'TIME', 'RETRYCOUNT', 'FORCE', 'JUSTID'];
218
219
        foreach ($options as $key => $value) {
220
            $check = is_numeric($key) ? $value : $key;
221
            if (!in_array($check, $available)) {
222
                return false;
223
            }
224
        }
225
226
        return !(array_key_exists('IDLE', $options) && array_key_exists('TIME', $options));
227
    }
228
229
230
    /**
231
     * [_checkInfoCommands description]
232
     *
233
     * @param  string $command
234
     *
235
     * @return bool
236
     */
237
    private function _checkInfoCommands(string $command): bool
238
    {
239
        $available = ['CONSUMERS', 'GROUPS', 'STREAM', 'HELP'];
240
241
        return in_array($command, $available, true);
242
    }
243
}
244