Passed
Push — master ( a635c7...cccea0 )
by Sergey
25:40 queued 07:31
created

Stream::listen()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 3
eloc 17
nc 1
nop 1
dl 0
loc 29
ccs 0
cts 18
cp 0
crap 12
rs 9.7
c 3
b 1
f 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Stream class
7
 *
8
 * @author Sergei Karii <[email protected]>
9
 */
10
11
namespace Asiries335\redisSteamPhp;
12
13
use Asiries335\redisSteamPhp\Data\Collection;
14
use Asiries335\redisSteamPhp\Data\Constants;
15
use Asiries335\redisSteamPhp\Data\Message;
16
use Asiries335\redisSteamPhp\Hydrator\CollectionHydrator;
17
use Asiries335\redisSteamPhp\Hydrator\MessageHydrator;
18
use Redis;
19
20
final class Stream
21
{
22
    /**
23
     * Client
24
     *
25
     * @var Redis
26
     */
27
    private $_client;
28
29
    /**
30
     * Name stream
31
     *
32
     * @var string
33
     */
34
    private $_streamName;
35
36
    /**
37
     * Stream constructor.
38
     *
39
     * @param Redis  $client     Redis Client
40
     * @param string $nameStream Name stream
41
     */
42 4
    public function __construct(\Redis $client, string $nameStream)
43
    {
44 4
        $this->_client     = $client;
45 4
        $this->_streamName = $nameStream;
46 4
    }
47
48
    /**
49
     * Appends the specified stream entry to the stream at the specified key
50
     *
51
     * @param string $key    Key Message
52
     * @param array  $values Value Message
53
     *
54
     * @return string
55
     *
56
     * @throws \Exception
57
     *
58
     * @see https://redis.io/commands/xadd
59
     */
60 2
    public function add(string $key, array $values) : string
61
    {
62
        try {
63 2
            return (string) $this->_client->rawCommand(
64 2
                Constants::COMMAND_XADD,
65 2
                $this->_streamName,
66 2
                '*',
0 ignored issues
show
Unused Code introduced by
The call to Redis::rawCommand() has too many arguments starting with '*'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            return (string) $this->_client->/** @scrutinizer ignore-call */ rawCommand(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
                $key,
68 2
                json_encode($values)
69
            );
70
        } catch (\Exception $exception) {
71
            throw $exception;
72
        }
73
    }
74
75
    /**
76
     * Removes the messages entries from a stream
77
     *
78
     * @param string $key Key Message
79
     *
80
     * @return int
81
     *
82
     * @throws \Exception
83
     *
84
     * @see https://redis.io/commands/xdel
85
     */
86 1
    public function delete(string $key) : int
87
    {
88
        try {
89 1
            return (int) $this->_client->rawCommand(
90 1
                Constants::COMMAND_XDEL,
91 1
                $this->_streamName,
92
                $key
0 ignored issues
show
Unused Code introduced by
The call to Redis::rawCommand() has too many arguments starting with $key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
            return (int) $this->_client->/** @scrutinizer ignore-call */ rawCommand(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
93
            );
94
        } catch (\Exception $exception) {
95
            throw $exception;
96
        }
97
    }
98
99
    /**
100
     * Get data from stream
101
     *
102
     * @return Collection
103
     *
104
     * @throws \Exception
105
     *
106
     * @see https://redis.io/commands/xread
107
     */
108 2
    public function get() : Collection
109
    {
110
        try {
111 2
            $items = $this->_client->rawCommand(
112 2
                Constants::COMMAND_XREAD,
113 2
                'STREAMS',
114 2
                $this->_streamName,
0 ignored issues
show
Unused Code introduced by
The call to Redis::rawCommand() has too many arguments starting with $this->_streamName. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
            /** @scrutinizer ignore-call */ 
115
            $items = $this->_client->rawCommand(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
115 2
                '0'
116
            );
117
118 2
            $collection = new CollectionHydrator();
119
120 2
            if (empty($items) === true) {
121 1
                return new Collection;
122
            }
123
124 1
            return $collection->hydrate($items, Collection::class);
125
126
        } catch (\Exception $exception) {
127
            throw $exception;
128
        }
129
    }
130
131
    /**
132
     * Listen stream
133
     *
134
     * @param \Closure $closure User callback
135
     *
136
     * @return void
137
     *
138
     * @throws \ErrorException
139
     */
140
    public function listen(\Closure $closure) : void
141
    {
142
        $messageHydrate = new MessageHydrator();
143
144
        $loop = \React\EventLoop\Factory::create();
145
146
        $loop->addPeriodicTimer(
147
            Constants::TIME_TICK_INTERVAL,
148
            function () use ($closure, $messageHydrate, $loop) {
0 ignored issues
show
Unused Code introduced by
The import $loop is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
149
                $rows = $this->_client->rawCommand(
150
                    Constants::COMMAND_XRANGE,
151
                    $this->_streamName,
152
                    '-',
0 ignored issues
show
Unused Code introduced by
The call to Redis::rawCommand() has too many arguments starting with '-'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
                /** @scrutinizer ignore-call */ 
153
                $rows = $this->_client->rawCommand(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
153
                    '+'
154
                );
155
156
                if (empty($rows) === true) {
157
                    return;
158
                }
159
160
                foreach ($rows as $row) {
161
                    $message = $messageHydrate->hydrate($row, Message::class);
162
                    $closure->call($this, $message);
163
                    $this->delete($message->getId());
164
                }
165
            }
166
        );
167
168
        $loop->run();
169
    }
170
171
}