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