-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
executable file
·255 lines (230 loc) · 7.1 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
declare(strict_types=1);
namespace MaplePHP\Http;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\ClientInterface;
use MaplePHP\Http\Interfaces\StreamInterface;
use MaplePHP\Http\Exceptions\ClientException;
use MaplePHP\Http\Exceptions\RequestException;
use MaplePHP\Http\Exceptions\NetworkException;
use InvalidArgumentException;
class Client implements ClientInterface
{
public const DEFAULT_TIMEOUT = 30;
public const DEFAULT_AUTH = CURLAUTH_DIGEST;
private $options;
private $curl;
private $requestData;
private $requestDataLength;
private $requestResponse;
private $requestMeta;
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
* Set option
* https://www.php.net/manual/en/function.curl-setopt.php
* @param int $key
* @param mixed $value
* @return void
*/
public function setOption(int $key, mixed $value): void
{
$this->options[$key] = $value;
}
/**
* Has option
* https://www.php.net/manual/en/function.curl-setopt.php
* @param int $key
* @return bool
*/
public function hasOption(int $key): bool
{
return (isset($this->options[$key]));
}
/**
* Sends a PSR-7 request and returns a PSR-7 response.
* @param RequestInterface $request
* @return ResponseInterface
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
$this->requestData = (string)$request->getBody();
$this->requestDataLength = strlen($this->requestData);
$this->prepareRequest($request);
try {
$this->curl = curl_init();
$this->buildHeaders($request);
if (!extension_loaded('curl')) {
throw new InvalidArgumentException('You need to enable CURL on your server.');
}
// Init curl request
$this->buildOptions();
$this->buildFromMethods($request);
// Execute request
$this->createRequest();
// Close curl request
curl_close($this->curl);
// Retrive the body
return $this->createResponse();
} catch (InvalidArgumentException $e) {
throw new RequestException($e->getMessage(), 1);
} catch (NetworkException $e) {
throw $e;
}
}
/**
* Build client curl methods
* @param RequestInterface $request
* @return void
*/
protected function buildFromMethods(RequestInterface $request): void
{
switch ($request->getMethod()) {
case 'GET':
$this->get();
break;
case 'POST':
$this->post();
break;
case 'PUT':
$this->put();
break;
case 'PATCH':
$request = $this->patch($request);
break;
case 'DELETE':
$this->delete();
break;
default:
throw new InvalidArgumentException('The request method (' . $request->getMethod() . ') is not supported.');
break;
}
}
/**
* This will open init the request
* @param RequestInterface $request
* @return void
*/
protected function prepareRequest(RequestInterface $request): void
{
$this->setOption(CURLOPT_URL, $request->getUri()->getUri());
$this->setOption(CURLOPT_RETURNTRANSFER, true);
// Default auth option if get user name
if (!$this->hasOption(CURLOPT_HTTPAUTH) && !is_null($request->getUri()->getPart("user"))) {
$this->setOption(CURLOPT_HTTPAUTH, static::DEFAULT_AUTH);
}
if (!$this->hasOption(CURLOPT_TIMEOUT)) {
$this->setOption(CURLOPT_TIMEOUT, static::DEFAULT_TIMEOUT);
}
}
protected function createResponse(): ResponseInterface
{
$stream = new Stream(Stream::TEMP);
$stream->write($this->requestResponse);
if (!$stream->isSeekable()) {
throw new RequestException("Request body is not seekable", 1);
}
$stream->seek(0);
return new Response($stream);
}
/**
* Main request. This will be used for all the request.
* @return void
*/
final protected function createRequest(): void
{
$this->requestResponse = curl_exec($this->curl);
if ($this->requestResponse === false) {
throw new NetworkException(curl_error($this->curl), 1);
}
$this->requestMeta = curl_getinfo($this->curl);
}
/**
* Get request
* @return void
*/
protected function get(): void
{
// Is empty placeholder at the moment, does not need any more code fo it to work buy..
// you could extend and add your own functionality to it if you want
}
/**
* Post request
* @return void
*/
protected function post(): void
{
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->requestData);
curl_setopt($this->curl, CURLOPT_POST, 1);
}
/**
* Put request
* @return void
*/
protected function put(): void
{
curl_setopt($this->curl, CURLOPT_INFILE, $this->createParsedBody()->getResource());
curl_setopt($this->curl, CURLOPT_INFILESIZE, $this->requestDataLength);
curl_setopt($this->curl, CURLOPT_PUT, true);
}
/**
* Path request
* @return RequestInterface
*/
protected function patch(RequestInterface $request): RequestInterface
{
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->requestData);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
$request = $request->withHeader("content-type", "application/json-patch+json");
return $request;
}
/**
* Delete request
* @return void
*/
protected function delete(): void
{
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
/**
* Will build you options
* @return void
*/
private function buildOptions(): void
{
foreach ($this->options as $i => $val) {
if (!is_int($i)) {
throw new ClientException("The options key needs to be an integer!", 1);
}
curl_setopt($this->curl, $i, $val);
}
}
/**
* Build the headers
* @return void
*/
private function buildHeaders(RequestInterface $request): void
{
$data = [];
foreach ($request->getHeaders() as $name => $_unUsedVal) {
$data[] = "{$name}: " . $request->getHeaderLine($name);
}
if (count($data) > 0) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $data);
}
}
/**
* Parsed body can be used in e.g. put method
* @return StreamInterface
*/
private function createParsedBody(): StreamInterface
{
$stream = new Stream('php://memory', 'rw');
$stream->write($this->requestData);
$stream->rewind();
return $stream;
}
}