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
|
|
|
use ContextTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ResponseInterface|AdapterPromiseInterface |
27
|
|
|
*/ |
28
|
|
|
protected $response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ClientRequestInterface |
32
|
|
|
*/ |
33
|
|
|
protected $request; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $jsonData; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $responseBody; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ErrorContainer |
47
|
|
|
*/ |
48
|
|
|
private $errors; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ResponseInterface $response |
52
|
|
|
* @param ClientRequestInterface $request |
53
|
|
|
* @param Context $context |
54
|
|
|
*/ |
55
|
326 |
|
public function __construct(ResponseInterface $response, ClientRequestInterface $request, Context $context = null) |
56
|
|
|
{ |
57
|
326 |
|
$this->setContext($context); |
58
|
326 |
|
$this->response = $response; |
59
|
326 |
|
$this->request = $request; |
60
|
326 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed|null |
64
|
|
|
*/ |
65
|
75 |
|
public function toObject() |
66
|
|
|
{ |
67
|
75 |
|
if (!$this->isError()) { |
68
|
74 |
|
return $this->getRequest()->mapResponse($this); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
280 |
|
public function toArray() |
78
|
|
|
{ |
79
|
280 |
|
if (is_null($this->jsonData)) { |
80
|
280 |
|
$this->jsonData = json_decode($this->getBody(), true); |
|
|
|
|
81
|
|
|
} |
82
|
280 |
|
return $this->jsonData; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
283 |
|
public function getBody() |
89
|
|
|
{ |
90
|
283 |
|
if (is_null($this->responseBody)) { |
91
|
283 |
|
$this->responseBody = (string)$this->response->getBody(); |
92
|
|
|
} |
93
|
283 |
|
return $this->responseBody; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
261 |
|
public function isError() |
100
|
|
|
{ |
101
|
261 |
|
$statusCode = $this->getStatusCode(); |
102
|
|
|
|
103
|
261 |
|
return (!in_array($statusCode, [200, 201])); |
104
|
|
|
} |
105
|
|
|
|
106
|
13 |
|
protected function getResponseField($fieldName, $default = '') |
107
|
|
|
{ |
108
|
13 |
|
$result = $this->toArray(); |
109
|
13 |
|
return isset($result[$fieldName]) ? $result[$fieldName]: $default; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
13 |
|
public function getErrors() |
116
|
|
|
{ |
117
|
13 |
|
if (is_null($this->errors)) { |
118
|
13 |
|
$this->errors = ErrorContainer::fromArray($this->getResponseField('errors', []), $this->getContext()); |
|
|
|
|
119
|
|
|
} |
120
|
13 |
|
return $this->errors; |
121
|
|
|
} |
122
|
|
|
|
123
|
263 |
|
public function getStatusCode() |
124
|
|
|
{ |
125
|
263 |
|
return $this->getResponse()->getStatusCode(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $header |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
/** |
133
|
|
|
* @param string $header |
134
|
|
|
* @return string[] |
135
|
|
|
*/ |
136
|
1 |
|
public function getHeader($header) |
137
|
|
|
{ |
138
|
1 |
|
return $this->getResponse()->getHeader($header); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
1 |
|
public function getHeaders() |
145
|
|
|
{ |
146
|
1 |
|
return $this->getResponse()->getHeaders(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return ResponseInterface|AdapterPromiseInterface |
151
|
|
|
*/ |
152
|
269 |
|
public function getResponse() |
153
|
|
|
{ |
154
|
269 |
|
return $this->response; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return ClientRequestInterface |
159
|
|
|
*/ |
160
|
75 |
|
public function getRequest() |
161
|
|
|
{ |
162
|
75 |
|
return $this->request; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns the result of the future either from cache or by blocking until |
167
|
|
|
* it is complete. |
168
|
|
|
* |
169
|
|
|
* This method must block until the future has a result or is cancelled. |
170
|
|
|
* Throwing an exception in the wait() method will mark the future as |
171
|
|
|
* realized and will throw the exception each time wait() is called. |
172
|
|
|
* Throwing an instance of GuzzleHttp\Ring\CancelledException will mark |
173
|
|
|
* the future as realized, will not throw immediately, but will throw the |
174
|
|
|
* exception if the future's wait() method is called again. |
175
|
|
|
* |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
2 |
|
public function wait() |
179
|
|
|
{ |
180
|
2 |
|
if (!$this->getResponse() instanceof AdapterPromiseInterface) { |
181
|
1 |
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
return $this->getResponse()->wait(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param callable $onFulfilled |
189
|
|
|
* @param callable $onRejected |
190
|
|
|
* @return ApiResponseInterface |
191
|
|
|
*/ |
192
|
4 |
|
public function then(callable $onFulfilled = null, callable $onRejected = null) |
193
|
|
|
{ |
194
|
4 |
|
if (!$this->getResponse() instanceof AdapterPromiseInterface) { |
195
|
1 |
|
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL); |
196
|
|
|
} |
197
|
3 |
|
$this->getResponse()->then($onFulfilled, $onRejected); |
198
|
|
|
|
199
|
3 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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..