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