GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#13)
by Cees-Jan
02:31
created

Messenger::onData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace WyriHaximus\React\ChildProcess\Messenger;
4
5
use Evenement\EventEmitter;
6
use React\Promise\PromiseInterface;
7
use React\Promise\RejectedPromise;
8
use React\Socket\ConnectionInterface;
9
use WyriHaximus\React\ChildProcess\Messenger\Messages\ActionableMessageInterface;
10
use WyriHaximus\React\ChildProcess\Messenger\Messages\Error;
11
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessagesFactory;
12
use WyriHaximus\React\ChildProcess\Messenger\Messages\LineInterface;
13
use WyriHaximus\React\ChildProcess\Messenger\Messages\Message;
14
use WyriHaximus\React\ChildProcess\Messenger\Messages\Rpc;
15
16
class Messenger extends EventEmitter
17
{
18
    const INTERVAL = 0.1;
19
    const TERMINATE_RPC = 'wyrihaximus.react.child-process.messenger.terminate';
20
21
    /**
22
     * @var ConnectionInterface
23
     */
24
    protected $connection;
25
26
    /**
27
     * @var OutstandingCalls
28
     */
29
    protected $outstandingRpcCalls;
30
31
    /**
32
     * @var array
33
     */
34
    protected $rpcs = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $options = [];
40
41
    /**
42
     * @var string[]
43
     */
44
    protected $buffer = '';
45
46
    protected $defaultOptions = [
47
        'lineClass' => 'WyriHaximus\React\ChildProcess\Messenger\Messages\Line',
48
        'messageFactoryClass' => 'WyriHaximus\React\ChildProcess\Messenger\Messages\Factory',
49
        'lineOptions' => [],
50
    ];
51
52
    /**
53
     * Messenger constructor.
54
     * @param ConnectionInterface $connection
55
     * @param array               $options
56
     */
57 8
    public function __construct(
58
        ConnectionInterface $connection,
59
        array $options = []
60
    ) {
61 8
        $this->connection = $connection;
62
63 8
        $this->options = $this->defaultOptions + $options;
64
65 8
        $this->outstandingRpcCalls = new OutstandingCalls();
66
67
        $this->connection->on('data', function ($data) {
68 3
            $this->buffer .= $data;
69 3
            $this->emit('data', [$data]);
70 3
            $this->handleData();
71 8
        });
72 8
    }
73
74
    /**
75
     * @param string   $target
76
     * @param callable $listener
77
     */
78 2
    public function registerRpc($target, callable $listener)
79
    {
80 2
        $this->rpcs[$target] = $listener;
81 2
    }
82
83
    /**
84
     * @param string $target
85
     */
86 1
    public function deregisterRpc($target)
87
    {
88 1
        unset($this->rpcs[$target]);
89 1
    }
90
91
    /**
92
     * @param  string $target
93
     * @return bool
94
     */
95 1
    public function hasRpc($target)
96
    {
97 1
        return isset($this->rpcs[$target]);
98
    }
99
100
    /**
101
     * @param $target
102
     * @param $payload
103
     * @return React\Promise\PromiseInterface
104
     */
105 2
    public function callRpc($target, $payload)
106
    {
107
        try {
108 2
            $promise = $this->rpcs[$target]($payload, $this);
109 2
            if ($promise instanceof PromiseInterface) {
110 1
                return $promise;
111
            }
112
113 1
            throw new \Exception('RPC must return promise');
114 1
        } catch (\Exception $exception) {
115 1
            return new RejectedPromise($exception);
116
        } catch (\Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
117
            return new RejectedPromise($exception);
118
        }
119
    }
120
121
    /**
122
     * @param Message $message
123
     */
124 1
    public function message(Message $message)
125
    {
126 1
        $this->write($this->createLine($message));
127 1
    }
128
129
    /**
130
     * @param Error $error
131
     */
132 1
    public function error(Error $error)
133
    {
134 1
        $this->write($this->createLine($error));
135 1
    }
136
137
    /**
138
     * @param  string          $uniqid
139
     * @return OutstandingCall
140
     */
141 1
    public function getOutstandingCall($uniqid)
142
    {
143 1
        return $this->outstandingRpcCalls->getCall($uniqid);
144
    }
145
146
    /**
147
     * @param  Rpc                    $rpc
148
     * @return \React\Promise\Promise
149
     */
150
    public function rpc(Rpc $rpc)
151
    {
152 2
        $callReference = $this->outstandingRpcCalls->newCall(function () {
153 2
        });
154
155 2
        $this->write($this->createLine($rpc->setUniqid($callReference->getUniqid())));
156
157 2
        return $callReference->getDeferred()->promise();
158
    }
159
160
    /**
161
     * @param  ActionableMessageInterface $line
162
     * @return LineInterface
163
     */
164 4
    public function createLine(ActionableMessageInterface $line)
165
    {
166 4
        $lineCLass = $this->options['lineClass'];
167
168 4
        return (string) new $lineCLass($line, $this->options['lineOptions']);
169
    }
170
171
    /**
172
     * @return \React\Promise\Promise
173
     */
174 1
    public function softTerminate()
175
    {
176 1
        return $this->rpc(MessagesFactory::rpc(static::TERMINATE_RPC));
177
    }
178
179
    /**
180
     * @param string $line
181
     */
182 4
    public function write($line)
183
    {
184 4
        $this->connection->write($line);
185 4
    }
186
187 3
    private function handleData()
188
    {
189 3
        if (strpos($this->buffer, LineInterface::EOL) === false) {
190 1
            return;
191
        }
192
193 2
        $messages = explode(LineInterface::EOL, $this->buffer);
194 2
        $this->buffer = array_pop($messages);
195 2
        $this->iterateMessages($messages);
196 2
    }
197
198 2
    private function iterateMessages(array $messages)
199
    {
200 2
        foreach ($messages as $message) {
201
            try {
202 2
                MessagesFactory::fromLine($message, [])->handle($this, 'source');
203
            } catch (\Exception $exception) {
204
                $this->emit('error', [$exception, $this]);
205
            } catch (\Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
206 2
                $this->emit('error', [$exception, $this]);
207
            }
208
        }
209 2
    }
210
}
211