Skip to content

Commit 1ace1b8

Browse files
committed
Handle the decoding of batch responses
1 parent 971e448 commit 1ace1b8

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

src/EdgeTelemetrics/JSON_RPC/React/Decoder.php

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,59 +65,52 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
6565
}
6666

6767
//@TODO Handle json-rpc batch (array of data)
68-
public function handleData($data)
68+
public function handleData($input)
6969
{
70-
if (!isset($data['jsonrpc']))
70+
/** Check if we are batch request */
71+
if (!isset($input[0]))
7172
{
72-
throw new RuntimeException('Unable to decode. Missing required jsonrpc field');
73+
$input = [$input];
7374
}
7475

75-
if ($data['jsonrpc'] != Notification::JSONRPC_VERSION)
76-
{
77-
throw new RuntimeException('Unknown JSON-RPC version string');
78-
}
79-
80-
if (isset($data['method']))
81-
{
82-
if (isset($data['id']))
83-
{
84-
$jsonrpc = new Request();
85-
$jsonrpc->setId($data['id']);
76+
/** Process responses whether batch or individual one by one and emit it up the the higher levels */
77+
foreach($input as $data) {
78+
if (!isset($data['jsonrpc'])) {
79+
throw new RuntimeException('Unable to decode. Missing required jsonrpc field');
8680
}
87-
else
88-
{
89-
$jsonrpc = new Notification();
90-
}
91-
$jsonrpc->setMethod($data['method']);
92-
if (isset($data['params']))
93-
{
94-
$jsonrpc->setParams($data['params']);
95-
}
96-
}
97-
elseif (isset($data['result']) || isset($data['error']))
98-
{
99-
$jsonrpc = new Response();
100-
$jsonrpc->setId($data['id']);
101-
if (isset($data['result']))
102-
{
103-
$jsonrpc->setResult($data['result']);
81+
82+
if ($data['jsonrpc'] != Notification::JSONRPC_VERSION) {
83+
throw new RuntimeException('Unknown JSON-RPC version string');
10484
}
105-
else
106-
{
107-
$error = new Error($data['error']['code'], $data['error']['message']);
108-
if (isset($data['error']['data']))
109-
{
110-
$error->setData($data['error']['data']);
85+
86+
if (isset($data['method'])) {
87+
if (isset($data['id'])) {
88+
$jsonrpc = new Request();
89+
$jsonrpc->setId($data['id']);
90+
} else {
91+
$jsonrpc = new Notification();
92+
}
93+
$jsonrpc->setMethod($data['method']);
94+
if (isset($data['params'])) {
95+
$jsonrpc->setParams($data['params']);
96+
}
97+
} elseif (isset($data['result']) || isset($data['error'])) {
98+
$jsonrpc = new Response();
99+
$jsonrpc->setId($data['id']);
100+
if (isset($data['result'])) {
101+
$jsonrpc->setResult($data['result']);
102+
} else {
103+
$error = new Error($data['error']['code'], $data['error']['message']);
104+
if (isset($data['error']['data'])) {
105+
$error->setData($data['error']['data']);
106+
}
107+
$jsonrpc->setError($error);
111108
}
112-
$jsonrpc->setError($error);
109+
} else {
110+
throw new RuntimeException('Unable to decode json rpc packet');
113111
}
112+
$this->emit('data', [$jsonrpc]);
114113
}
115-
else
116-
{
117-
throw new RuntimeException('Unable to decode json rpc packet');
118-
}
119-
120-
$this->emit('data', [$jsonrpc]);
121114
}
122115

123116
/** @internal */

0 commit comments

Comments
 (0)