1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[email protected]> |
4
|
|
|
* @created: 26.01.15, 11:00 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Request; |
8
|
|
|
|
9
|
|
|
use Commercetools\Core\Model\Common\JsonObject; |
10
|
|
|
use Commercetools\Core\Model\JsonObjectMapper; |
11
|
|
|
use Commercetools\Core\Model\MapperInterface; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Commercetools\Core\Client; |
15
|
|
|
use Commercetools\Core\Client\JsonEndpoint; |
16
|
|
|
use Commercetools\Core\Model\Common\Context; |
17
|
|
|
use Commercetools\Core\Model\Common\ContextAwareInterface; |
18
|
|
|
use Commercetools\Core\Model\Common\ContextTrait; |
19
|
|
|
use Commercetools\Core\Model\Common\JsonDeserializeInterface; |
20
|
|
|
use Commercetools\Core\Request\Query\MultiParameter; |
21
|
|
|
use Commercetools\Core\Request\Query\Parameter; |
22
|
|
|
use Commercetools\Core\Request\Query\ParameterInterface; |
23
|
|
|
use Commercetools\Core\Response\ApiResponseInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Commercetools\Core\Request |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractApiRequest implements ClientRequestInterface, ContextAwareInterface |
29
|
|
|
{ |
30
|
|
|
const EXTERNAL_USER_HEADER = 'X-External-User-ID'; |
31
|
|
|
use ContextTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var JsonEndpoint |
35
|
|
|
*/ |
36
|
|
|
protected $endpoint; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $params = []; |
42
|
|
|
|
43
|
|
|
protected $identifier; |
44
|
|
|
|
45
|
|
|
protected $resultClass = JsonObject::class; |
46
|
|
|
|
47
|
|
|
protected $externalUserId = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param JsonEndpoint $endpoint |
51
|
|
|
* @param Context $context |
52
|
|
|
*/ |
53
|
1501 |
|
public function __construct(JsonEndpoint $endpoint, Context $context = null) |
54
|
|
|
{ |
55
|
1501 |
|
$this->setContext($context); |
56
|
1501 |
|
$this->setEndpoint($endpoint); |
57
|
1501 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
9 |
|
public function getParamCount() |
63
|
|
|
{ |
64
|
9 |
|
return count($this->params); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
* @internal |
70
|
|
|
*/ |
71
|
|
|
public function getResultClass() |
72
|
|
|
{ |
73
|
|
|
return $this->resultClass; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
607 |
|
public function getIdentifier() |
80
|
|
|
{ |
81
|
607 |
|
if (is_null($this->identifier)) { |
82
|
606 |
|
$this->identifier = uniqid(); |
83
|
|
|
} |
84
|
|
|
|
85
|
607 |
|
return $this->identifier; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $identifier |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
1 |
|
public function setIdentifier($identifier) |
93
|
|
|
{ |
94
|
1 |
|
$this->identifier = $identifier; |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string|null $externalUserId |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
1 |
|
public function setExternalUserId($externalUserId) |
104
|
|
|
{ |
105
|
1 |
|
$this->externalUserId = $externalUserId; |
106
|
|
|
|
107
|
1 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param JsonEndpoint $endpoint |
112
|
|
|
* @return $this |
113
|
|
|
* @internal |
114
|
|
|
*/ |
115
|
1501 |
|
protected function setEndpoint(JsonEndpoint $endpoint) |
116
|
|
|
{ |
117
|
1501 |
|
$this->endpoint = $endpoint; |
118
|
|
|
|
119
|
1501 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return JsonEndpoint |
124
|
|
|
* @internal |
125
|
|
|
*/ |
126
|
879 |
|
public function getEndpoint() |
127
|
|
|
{ |
128
|
879 |
|
return $this->endpoint; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $key |
133
|
|
|
* @param $value |
134
|
|
|
* @param bool $replace |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
36 |
|
public function addParam($key, $value = null, $replace = true) |
138
|
|
|
{ |
139
|
36 |
|
if ($replace) { |
140
|
24 |
|
$param = new Parameter($key, $value); |
141
|
|
|
} else { |
142
|
13 |
|
$param = new MultiParameter($key, $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
35 |
|
return $this->addParamObject($param); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param ParameterInterface $param |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
916 |
|
public function addParamObject(ParameterInterface $param) |
153
|
|
|
{ |
154
|
916 |
|
$this->params[$param->getId()] = $param; |
155
|
|
|
|
156
|
916 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
884 |
|
protected function convertToString($params) |
160
|
|
|
{ |
161
|
884 |
|
$params = array_map( |
162
|
884 |
|
function ($param) { |
163
|
790 |
|
return (string)$param; |
164
|
884 |
|
}, |
165
|
884 |
|
$params |
166
|
|
|
); |
167
|
884 |
|
ksort($params); |
168
|
884 |
|
$params = implode('&', $params); |
169
|
|
|
|
170
|
884 |
|
return $params; |
171
|
|
|
} |
172
|
|
|
/** |
173
|
|
|
* @return string |
174
|
|
|
* @internal |
175
|
|
|
*/ |
176
|
843 |
|
protected function getParamString() |
177
|
|
|
{ |
178
|
843 |
|
$params = $this->convertToString($this->params); |
179
|
|
|
|
180
|
843 |
|
return (!empty($params) ? '?' . $params : ''); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
* @internal |
186
|
|
|
*/ |
187
|
746 |
|
protected function getPath() |
188
|
|
|
{ |
189
|
746 |
|
return (string)$this->getEndpoint() . $this->getParamString(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param ResponseInterface $response |
194
|
|
|
* @return ApiResponseInterface |
195
|
|
|
* @internal |
196
|
|
|
*/ |
197
|
|
|
abstract public function buildResponse(ResponseInterface $response); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return RequestInterface |
201
|
|
|
* @internal |
202
|
|
|
*/ |
203
|
|
|
abstract public function httpRequest(); |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param array $result |
208
|
|
|
* @param Context $context |
209
|
|
|
* @return JsonDeserializeInterface|null |
210
|
|
|
* @internal |
211
|
|
|
*/ |
212
|
383 |
|
public function mapResult(array $result, Context $context = null) |
213
|
|
|
{ |
214
|
383 |
|
return $this->map($result, $context); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param ApiResponseInterface $response |
219
|
|
|
* @return JsonDeserializeInterface|null |
220
|
|
|
*/ |
221
|
688 |
|
public function mapResponse(ApiResponseInterface $response) |
222
|
|
|
{ |
223
|
688 |
|
return $this->mapFromResponse($response); |
224
|
|
|
} |
225
|
|
|
|
226
|
690 |
|
public function mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null) |
227
|
|
|
{ |
228
|
690 |
|
if ($response->isError()) { |
229
|
6 |
|
return null; |
230
|
|
|
} |
231
|
690 |
|
$result = $response->toArray(); |
232
|
690 |
|
if ($response instanceof ContextAwareInterface) { |
233
|
690 |
|
return $this->map($result, $response->getContext(), $mapper); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this->map($result, $this->getContext(), $mapper); |
237
|
|
|
} |
238
|
|
|
|
239
|
1013 |
|
public function map(array $data, Context $context = null, MapperInterface $mapper = null) |
240
|
|
|
{ |
241
|
1013 |
|
if (!empty($data)) { |
242
|
853 |
|
if (is_null($mapper)) { |
243
|
853 |
|
$mapper = JsonObjectMapper::of($context); |
244
|
|
|
} |
245
|
853 |
|
return $mapper->map($data, $this->resultClass); |
246
|
|
|
} |
247
|
|
|
|
248
|
160 |
|
return null; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param Client $client |
253
|
|
|
* @param array|null $headers |
254
|
|
|
* @return ApiResponseInterface |
255
|
|
|
*/ |
256
|
688 |
|
public function executeWithClient(Client $client, array $headers = null) |
257
|
|
|
{ |
258
|
688 |
|
if (!is_null($this->externalUserId) && !isset($headers[self::EXTERNAL_USER_HEADER])) { |
259
|
1 |
|
$headers[self::EXTERNAL_USER_HEADER] = $this->externalUserId; |
260
|
|
|
} |
261
|
688 |
|
return $client->execute($this, $headers); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|