|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
* @created: 26.01.15, 14:44 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Response; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Error\ErrorContainer; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Commercetools\Core\Client\Adapter\AdapterPromiseInterface; |
|
12
|
|
|
use Commercetools\Core\Error\Message; |
|
13
|
|
|
use Commercetools\Core\Model\Common\Context; |
|
14
|
|
|
use Commercetools\Core\Model\Common\ContextAwareInterface; |
|
15
|
|
|
use Commercetools\Core\Model\Common\ContextTrait; |
|
16
|
|
|
use Commercetools\Core\Request\ClientRequestInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @package Commercetools\Core\Response |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractApiResponse implements ApiResponseInterface, ContextAwareInterface |
|
22
|
|
|
{ |
|
23
|
|
|
const X_CORRELATION_ID = 'X-Correlation-ID'; |
|
24
|
|
|
use ContextTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ResponseInterface|AdapterPromiseInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $response; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ClientRequestInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $request; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $jsonData; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $responseBody; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ErrorContainer |
|
48
|
|
|
*/ |
|
49
|
|
|
private $errors; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param ResponseInterface $response |
|
53
|
|
|
* @param ClientRequestInterface $request |
|
54
|
|
|
* @param Context $context |
|
55
|
|
|
*/ |
|
56
|
334 |
|
public function __construct(ResponseInterface $response, ClientRequestInterface $request, Context $context = null) |
|
57
|
|
|
{ |
|
58
|
334 |
|
$this->setContext($context); |
|
59
|
334 |
|
$this->response = $response; |
|
60
|
334 |
|
$this->request = $request; |
|
61
|
334 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return mixed|null |
|
65
|
|
|
*/ |
|
66
|
75 |
|
public function toObject() |
|
67
|
|
|
{ |
|
68
|
75 |
|
if (!$this->isError()) { |
|
69
|
74 |
|
return $this->getRequest()->mapResponse($this); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
284 |
|
public function toArray() |
|
79
|
|
|
{ |
|
80
|
284 |
|
if (is_null($this->jsonData)) { |
|
81
|
284 |
|
$this->jsonData = json_decode($this->getBody(), true); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
284 |
|
return $this->jsonData; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
287 |
|
public function getBody() |
|
90
|
|
|
{ |
|
91
|
287 |
|
if (is_null($this->responseBody)) { |
|
92
|
287 |
|
$this->responseBody = (string)$this->response->getBody(); |
|
93
|
|
|
} |
|
94
|
287 |
|
return $this->responseBody; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
266 |
|
public function isError() |
|
101
|
|
|
{ |
|
102
|
266 |
|
$statusCode = $this->getStatusCode(); |
|
103
|
|
|
|
|
104
|
266 |
|
return (!in_array($statusCode, [200, 201])); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
15 |
|
protected function getResponseField($fieldName, $default = '') |
|
108
|
|
|
{ |
|
109
|
15 |
|
$result = $this->toArray(); |
|
110
|
15 |
|
return isset($result[$fieldName]) ? $result[$fieldName]: $default; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
15 |
|
public function getErrors() |
|
117
|
|
|
{ |
|
118
|
15 |
|
if (is_null($this->errors)) { |
|
119
|
15 |
|
$this->errors = ErrorContainer::fromArray($this->getResponseField('errors', []), $this->getContext()); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
15 |
|
return $this->errors; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
public function getCorrelationId() |
|
125
|
|
|
{ |
|
126
|
1 |
|
return current($this->getHeader(self::X_CORRELATION_ID)); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
268 |
|
public function getStatusCode() |
|
130
|
|
|
{ |
|
131
|
268 |
|
return $this->getResponse()->getStatusCode(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $header |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $header |
|
140
|
|
|
* @return string[] |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function getHeader($header) |
|
143
|
|
|
{ |
|
144
|
2 |
|
return $this->getResponse()->getHeader($header); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function getHeaders() |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->getResponse()->getHeaders(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return ResponseInterface|AdapterPromiseInterface |
|
157
|
|
|
*/ |
|
158
|
275 |
|
public function getResponse() |
|
159
|
|
|
{ |
|
160
|
275 |
|
return $this->response; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return ClientRequestInterface |
|
165
|
|
|
*/ |
|
166
|
75 |
|
public function getRequest() |
|
167
|
|
|
{ |
|
168
|
75 |
|
return $this->request; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns the result of the future either from cache or by blocking until |
|
173
|
|
|
* it is complete. |
|
174
|
|
|
* |
|
175
|
|
|
* This method must block until the future has a result or is cancelled. |
|
176
|
|
|
* Throwing an exception in the wait() method will mark the future as |
|
177
|
|
|
* realized and will throw the exception each time wait() is called. |
|
178
|
|
|
* Throwing an instance of GuzzleHttp\Ring\CancelledException will mark |
|
179
|
|
|
* the future as realized, will not throw immediately, but will throw the |
|
180
|
|
|
* exception if the future's wait() method is called again. |
|
181
|
|
|
* |
|
182
|
|
|
* @return mixed |
|
183
|
|
|
*/ |
|
184
|
2 |
|
public function wait() |
|
185
|
|
|
{ |
|
186
|
2 |
|
if (!$this->getResponse() instanceof AdapterPromiseInterface) { |
|
187
|
1 |
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
return $this->getResponse()->wait(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param callable $onFulfilled |
|
195
|
|
|
* @param callable $onRejected |
|
196
|
|
|
* @return ApiResponseInterface |
|
197
|
|
|
*/ |
|
198
|
4 |
|
public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
199
|
|
|
{ |
|
200
|
4 |
|
if (!$this->getResponse() instanceof AdapterPromiseInterface) { |
|
201
|
1 |
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
|
202
|
|
|
} |
|
203
|
3 |
|
$this->getResponse()->then($onFulfilled, $onRejected); |
|
204
|
|
|
|
|
205
|
3 |
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..