1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ChildProcess\Messenger\Messages; |
4
|
|
|
|
5
|
|
|
class Rpc implements \JsonSerializable, ActionableMessageInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $target; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Payload |
14
|
|
|
*/ |
15
|
|
|
protected $payload; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $uniqid; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $target |
24
|
|
|
* @param Payload $payload |
25
|
|
|
* @param string $uniqid |
26
|
|
|
*/ |
27
|
10 |
|
public function __construct($target, Payload $payload, $uniqid = '') |
28
|
|
|
{ |
29
|
10 |
|
$this->target = $target; |
30
|
10 |
|
$this->payload = $payload; |
31
|
10 |
|
$this->uniqid = $uniqid; |
32
|
10 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Payload |
36
|
|
|
*/ |
37
|
3 |
|
public function getPayload() |
38
|
|
|
{ |
39
|
3 |
|
return $this->payload; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $uniqid |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
3 |
|
public function setUniqid($uniqid) |
47
|
|
|
{ |
48
|
3 |
|
return new static($this->target, $this->payload, $uniqid); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
9 |
|
public function jsonSerialize() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
9 |
|
'type' => 'rpc', |
58
|
9 |
|
'uniqid' => $this->uniqid, |
59
|
9 |
|
'target' => $this->target, |
60
|
9 |
|
'payload' => $this->payload, |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $bindTo |
66
|
|
|
* @param $source |
67
|
|
|
*/ |
68
|
3 |
|
public function handle($bindTo, $source) |
69
|
|
|
{ |
70
|
|
|
$cb = function ($target, $payload, $uniqid) { |
|
|
|
|
71
|
3 |
|
if (!$this->hasRpc($target)) { |
|
|
|
|
72
|
|
|
$this->write($this->createLine(Factory::rpcError($uniqid, new \Exception('Target doesn\'t exist')))); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
$this->callRpc($target, $payload)->done( |
|
|
|
|
78
|
|
|
function (array $payload) use ($uniqid) { |
79
|
1 |
|
$this->write($this->createLine(Factory::rpcSuccess($uniqid, $payload))); |
|
|
|
|
80
|
3 |
|
}, |
81
|
|
|
function ($error) use ($uniqid) { |
82
|
2 |
|
$this->write($this->createLine(Factory::rpcError($uniqid, $error))); |
|
|
|
|
83
|
3 |
|
}, |
84
|
3 |
|
function ($payload) use ($uniqid) { |
85
|
|
|
$this->write($this->createLine(Factory::rpcNotify($uniqid, $payload))); |
|
|
|
|
86
|
3 |
|
} |
87
|
|
|
); |
88
|
3 |
|
}; |
89
|
3 |
|
$cb = $cb->bindTo($bindTo); |
90
|
3 |
|
$cb($this->target, $this->payload, $this->uniqid); |
91
|
3 |
|
} |
92
|
|
|
} |
93
|
|
|
|
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.