Passed
Push — master ( 4668c7...1981ee )
by Sergey
02:36
created

Stream   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 46.51%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
eloc 43
c 5
b 1
f 3
dl 0
loc 125
ccs 20
cts 43
cp 0.4651
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 12 2
A get() 0 20 3
A __construct() 0 4 1
A listen() 0 29 4
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\Message;
15
use Asiries335\redisSteamPhp\Hydrator\CollectionHydrator;
16
use Asiries335\redisSteamPhp\Hydrator\MessageHydrator;
17
use Redis;
18
19
final class Stream
20
{
21
    /**
22
     * Client
23
     *
24
     * @var Redis
25
     */
26
    private $_client;
27
28
    /**
29
     * Name stream
30
     *
31
     * @var string
32
     */
33
    private $_streamName;
34
35
    /**
36
     * Stream constructor.
37
     *
38
     * @param Redis  $client     Redis Client
39
     * @param string $nameStream Name stream
40
     */
41 3
    public function __construct(\Redis $client, string $nameStream)
42
    {
43 3
        $this->_client     = $client;
44 3
        $this->_streamName = $nameStream;
45 3
    }
46
47
    /**
48
     * Appends the specified stream entry to the stream at the specified key
49
     *
50
     * @param string $key    Key Message
51
     * @param array  $values Value Message
52
     *
53
     * @return string
54
     *
55
     * @throws \Exception
56
     *
57
     * @see https://redis.io/commands/xadd
58
     */
59 1
    public function add(string $key, array $values) : string
60
    {
61
        try {
62 1
            return (string) $this->_client->rawCommand(
63 1
                'xadd',
64 1
                $this->_streamName,
65 1
                '*',
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

65
            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...
66
                $key,
67 1
                json_encode($values)
68
            );
69
        } catch (\Exception $exception) {
70
            throw $exception;
71
        }
72
    }
73
74
    /**
75
     * Get data from stream
76
     *
77
     * @return Collection
78
     *
79
     * @throws \Exception
80
     *
81
     * @see https://redis.io/commands/xread
82
     */
83 2
    public function get() : Collection
84
    {
85
        try {
86 2
            $items = $this->_client->rawCommand(
87 2
                'xread',
88 2
                'STREAMS',
89 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

89
            /** @scrutinizer ignore-call */ 
90
            $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...
90 2
                '0'
91
            );
92
93 2
            $collection = new CollectionHydrator();
94
95 2
            if (empty($items) === true) {
96 1
                return new Collection;
97
            }
98
99 1
            return $collection->hydrate($items, Collection::class);
100
101
        } catch (\Exception $exception) {
102
            throw $exception;
103
        }
104
    }
105
106
    /**
107
     * Listen stream
108
     *
109
     * @param \Closure $closure User callback
110
     *
111
     * @return void
112
     *
113
     * @throws \ErrorException
114
     */
115
    public function listen(\Closure $closure) : void
116
    {
117
        $messageHydrate = new MessageHydrator();
118
119
        $lastMessageId = null;
120
121
        while (true) {
122
            $data = $this->_client->rawCommand(
123
                'xrevrange',
124
                $this->_streamName,
125
                '+',
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

125
            /** @scrutinizer ignore-call */ 
126
            $data = $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...
126
                '-',
127
                'COUNT',
128
                1
129
            );
130
131
            if (empty($data) === true) {
132
                usleep(1);
133
                continue;
134
            }
135
136
            $message = $messageHydrate->hydrate($data[0], Message::class);
137
138
            if ($message->getId() !== $lastMessageId) {
139
                $lastMessageId = $message->getId();
140
                $closure->call($this, $message);
141
            }
142
143
            usleep(1);
144
        }
145
    }
146
147
}