|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FAPI\Sylius\Api; |
|
11
|
|
|
|
|
12
|
|
|
use FAPI\Sylius\Exception\Domain as DomainExceptions; |
|
13
|
|
|
use FAPI\Sylius\Exception\DomainException; |
|
14
|
|
|
use FAPI\Sylius\Hydrator\Hydrator; |
|
15
|
|
|
use FAPI\Sylius\Hydrator\NoopHydrator; |
|
16
|
|
|
use FAPI\Sylius\RequestBuilder; |
|
17
|
|
|
use Http\Client\HttpClient; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class HttpApi |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var HttpClient |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $httpClient; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Hydrator |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $hydrator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var RequestBuilder |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $requestBuilder; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->httpClient = $httpClient; |
|
43
|
|
|
$this->requestBuilder = $requestBuilder; |
|
44
|
|
|
if (!$hydrator instanceof NoopHydrator) { |
|
45
|
|
|
$this->hydrator = $hydrator; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Send a GET request with query parameters. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path Request path |
|
53
|
|
|
* @param array $params GET parameters |
|
54
|
|
|
* @param array $requestHeaders Request Headers |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
57
|
|
|
{ |
|
58
|
|
|
if (\count($params) > 0) { |
|
59
|
|
|
$path .= '?'.\http_build_query($params); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->httpClient->sendRequest( |
|
63
|
|
|
$this->requestBuilder->create('GET', $path, $requestHeaders) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Send a POST request with JSON-encoded parameters. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $path Request path |
|
71
|
|
|
* @param array $params POST parameters to be JSON encoded |
|
72
|
|
|
* @param array $requestHeaders Request headers |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->httpPostRaw($path, $this->createJsonBody($params), $requestHeaders); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Send a POST request with raw data. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $path Request path |
|
83
|
|
|
* @param array|string $body Request body |
|
84
|
|
|
* @param array $requestHeaders Request headers |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface |
|
87
|
|
|
{ |
|
88
|
|
|
return $response = $this->httpClient->sendRequest( |
|
|
|
|
|
|
89
|
|
|
$this->requestBuilder->create('POST', $path, $requestHeaders, $body) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Send a PUT request with JSON-encoded parameters. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $path Request path |
|
97
|
|
|
* @param array $params POST parameters to be JSON encoded |
|
98
|
|
|
* @param array $requestHeaders Request headers |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->httpClient->sendRequest( |
|
103
|
|
|
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createJsonBody($params)) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Send a PATCH request with JSON-encoded parameters. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $path Request path |
|
111
|
|
|
* @param array $params POST parameters to be JSON encoded |
|
112
|
|
|
* @param array $requestHeaders Request headers |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function httpPatch(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->httpClient->sendRequest( |
|
117
|
|
|
$this->requestBuilder->create('PATCH', $path, $requestHeaders, $this->createJsonBody($params)) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $path Request path |
|
125
|
|
|
* @param array $params POST parameters to be JSON encoded |
|
126
|
|
|
* @param array $requestHeaders Request headers |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->httpClient->sendRequest( |
|
131
|
|
|
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createJsonBody($params)) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Handle HTTP errors. |
|
137
|
|
|
* |
|
138
|
|
|
* Call is controlled by the specific API methods. |
|
139
|
|
|
* |
|
140
|
|
|
* @throws DomainException |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function handleErrors(ResponseInterface $response) |
|
143
|
|
|
{ |
|
144
|
|
|
switch ($response->getStatusCode()) { |
|
145
|
|
|
case 401: |
|
146
|
|
|
throw new DomainExceptions\UnauthorizedException(); |
|
147
|
|
|
case 404: |
|
148
|
|
|
throw new DomainExceptions\NotFoundException(); |
|
149
|
|
|
default: |
|
150
|
|
|
throw new DomainExceptions\UnknownErrorException(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Create a JSON encoded version of an array of parameters. |
|
156
|
|
|
* |
|
157
|
|
|
* @param array $params Request parameters |
|
158
|
|
|
* |
|
159
|
|
|
* @return null|string |
|
160
|
|
|
*/ |
|
161
|
|
|
private function createJsonBody(array $params) |
|
162
|
|
|
{ |
|
163
|
|
|
return (0 === \count($params)) ? null : \json_encode($params, empty($params) ? \JSON_FORCE_OBJECT : 0); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.