1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\Yaroc\Http; |
4
|
|
|
|
5
|
|
|
use drupol\Yaroc\Plugin\MethodPluginInterface; |
6
|
|
|
use Http\Client\Common\HttpMethodsClient; |
7
|
|
|
use Http\Client\Common\Plugin; |
8
|
|
|
use Http\Client\Common\Plugin\HeaderDefaultsPlugin; |
9
|
|
|
use Http\Client\Common\PluginClient; |
10
|
|
|
use Http\Client\Exception\NetworkException; |
11
|
|
|
use Http\Client\HttpClient; |
12
|
|
|
use Http\Discovery\HttpClientDiscovery; |
13
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
14
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
15
|
|
|
use Http\Message\UriFactory; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\UriInterface; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Client |
22
|
|
|
* |
23
|
|
|
* @package drupol\Yaroc\Http |
24
|
|
|
*/ |
25
|
|
|
class Client extends HttpMethodsClient { |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The default Random.org endpoint. |
29
|
|
|
* |
30
|
|
|
* @var UriInterface |
31
|
|
|
*/ |
32
|
|
|
protected $endpoint; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The URI Factory. |
36
|
|
|
* |
37
|
|
|
* @var UriFactory |
38
|
|
|
*/ |
39
|
|
|
protected $uriFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The HTTP plugins array. |
43
|
|
|
* |
44
|
|
|
* @var Plugin[] |
45
|
|
|
*/ |
46
|
|
|
protected $plugins; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Client constructor. |
50
|
|
|
* |
51
|
|
|
* @param \Http\Client\HttpClient|NULL $httpClient |
52
|
|
|
* @param \Http\Message\UriFactory|NULL $uriFactory |
53
|
|
|
*/ |
54
|
15 |
|
public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL) { |
55
|
15 |
|
$httpClient = $httpClient ?: HttpClientDiscovery::find(); |
56
|
15 |
|
$httpClient = new HttpMethodsClient(new PluginClient($httpClient, $this->setPlugins()->getPlugins()), MessageFactoryDiscovery::find()); |
57
|
15 |
|
$this->setUriFactory($uriFactory ?: UriFactoryDiscovery::find()); |
58
|
|
|
|
59
|
15 |
|
parent::__construct($httpClient, MessageFactoryDiscovery::find()); |
60
|
15 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the HTTP plugins array. |
64
|
|
|
* |
65
|
|
|
* @return Plugin[] |
66
|
|
|
*/ |
67
|
15 |
|
public function getPlugins() { |
68
|
15 |
|
return $this->plugins; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the HTTP plugins. |
73
|
|
|
* |
74
|
|
|
* @param Plugin[] $plugins |
75
|
|
|
* An array of HTTP plugin. |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
15 |
|
public function setPlugins(array $plugins = array()) { |
80
|
|
|
$defaultPlugins = [ |
81
|
15 |
|
new HeaderDefaultsPlugin(['Content-Type' => 'application/json']) |
82
|
|
|
]; |
83
|
|
|
|
84
|
15 |
|
$this->plugins = array_merge($defaultPlugins, $plugins); |
85
|
|
|
|
86
|
15 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the Random.org endpoint. |
91
|
|
|
* |
92
|
|
|
* @param string $uri |
93
|
|
|
*/ |
94
|
15 |
|
public function setEndpoint($uri) { |
95
|
15 |
|
$this->endpoint = $this->getUriFactory()->createUri($uri); |
96
|
15 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the Random.org endpoint. |
100
|
|
|
* |
101
|
|
|
* @return UriInterface |
102
|
|
|
*/ |
103
|
12 |
|
public function getEndpoint() { |
104
|
12 |
|
return $this->endpoint; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Request. |
109
|
|
|
* |
110
|
|
|
* @param MethodPluginInterface $methodPlugin |
111
|
|
|
* |
112
|
|
|
* @return null|ResponseInterface |
113
|
|
|
*/ |
114
|
12 |
|
public function request(MethodPluginInterface $methodPlugin) { |
115
|
|
|
try { |
116
|
12 |
|
$response = $this->post($this->getEndpoint(), [], json_encode($methodPlugin->getParameters())); |
117
|
|
|
} catch (NetworkException $e) { |
118
|
|
|
return NULL; |
119
|
|
|
} |
120
|
|
|
|
121
|
12 |
|
return $this->validateResponse($response); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Validate the response. |
126
|
|
|
* |
127
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
128
|
|
|
* |
129
|
|
|
* @return \Exception|ResponseInterface |
130
|
|
|
*/ |
131
|
12 |
|
public function validateResponse(ResponseInterface $response) { |
132
|
12 |
|
if (200 == $response->getStatusCode()) { |
133
|
12 |
|
$body = json_decode((string) $response->getBody()->getContents(), TRUE); |
134
|
|
|
|
135
|
12 |
|
if (isset($body['error']['code'])) { |
136
|
|
|
switch ($body['error']['code']) { |
137
|
|
View Code Duplication |
case -32600: |
|
|
|
|
138
|
|
|
throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']); |
139
|
|
|
case -32601: |
140
|
|
|
throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']); |
141
|
|
View Code Duplication |
case -32602: |
|
|
|
|
142
|
|
|
throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']); |
143
|
|
View Code Duplication |
case -32603: |
|
|
|
|
144
|
|
|
throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']); |
145
|
|
View Code Duplication |
default: |
|
|
|
|
146
|
|
|
throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
12 |
|
$response->getBody()->rewind(); |
152
|
|
|
|
153
|
12 |
|
return $response; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the logger. |
158
|
|
|
* |
159
|
|
|
* @return \Psr\Log\LoggerInterface |
160
|
|
|
*/ |
161
|
|
|
public function getLogger() { |
162
|
|
|
return $this->logger; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the logger. |
167
|
|
|
* |
168
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
169
|
|
|
*/ |
170
|
|
|
public function setLogger(LoggerInterface $logger) { |
171
|
|
|
$this->logger = $logger; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the UriFactory. |
176
|
|
|
* |
177
|
|
|
* @return \Http\Message\UriFactory |
178
|
|
|
*/ |
179
|
15 |
|
public function getUriFactory() { |
180
|
15 |
|
return $this->uriFactory; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param \Http\Message\UriFactory $uriFactory |
185
|
|
|
*/ |
186
|
15 |
|
public function setUriFactory(UriFactory $uriFactory) { |
187
|
15 |
|
$this->uriFactory = $uriFactory; |
188
|
15 |
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
|