1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ChildProcess\Messenger\Messages; |
4
|
|
|
|
5
|
|
|
use React\Promise\Deferred; |
6
|
|
|
use React\Promise\RejectedPromise; |
7
|
|
|
|
8
|
|
|
class Rpc implements \JsonSerializable, ActionableMessageInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $target; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Payload |
17
|
|
|
*/ |
18
|
|
|
protected $payload; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $uniqid; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $target |
27
|
|
|
* @param Payload $payload |
28
|
|
|
* @param string $uniqid |
29
|
|
|
*/ |
30
|
8 |
|
public function __construct($target, Payload $payload, $uniqid = '') |
31
|
|
|
{ |
32
|
8 |
|
$this->target = $target; |
33
|
8 |
|
$this->payload = $payload; |
34
|
8 |
|
$this->uniqid = $uniqid; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Payload |
39
|
|
|
*/ |
40
|
3 |
|
public function getPayload() |
41
|
|
|
{ |
42
|
3 |
|
return $this->payload; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $uniqid |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
|
|
public function setUniqid($uniqid) |
50
|
|
|
{ |
51
|
|
|
return new static($this->target, $this->payload, $uniqid); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
6 |
|
public function jsonSerialize() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
6 |
|
'type' => 'rpc', |
61
|
6 |
|
'uniqid' => $this->uniqid, |
62
|
6 |
|
'target' => $this->target, |
63
|
6 |
|
'payload' => $this->payload, |
64
|
6 |
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $bindTo |
69
|
|
|
* @param $source |
70
|
|
|
*/ |
71
|
4 |
|
public function handle($bindTo, $source) |
72
|
|
|
{ |
73
|
|
|
$cb = function ($target, $payload, $uniqid) { |
|
|
|
|
74
|
|
|
if (!$this->hasRpc($target)) { |
|
|
|
|
75
|
|
|
$this->getStderr()->write($this->createLine(Factory::rpcError($uniqid, [ |
|
|
|
|
76
|
|
|
'message' => 'Target doesn\'t exist', |
77
|
|
|
]))); |
78
|
1 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
$this->callRpc($target, $payload)->then( |
|
|
|
|
82
|
|
|
function (array $payload) use ($uniqid) { |
83
|
|
|
$this->getStdout()->write($this->createLine(Factory::rpcSuccess($uniqid, $payload))); |
|
|
|
|
84
|
2 |
|
}, |
85
|
|
|
function ($error) use ($uniqid) { |
86
|
|
|
$this->getStderr()->write($this->createLine(Factory::rpcError($uniqid, [ |
|
|
|
|
87
|
|
|
'error' => $error, |
88
|
|
|
]))); |
89
|
2 |
|
}, |
90
|
2 |
|
function (array $payload) use ($uniqid) { |
91
|
|
|
$this->getStdout()->write($this->createLine(Factory::rpcNotify($uniqid, $payload))); |
|
|
|
|
92
|
2 |
|
} |
93
|
|
|
); |
94
|
1 |
|
}; |
95
|
4 |
|
$cb = $cb->bindTo($bindTo); |
96
|
|
|
$cb($this->target, $this->payload, $this->uniqid); |
97
|
4 |
|
} |
98
|
|
|
} |
99
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.