1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[email protected]> |
4
|
|
|
* @created: 26.01.15, 14:44 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Response; |
8
|
|
|
|
9
|
|
|
use Commercetools\Core\Client\Adapter\PromiseGetInterface; |
10
|
|
|
use Commercetools\Core\Error\ErrorContainer; |
11
|
|
|
use GuzzleHttp\Ring\Future\FutureInterface; |
|
|
|
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Commercetools\Core\Client\Adapter\AdapterPromiseInterface; |
14
|
|
|
use Commercetools\Core\Error\Message; |
15
|
|
|
use Commercetools\Core\Model\Common\Context; |
16
|
|
|
use Commercetools\Core\Model\Common\ContextAwareInterface; |
17
|
|
|
use Commercetools\Core\Model\Common\ContextTrait; |
18
|
|
|
use Commercetools\Core\Request\ClientRequestInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @package Commercetools\Core\Response |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractApiResponse implements ApiResponseInterface, ContextAwareInterface, ApiPromiseGetInterface |
24
|
|
|
{ |
25
|
|
|
const X_CORRELATION_ID = 'X-Correlation-ID'; |
26
|
|
|
use ContextTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ResponseInterface|AdapterPromiseInterface |
30
|
|
|
*/ |
31
|
|
|
protected $response; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ClientRequestInterface |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $jsonData; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $responseBody; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ErrorContainer |
50
|
|
|
*/ |
51
|
|
|
private $errors; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ResponseInterface $response |
55
|
|
|
* @param ClientRequestInterface $request |
56
|
|
|
* @param Context $context |
57
|
|
|
*/ |
58
|
790 |
|
public function __construct(ResponseInterface $response, ClientRequestInterface $request, Context $context = null) |
59
|
|
|
{ |
60
|
790 |
|
$this->setContext($context); |
61
|
790 |
|
$this->response = $response; |
62
|
790 |
|
$this->request = $request; |
63
|
790 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed|null |
67
|
|
|
*/ |
68
|
40 |
|
public function toObject() |
69
|
|
|
{ |
70
|
40 |
|
if (!$this->isError()) { |
71
|
39 |
|
return $this->getRequest()->mapFromResponse($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
732 |
|
public function toArray() |
81
|
|
|
{ |
82
|
732 |
|
if (is_null($this->jsonData)) { |
|
|
|
|
83
|
732 |
|
$this->jsonData = json_decode($this->getBody(), true); |
84
|
|
|
} |
85
|
732 |
|
return $this->jsonData; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
735 |
|
public function getBody() |
92
|
|
|
{ |
93
|
735 |
|
if (is_null($this->responseBody)) { |
|
|
|
|
94
|
735 |
|
$this->responseBody = (string)$this->response->getBody(); |
95
|
|
|
} |
96
|
735 |
|
return $this->responseBody; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
713 |
|
public function isError() |
103
|
|
|
{ |
104
|
713 |
|
$statusCode = $this->getStatusCode(); |
105
|
|
|
|
106
|
713 |
|
return (!in_array($statusCode, [200, 201])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $fieldName |
111
|
|
|
* @param mixed $default |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
32 |
|
protected function getResponseField($fieldName, $default = '') |
115
|
|
|
{ |
116
|
32 |
|
$result = $this->toArray(); |
117
|
32 |
|
return isset($result[$fieldName]) ? $result[$fieldName]: $default; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return ErrorContainer |
122
|
|
|
*/ |
123
|
32 |
|
public function getErrors() |
124
|
|
|
{ |
125
|
32 |
|
if (is_null($this->errors)) { |
126
|
32 |
|
$this->errors = ErrorContainer::fromArray($this->getResponseField('errors', []), $this->getContext()); |
127
|
|
|
} |
128
|
32 |
|
return $this->errors; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function getCorrelationId() |
132
|
|
|
{ |
133
|
1 |
|
$correlationId = $this->getHeader(self::X_CORRELATION_ID); |
134
|
1 |
|
return count($correlationId) > 0 ? current($correlationId) : null; |
135
|
|
|
} |
136
|
|
|
|
137
|
719 |
|
public function getStatusCode() |
138
|
|
|
{ |
139
|
719 |
|
return $this->getResponse()->getStatusCode(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $header |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
/** |
147
|
|
|
* @param string $header |
148
|
|
|
* @return string[] |
149
|
|
|
*/ |
150
|
3 |
|
public function getHeader($header) |
151
|
|
|
{ |
152
|
3 |
|
return $this->getResponse()->getHeader($header); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
1 |
|
public function getHeaders() |
159
|
|
|
{ |
160
|
1 |
|
return $this->getResponse()->getHeaders(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return ResponseInterface|AdapterPromiseInterface |
165
|
|
|
*/ |
166
|
727 |
|
public function getResponse() |
167
|
|
|
{ |
168
|
727 |
|
return $this->response; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return ClientRequestInterface |
173
|
|
|
*/ |
174
|
40 |
|
public function getRequest() |
175
|
|
|
{ |
176
|
40 |
|
return $this->request; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the result of the future either from cache or by blocking until |
181
|
|
|
* it is complete. |
182
|
|
|
* |
183
|
|
|
* This method must block until the future has a result or is cancelled. |
184
|
|
|
* Throwing an exception in the wait() method will mark the future as |
185
|
|
|
* realized and will throw the exception each time wait() is called. |
186
|
|
|
* Throwing an instance of GuzzleHttp\Ring\CancelledException will mark |
187
|
|
|
* the future as realized, will not throw immediately, but will throw the |
188
|
|
|
* exception if the future's wait() method is called again. |
189
|
|
|
* |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
3 |
|
public function wait() |
193
|
|
|
{ |
194
|
3 |
|
if (!$this->getResponse() instanceof AdapterPromiseInterface) { |
195
|
1 |
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
196
|
|
|
} |
197
|
|
|
|
198
|
2 |
|
return $this->getResponse()->wait(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param callable $onFulfilled |
203
|
|
|
* @param callable $onRejected |
204
|
|
|
* @return ApiResponseInterface |
205
|
|
|
*/ |
206
|
6 |
|
public function then(callable $onFulfilled = null, callable $onRejected = null) |
207
|
|
|
{ |
208
|
6 |
|
if (!$this->getResponse() instanceof AdapterPromiseInterface) { |
209
|
1 |
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
210
|
|
|
} |
211
|
5 |
|
$this->getResponse()->then($onFulfilled, $onRejected); |
212
|
|
|
|
213
|
5 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritDoc |
218
|
|
|
*/ |
219
|
1 |
|
public function getPromise() |
220
|
|
|
{ |
221
|
1 |
|
$adapterResponse = $this->getResponse(); |
222
|
1 |
|
if ($adapterResponse instanceof PromiseGetInterface) { |
223
|
1 |
|
return $adapterResponse->getPromise(); |
224
|
|
|
} |
225
|
|
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths