1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\Yaroc\Plugin; |
4
|
|
|
|
5
|
|
|
use drupol\Yaroc\Http\AbstractClient; |
6
|
|
|
|
7
|
|
|
abstract class AbstractMethodPlugin extends AbstractClient implements MethodPluginInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The endpoint. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $endpoint; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $parameters = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws \Http\Client\Exception |
27
|
|
|
* |
28
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
29
|
|
|
*/ |
30
|
|
|
public function call() |
31
|
|
|
{ |
32
|
|
|
$parameters = [ |
33
|
|
|
'jsonrpc' => '2.0', |
34
|
|
|
'id' => uniqid($this->getMethod() . '_'), |
35
|
|
|
'params' => $this->getParameters(), |
36
|
|
|
'method' => $this->getMethod(), |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$response = false; |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$response = $this->getHttpClient()->sendRequest( |
43
|
|
|
$this->getMessageFactory()->createRequest( |
44
|
|
|
'POST', |
45
|
|
|
$this->getEndpoint(), |
46
|
|
|
[], |
47
|
|
|
json_encode( |
48
|
|
|
$parameters |
49
|
|
|
) |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} catch (\Exception $exception) { |
53
|
|
|
// @todo: log this. |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $response; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function withEndPoint(string $endpoint) |
63
|
|
|
{ |
64
|
|
|
$clone = clone $this; |
65
|
|
|
|
66
|
|
|
return $clone->setEndPoint($endpoint); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getEndPoint() |
73
|
|
|
{ |
74
|
|
|
return $this->endpoint; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function withApiKey(string $apikey) |
81
|
|
|
{ |
82
|
|
|
$clone = clone $this; |
83
|
|
|
|
84
|
|
|
return $clone->setApiKey($apikey); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function withParameters(array $parameters) |
91
|
|
|
{ |
92
|
|
|
$clone = clone $this; |
93
|
|
|
|
94
|
|
|
return $clone->setParameters($parameters); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getParameters() |
101
|
|
|
{ |
102
|
|
|
return $this->parameters; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getMethod() |
109
|
|
|
{ |
110
|
|
|
return $this::METHOD; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $parameters |
115
|
|
|
* |
116
|
|
|
* @return \drupol\Yaroc\Plugin\AbstractMethodPlugin |
117
|
|
|
*/ |
118
|
|
|
private function setParameters(array $parameters) |
119
|
|
|
{ |
120
|
|
|
$this->parameters = $parameters; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $endpoint |
127
|
|
|
* |
128
|
|
|
* @return \drupol\Yaroc\Plugin\AbstractMethodPlugin |
129
|
|
|
*/ |
130
|
|
|
private function setEndPoint(string $endpoint) |
131
|
|
|
{ |
132
|
|
|
$this->endpoint = $endpoint; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $apikey |
139
|
|
|
* |
140
|
|
|
* @return \drupol\Yaroc\Plugin\AbstractMethodPlugin |
141
|
|
|
*/ |
142
|
|
|
private function setApiKey(string $apikey) |
143
|
|
|
{ |
144
|
|
|
$parameters = $this->getParameters(); |
145
|
|
|
|
146
|
|
|
$parameters += ['apiKey' => $apikey]; |
147
|
|
|
|
148
|
|
|
return $this->setParameters($parameters); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|