1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\eCurring\Http\Adapter\Guzzle; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
9
|
|
|
use LauLamanApps\eCurring\Exception\eCurringException; |
10
|
|
|
use LauLamanApps\eCurring\Http\Adapter\Guzzle\Exception\Handler; |
11
|
|
|
use LauLamanApps\eCurring\Http\Adapter\Guzzle\Exception\NotFoundException; |
12
|
|
|
use LauLamanApps\eCurring\Http\ClientInterface; |
13
|
|
|
use LauLamanApps\eCurring\Http\Endpoint\Exception\EndpointCouldNotBeMappedException; |
14
|
|
|
use LauLamanApps\eCurring\Http\Endpoint\MapperInterface; |
15
|
|
|
use LauLamanApps\eCurring\Http\Exception\ApiCallException; |
16
|
|
|
use LauLamanApps\eCurring\Resource\Curser\Pagination; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
final class Client implements ClientInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var GuzzleClientInterface |
23
|
|
|
*/ |
24
|
|
|
private $guzzleClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var MapperInterface |
28
|
|
|
*/ |
29
|
|
|
private $endpointMapper; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
GuzzleClientInterface $guzzleClient, |
33
|
|
|
MapperInterface $urlMapper |
34
|
|
|
) { |
35
|
|
|
$this->guzzleClient = $guzzleClient; |
36
|
|
|
$this->endpointMapper = $urlMapper; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws EndpointCouldNotBeMappedException |
41
|
|
|
* @throws eCurringException |
42
|
|
|
* @throws NotFoundException |
43
|
|
|
*/ |
44
|
|
|
public function getEndpoint(string $endpoint, ?array $urlBits = [], ?Pagination $page = null): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
$options = $page ? ['query' => $page->getQueryOptions()] : []; |
47
|
|
|
|
48
|
|
|
return $this->get( |
49
|
|
|
$this->endpointMapper->map($endpoint, $urlBits), |
50
|
|
|
$options |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws eCurringException |
56
|
|
|
* @throws NotFoundException |
57
|
|
|
* @return ResponseInterface |
58
|
|
|
*/ |
59
|
|
|
public function getUrl(string $url): ResponseInterface |
60
|
|
|
{ |
61
|
|
|
return $this->get($url); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getJson(ResponseInterface $response): string |
65
|
|
|
{ |
66
|
|
|
return (string) $response->getBody(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws NotFoundException |
71
|
|
|
* @throws eCurringException |
72
|
|
|
*/ |
73
|
|
|
private function get(string $url, ?array $options = []): ResponseInterface |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$response = $this->guzzleClient->get($url, $options); |
77
|
|
|
|
78
|
|
|
$this->wasSuccessfulRequest($response); |
79
|
|
|
|
80
|
|
|
return $response; |
81
|
|
|
} catch (RequestException $exception) { |
82
|
|
|
Handler::handleRequestException($exception); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws eCurringException |
88
|
|
|
*/ |
89
|
|
|
public function postEndpoint(string $endpoint, array $data, array $urlBits = null): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
return $this->post( |
92
|
|
|
$this->endpointMapper->map($endpoint, $urlBits), |
|
|
|
|
93
|
|
|
['json' => $data] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws ApiCallException |
99
|
|
|
* @throws eCurringException |
100
|
|
|
* @throws NotFoundException |
101
|
|
|
*/ |
102
|
|
|
private function post(string $url, ?array $options = []): ResponseInterface |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$response = $this->guzzleClient->post($url, $options); |
106
|
|
|
$this->wasSuccessfulRequest($response); |
107
|
|
|
|
108
|
|
|
return $response; |
109
|
|
|
} catch (RequestException $exception) { |
110
|
|
|
Handler::handleRequestException($exception); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @throws eCurringException |
116
|
|
|
*/ |
117
|
|
|
public function patchEndpoint(string $endpoint, array $data, array $urlBits = null): ResponseInterface |
118
|
|
|
{ |
119
|
|
|
return $this->patch( |
120
|
|
|
$this->endpointMapper->map($endpoint, $urlBits), |
|
|
|
|
121
|
|
|
['json' => $data] |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws ApiCallException |
127
|
|
|
* @throws eCurringException |
128
|
|
|
* @throws NotFoundException |
129
|
|
|
*/ |
130
|
|
|
private function patch(string $url, ?array $options = []): ResponseInterface |
131
|
|
|
{ |
132
|
|
|
try { |
133
|
|
|
$response = $this->guzzleClient->patch($url, $options); |
134
|
|
|
$this->wasSuccessfulRequest($response); |
135
|
|
|
|
136
|
|
|
return $response; |
137
|
|
|
} catch (RequestException $exception) { |
138
|
|
|
Handler::handleRequestException($exception); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws eCurringException |
144
|
|
|
*/ |
145
|
|
|
public function deleteEndpoint(string $endpoint, array $data, array $urlBits = null): void |
146
|
|
|
{ |
147
|
|
|
$this->delete( |
148
|
|
|
$this->endpointMapper->map($endpoint, $urlBits), |
|
|
|
|
149
|
|
|
['json' => $data] |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @throws ApiCallException |
155
|
|
|
* @throws eCurringException |
156
|
|
|
* @throws NotFoundException |
157
|
|
|
*/ |
158
|
|
|
private function delete(string $url, ?array $options = []): ResponseInterface |
159
|
|
|
{ |
160
|
|
|
try { |
161
|
|
|
$response = $this->guzzleClient->delete($url, $options); |
162
|
|
|
$this->wasSuccessfulRequest($response); |
163
|
|
|
|
164
|
|
|
return $response; |
165
|
|
|
} catch (RequestException $exception) { |
166
|
|
|
Handler::handleRequestException($exception); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @throws ApiCallException |
172
|
|
|
*/ |
173
|
|
|
private function wasSuccessfulRequest(ResponseInterface $response): void |
174
|
|
|
{ |
175
|
|
|
switch ($response->getStatusCode()) { |
176
|
|
|
case 200: |
177
|
|
|
return; |
178
|
|
|
|
179
|
|
|
break; |
|
|
|
|
180
|
|
|
case 201: |
181
|
|
|
return; |
182
|
|
|
|
183
|
|
|
break; |
184
|
|
|
default: |
185
|
|
|
throw new ApiCallException( |
186
|
|
|
sprintf( |
187
|
|
|
'%s %s: %s', |
188
|
|
|
$response->getStatusCode(), |
189
|
|
|
$response->getReasonPhrase(), |
190
|
|
|
$response->getBody()->getContents() |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: