Passed
Pull Request — master (#2)
by Sergey
07:38
created

Stream::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 9
c 2
b 1
f 1
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 8
cp 0.75
crap 2.0625
rs 9.9666
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\Actions\ListenAction;
0 ignored issues
show
Bug introduced by
The type Asiries335\redisSteamPhp\Actions\ListenAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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

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

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

129
                /** @scrutinizer ignore-call */ 
130
                $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...
130
                    '+'
131
                );
132
133
                if (empty($rows) === true) {
134
                    return;
135
                }
136
137
                foreach ($rows as $row) {
138
                    $message = $messageHydrate->hydrate($row, Message::class);
139
                    $closure->call($this, $message);
140
                }
141
            }
142
        );
143
144
        $loop->run();
145
    }
146
147
}