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\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 logger. |
43
|
|
|
* |
44
|
|
|
* @var \Psr\Log\LoggerInterface |
45
|
|
|
*/ |
46
|
|
|
protected $logger; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Client constructor. |
50
|
|
|
* |
51
|
|
|
* @param \Http\Client\HttpClient|NULL $httpClient |
52
|
|
|
* @param \Http\Message\UriFactory|NULL $uriFactory |
53
|
|
|
* @param \Psr\Log\LoggerInterface|NULL $logger |
54
|
|
|
*/ |
55
|
15 |
|
public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, LoggerInterface $logger = NULL) { |
56
|
15 |
|
$httpClient = $httpClient ?: HttpClientDiscovery::find(); |
57
|
|
|
|
58
|
|
|
$plugins = [ |
59
|
15 |
|
new HeaderDefaultsPlugin(['Content-Type' => 'application/json']), |
60
|
15 |
|
new LoggerPlugin($logger ?: new \drupol\Yaroc\Log\Logger(), new Formatter()), |
61
|
|
|
]; |
62
|
15 |
|
$httpClient = new HttpMethodsClient(new PluginClient($httpClient, $plugins), MessageFactoryDiscovery::find()); |
63
|
|
|
|
64
|
15 |
|
$this->setUriFactory($uriFactory ?: UriFactoryDiscovery::find()); |
65
|
15 |
|
parent::__construct($httpClient, MessageFactoryDiscovery::find()); |
66
|
15 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the Random.org endpoint. |
70
|
|
|
* |
71
|
|
|
* @param string $uri |
72
|
|
|
*/ |
73
|
15 |
|
public function setEndpoint($uri) { |
74
|
15 |
|
$this->endpoint = $this->getUriFactory()->createUri($uri); |
75
|
15 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the Random.org endpoint. |
79
|
|
|
* |
80
|
|
|
* @return UriInterface |
81
|
|
|
*/ |
82
|
12 |
|
public function getEndpoint() { |
83
|
12 |
|
return $this->endpoint; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Request. |
88
|
|
|
* |
89
|
|
|
* @param MethodPluginInterface $methodPlugin |
90
|
|
|
* |
91
|
|
|
* @return \Exception|ResponseInterface |
92
|
|
|
*/ |
93
|
12 |
|
public function request(MethodPluginInterface $methodPlugin) { |
94
|
12 |
|
$response = $this->post($this->getEndpoint(), [], json_encode($methodPlugin->getParameters())); |
95
|
|
|
|
96
|
12 |
|
return $this->validateResponse($response); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validate the response. |
101
|
|
|
* |
102
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
103
|
|
|
* |
104
|
|
|
* @return \Exception|ResponseInterface |
105
|
|
|
*/ |
106
|
12 |
|
public function validateResponse(ResponseInterface $response) { |
107
|
12 |
|
if (200 == $response->getStatusCode()) { |
108
|
12 |
|
$body = json_decode((string) $response->getBody()->getContents(), TRUE); |
109
|
|
|
|
110
|
12 |
|
if (isset($body['error']['code'])) { |
111
|
|
|
switch ($body['error']['code']) { |
112
|
|
View Code Duplication |
case -32600: |
|
|
|
|
113
|
|
|
throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']); |
114
|
|
|
case -32601: |
115
|
|
|
throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']); |
116
|
|
View Code Duplication |
case -32602: |
|
|
|
|
117
|
|
|
throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']); |
118
|
|
View Code Duplication |
case -32603: |
|
|
|
|
119
|
|
|
throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']); |
120
|
|
View Code Duplication |
default: |
|
|
|
|
121
|
|
|
throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
12 |
|
$response->getBody()->rewind(); |
127
|
|
|
|
128
|
12 |
|
return $response; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the logger. |
133
|
|
|
* |
134
|
|
|
* @return \Psr\Log\LoggerInterface |
135
|
|
|
*/ |
136
|
|
|
public function getLogger() { |
137
|
|
|
return $this->logger; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the logger. |
142
|
|
|
* |
143
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
144
|
|
|
*/ |
145
|
|
|
public function setLogger(LoggerInterface $logger) { |
146
|
|
|
$this->logger = $logger; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the UriFactory. |
151
|
|
|
* |
152
|
|
|
* @return \Http\Message\UriFactory |
153
|
|
|
*/ |
154
|
15 |
|
public function getUriFactory() { |
155
|
15 |
|
return $this->uriFactory; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param \Http\Message\UriFactory $uriFactory |
160
|
|
|
*/ |
161
|
15 |
|
public function setUriFactory(UriFactory $uriFactory) { |
162
|
15 |
|
$this->uriFactory = $uriFactory; |
163
|
15 |
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|