1 | <?php |
||
25 | abstract class AbstractApiRequest implements ClientRequestInterface, ContextAwareInterface |
||
26 | { |
||
27 | use ContextTrait; |
||
28 | |||
29 | /** |
||
30 | * @var JsonEndpoint |
||
31 | */ |
||
32 | protected $endpoint; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $params = []; |
||
38 | |||
39 | protected $identifier; |
||
40 | |||
41 | protected $resultClass = '\Commercetools\Core\Model\Common\JsonObject'; |
||
42 | |||
43 | /** |
||
44 | * @param JsonEndpoint $endpoint |
||
45 | * @param Context $context |
||
46 | */ |
||
47 | 738 | public function __construct(JsonEndpoint $endpoint, Context $context = null) |
|
48 | { |
||
49 | 738 | $this->setContext($context); |
|
50 | 738 | $this->setEndpoint($endpoint); |
|
51 | 738 | } |
|
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | * @internal |
||
56 | */ |
||
57 | public function getResultClass() |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | 202 | public function getIdentifier() |
|
66 | { |
||
67 | 202 | if (is_null($this->identifier)) { |
|
68 | 201 | $this->identifier = uniqid(); |
|
69 | } |
||
70 | |||
71 | 202 | return $this->identifier; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $identifier |
||
76 | * @return $this |
||
77 | */ |
||
78 | 1 | public function setIdentifier($identifier) |
|
79 | { |
||
80 | 1 | $this->identifier = $identifier; |
|
81 | |||
82 | 1 | return $this; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param JsonEndpoint $endpoint |
||
87 | * @return $this |
||
88 | * @internal |
||
89 | */ |
||
90 | 738 | protected function setEndpoint(JsonEndpoint $endpoint) |
|
91 | { |
||
92 | 738 | $this->endpoint = $endpoint; |
|
93 | |||
94 | 738 | return $this; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return JsonEndpoint |
||
99 | * @internal |
||
100 | */ |
||
101 | 407 | public function getEndpoint() |
|
105 | |||
106 | /** |
||
107 | * @param $key |
||
108 | * @param $value |
||
109 | * @param bool $replace |
||
110 | * @return $this |
||
111 | */ |
||
112 | 41 | public function addParam($key, $value = null, $replace = true) |
|
113 | { |
||
114 | 41 | if ($replace) { |
|
115 | 29 | $param = new Parameter($key, $value); |
|
116 | } else { |
||
117 | 13 | $param = new MultiParameter($key, $value); |
|
118 | } |
||
119 | |||
120 | 40 | return $this->addParamObject($param); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param ParameterInterface $param |
||
125 | * @return $this |
||
126 | */ |
||
127 | 383 | public function addParamObject(ParameterInterface $param) |
|
133 | |||
134 | 370 | protected function convertToString($params) |
|
135 | { |
||
136 | 370 | $params = array_map( |
|
137 | 370 | function ($param) { |
|
138 | 315 | return (string)$param; |
|
139 | 370 | }, |
|
140 | $params |
||
141 | ); |
||
142 | 370 | sort($params); |
|
143 | 370 | $params = implode('&', $params); |
|
144 | |||
145 | 370 | return $params; |
|
146 | } |
||
147 | /** |
||
148 | * @return string |
||
149 | * @internal |
||
150 | */ |
||
151 | 355 | protected function getParamString() |
|
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | * @internal |
||
161 | */ |
||
162 | 317 | protected function getPath() |
|
166 | |||
167 | /** |
||
168 | * @param ResponseInterface $response |
||
169 | * @return ApiResponseInterface |
||
170 | * @internal |
||
171 | */ |
||
172 | abstract public function buildResponse(ResponseInterface $response); |
||
173 | |||
174 | /** |
||
175 | * @return RequestInterface |
||
176 | * @internal |
||
177 | */ |
||
178 | abstract public function httpRequest(); |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @param array $result |
||
183 | * @param Context $context |
||
184 | * @return JsonDeserializeInterface|null |
||
185 | * @internal |
||
186 | */ |
||
187 | 456 | public function mapResult(array $result, Context $context = null) |
|
195 | |||
196 | /** |
||
197 | * @param ApiResponseInterface $response |
||
198 | * @return JsonDeserializeInterface|null |
||
199 | */ |
||
200 | 261 | public function mapResponse(ApiResponseInterface $response) |
|
201 | { |
||
202 | 261 | if ($response->isError()) { |
|
203 | 3 | return null; |
|
204 | } |
||
205 | 259 | $result = $response->toArray(); |
|
206 | 259 | if ($response instanceof ContextAwareInterface) { |
|
207 | 259 | return $this->mapResult($result, $response->getContext()); |
|
208 | } |
||
209 | |||
210 | return $this->mapResult($result, $this->getContext()); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param Client $client |
||
215 | * @return ApiResponseInterface |
||
216 | */ |
||
217 | 265 | public function executeWithClient(Client $client) |
|
221 | } |
||
222 |