Completed
Pull Request — master (#123)
by Roberto
26:15 queued 11:15
created

Streams   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 192
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A xAck() 0 4 1
A xAdd() 0 13 3
A xClaim() 0 17 4
A xDel() 0 4 1
A xGroup() 0 9 1
A xInfo() 0 4 1
A xLen() 0 4 1
A xPending() 0 4 1
A xRange() 0 4 1
A xRead() 0 4 1
A xReadGroup() 0 4 1
A xRevRange() 0 4 1
A xTrim() 0 4 1
A _checkClaimOptions() 0 14 6
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
84
    /**
85
     * Delete one or more messages from a stream.
86
     *
87
     * @param  string $stream
88
     * @param  array  $messageIds
89
     *
90
     * @return int                  The number of messages removed.
91
     */
92
    public function xDel(string $stream, array $messageIds): int
93
    {
94
        return $this->redis->xDel($stream, $messageIds);
95
    }
96
97
98
    /**
99
     * This command is used in order to create, destroy, or manage consumer groups.
100
     *
101
     * @param  string       $command                [description]
102
     * @param  string|null  $stream                 [description]
103
     * @param  string|null  $group                  [description]
104
     * @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...
105
     * @param  bool|boolean $makeStream             [description]
106
     *
107
     * @return mixed                                This command returns different
108
     *                                              types depending on the specific
109
     *                                              XGROUP command executed.
110
     */
111
    public function xGroup(
112
        string $command,
113
        ?string $stream = null,
114
        ?string $group = null,
115
        $messageId_consumerName = null,
116
        bool $makeStream = false
117
    ) {
118
        return $this->redis->xGroup($command, $stream, $group, $messageId_consumerName, $makeStream);
119
    }
120
121
    public function xInfo(): bool
122
    {
123
        return false;
124
    }
125
126
    public function xLen(): bool
127
    {
128
        return false;
129
    }
130
131
    public function xPending(): bool
132
    {
133
        return false;
134
    }
135
136
    public function xRange(): bool
137
    {
138
        return false;
139
    }
140
141
    public function xRead(): bool
142
    {
143
        return false;
144
    }
145
146
    public function xReadGroup(): bool
147
    {
148
        return false;
149
    }
150
151
    public function xRevRange(): bool
152
    {
153
        return false;
154
    }
155
156
    public function xTrim(): bool
157
    {
158
        return false;
159
    }
160
161
    /**
162
     * ************************************************************************
163
     * H E L P E R    F U N C T I O N S
164
     * ************************************************************************
165
     */
166
167
    /**
168
     * Claim Options available
169
     *
170
     * Note:  'TIME', and 'IDLE' are mutually exclusive
171
     *
172
     * 'IDLE' => $value, Set the idle time to $value ms
173
     * 'TIME' => $value, Set the idle time to now - $value
174
     * 'RETRYCOUNT' => $value, Update message retrycount to $value
175
     * 'FORCE', Claim the message(s) even if they're not pending anywhere
176
     * 'JUSTID',Instruct Redis to only return IDs
177
     *
178
     * @param  array  $options
179
     *
180
     * @return bool
181
     */
182
    private function _checkClaimOptions(array $options): bool
183
    {
184
        $available = ['IDLE', 'TIME', 'RETRYCOUNT', 'FORCE', 'JUSTID'];
185
        foreach ($options as $key => $value) {
186
            $check = is_numeric($key) ? $value : $key;
187
            if (!in_array($check, $available)) {
188
                return false;
189
            }
190
        }
191
        if (in_array('IDLE', $options) && in_array('TIME', $options)) {
192
            return false;
193
        }
194
        return true;
195
    }
196
}
197