Completed
Pull Request — master (#123)
by Roberto
30:03 queued 14:58
created

Streams::xRevRange()   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 $this->redis->xAdd($key, $id, $message, $maxLenght, $approximate);
48
    }
49
50
51
    /**
52
     * Claim ownership of one or more pending messages.
53
     *
54
     * @param  string     $stream
55
     * @param  string     $group
56
     * @param  string     $consumer
57
     * @param  int        $minIdleTime
58
     * @param  array      $messageIds
59
     * @param  array|null $options
60
     *
61
     * @return array                    Either an array of message IDs along with
62
     *                                  corresponding data, or just an array of
63
     *                                  IDs (if the 'JUSTID' option was passed).
64
     */
65
    public function xClaim(
66
        string $stream,
67
        string $group,
68
        string $consumer,
69
        int $minIdleTime,
70
        array $messageIds,
71
        ?array $options = null
72
    ): array {
73
        if (!is_null($options) && !$this->_checkClaimOptions($options)) {
74
            throw new \Exception("Bad Claim Options", 1);
75
        }
76
        if (is_null($options)) {
77
            return $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds);
78
        } else {
79
            return $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds, $options);
80
        }
81
    }
82
83
    public function xDel(): bool
84
    {
85
        return false;
86
    }
87
88
    /**
89
     * This command is used in order to create, destroy, or manage consumer groups.
90
     *
91
     * @param  string       $command                [description]
92
     * @param  string|null  $stream                 [description]
93
     * @param  string|null  $group                  [description]
94
     * @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...
95
     * @param  bool|boolean $makeStream             [description]
96
     *
97
     * @return mixed                                This command returns different
98
     *                                              types depending on the specific
99
     *                                              XGROUP command executed.
100
     */
101
    public function xGroup(string $command, ?string $stream = null, ?string $group = null, $messageId_consumerName = null, bool $makeStream = false)
102
    {
103
        return $this->redis->xGroup($command, $stream, $group, $messageId_consumerName, $makeStream);
104
    }
105
106
    public function xInfo(): bool
107
    {
108
        return false;
109
    }
110
111
    public function xLen(): bool
112
    {
113
        return false;
114
    }
115
116
    public function xPending(): bool
117
    {
118
        return false;
119
    }
120
121
    public function xRange(): bool
122
    {
123
        return false;
124
    }
125
126
    public function xRead(): bool
127
    {
128
        return false;
129
    }
130
131
    public function xReadGroup(): bool
132
    {
133
        return false;
134
    }
135
136
    public function xRevRange(): bool
137
    {
138
        return false;
139
    }
140
141
    public function xTrim(): bool
142
    {
143
        return false;
144
    }
145
146
    /**
147
     * ************************************************************************
148
     * H E L P E R    F U N C T I O N S
149
     * ************************************************************************
150
     */
151
152
    /**
153
     * Claim Options available
154
     *
155
     * Note:  'TIME', and 'IDLE' are mutually exclusive
156
     *
157
     * 'IDLE' => $value, Set the idle time to $value ms
158
     * 'TIME' => $value, Set the idle time to now - $value
159
     * 'RETRYCOUNT' => $value, Update message retrycount to $value
160
     * 'FORCE', Claim the message(s) even if they're not pending anywhere
161
     * 'JUSTID',Instruct Redis to only return IDs
162
     *
163
     * @param  array  $options
164
     *
165
     * @return bool
166
     */
167
    private function _checkClaimOptions(array $options): bool
168
    {
169
        $available = ['IDLE', 'TIME', 'RETRYCOUNT', 'FORCE', 'JUSTID'];
170
        foreach ($options as $key => $value) {
171
            $check = is_numeric($key) ? $value : $key;
172
            if (!in_array($check, $available)) {
173
                return false;
174
            }
175
        }
176
        if (in_array('IDLE', $options) && in_array('TIME', $options)) {
177
            return false;
178
        }
179
        return true;
180
    }
181
}
182