1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
+------------------------------------------------------------------------+ |
4
|
|
|
| Plinker-RPC PHP | |
5
|
|
|
+------------------------------------------------------------------------+ |
6
|
|
|
| Copyright (c)2017-2018 (https://github.com/plinker-rpc/core) | |
7
|
|
|
+------------------------------------------------------------------------+ |
8
|
|
|
| This source file is subject to MIT License | |
9
|
|
|
| that is bundled with this package in the file LICENSE. | |
10
|
|
|
| | |
11
|
|
|
| If you did not receive a copy of the license and are unable to | |
12
|
|
|
| obtain it through the world-wide-web, please send an email | |
13
|
|
|
| to [email protected] so we can send you a copy immediately. | |
14
|
|
|
+------------------------------------------------------------------------+ |
15
|
|
|
| Authors: Lawrence Cherone <[email protected]> | |
16
|
|
|
+------------------------------------------------------------------------+ |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Plinker\Core; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Plinker\Core\Client |
23
|
|
|
*/ |
24
|
|
|
final class Client |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
private $component; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private $response; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var |
38
|
|
|
*/ |
39
|
|
|
private $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var |
43
|
|
|
*/ |
44
|
|
|
private $curl; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var |
48
|
|
|
*/ |
49
|
|
|
private $signer; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Class construct |
53
|
|
|
* |
54
|
|
|
* @param string $server - server enpoint url |
55
|
|
|
* @param array $config - config array which holds object configuration |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
3 |
|
public function __construct($server, array $config = []) |
59
|
|
|
{ |
60
|
3 |
|
$this->config = array_merge([ |
61
|
3 |
|
"server" => $server, |
62
|
|
|
"secret" => null |
63
|
3 |
|
], $config); |
64
|
|
|
|
65
|
|
|
// check and set client timeout |
66
|
3 |
|
if (!isset($this->config["timeout"]) || !is_numeric($this->config["timeout"])) { |
67
|
3 |
|
$this->config["timeout"] = 10; |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Magic getter method, which sets component |
73
|
|
|
* |
74
|
|
|
* @param string $component |
75
|
|
|
* @return object |
76
|
|
|
*/ |
77
|
1 |
|
public function __get($component) |
78
|
|
|
{ |
79
|
1 |
|
$this->component = $component; |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Magic caller method, which calls component |
86
|
|
|
* |
87
|
|
|
* @param string $action |
88
|
|
|
* @param array $params |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function __call($action, $params) |
92
|
|
|
{ |
93
|
|
|
// load curl |
94
|
|
|
if (!$this->curl) { |
95
|
|
|
$this->curl = new Lib\Curl($this->config); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// load signer |
99
|
|
|
if (!$this->signer) { |
100
|
|
|
$this->signer = new Lib\Signer($this->config); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// change params array into numeric |
104
|
|
|
$params = array_values($params); |
105
|
|
|
|
106
|
|
|
// unset local private key |
107
|
|
|
unset($this->config["plinker"]["private_key"]); |
108
|
|
|
|
109
|
|
|
// encode payload |
110
|
|
|
$payload = $this->signer->encode([ |
111
|
|
|
"component" => $this->component, |
112
|
|
|
"config" => $this->config, |
113
|
|
|
"action" => $action, |
114
|
|
|
"params" => $params |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
// post request to plinker server |
118
|
|
|
$this->response = $this->curl->post($this->config["server"], $payload, [ |
119
|
|
|
"PLINKER: ".$payload["token"] |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
return unserialize($this->response); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|