Passed
Push — master ( 3f8301...c7321f )
by Jens
42:49 queued 17:59
created

AbstractApiRequest::mapResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
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 1617
    public function __construct(JsonEndpoint $endpoint, Context $context = null)
54
    {
55 1617
        $this->setContext($context);
56 1617
        $this->setEndpoint($endpoint);
57 1617
    }
58
59
    /**
60
     * @return int
61
     */
62 3
    public function getParamCount()
63
    {
64 3
        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 186
    public function getIdentifier()
80
    {
81 186
        if (is_null($this->identifier)) {
82 185
            $this->identifier = uniqid();
83
        }
84
85 186
        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
    public function setExternalUserId($externalUserId)
104
    {
105
        $this->externalUserId = $externalUserId;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param JsonEndpoint $endpoint
112
     * @return $this
113
     * @internal
114
     */
115 1617
    protected function setEndpoint(JsonEndpoint $endpoint)
116
    {
117 1617
        $this->endpoint = $endpoint;
118
119 1617
        return $this;
120
    }
121
122
    /**
123
     * @return JsonEndpoint
124
     * @internal
125
     */
126 959
    public function getEndpoint()
127
    {
128 959
        return $this->endpoint;
129
    }
130
131
    /**
132
     * @param $key
133
     * @param $value
134
     * @param bool $replace
135
     * @return $this
136
     */
137 89
    public function addParam($key, $value = null, $replace = true)
138
    {
139 89
        if ($replace) {
140 77
            $param = new Parameter($key, $value);
141
        } else {
142 13
            $param = new MultiParameter($key, $value);
143
        }
144
145 88
        return $this->addParamObject($param);
146
    }
147
148
    /**
149
     * @param ParameterInterface $param
150
     * @return $this
151
     */
152 971
    public function addParamObject(ParameterInterface $param)
153
    {
154 971
        $this->params[$param->getId()] = $param;
155
156 971
        return $this;
157
    }
158
159 964
    protected function convertToString($params)
160
    {
161 964
        $params = array_map(
162
            function ($param) {
163 833
                return (string)$param;
164 964
            },
165
            $params
166
        );
167 964
        ksort($params);
168 964
        $params = implode('&', $params);
169
170 964
        return $params;
171
    }
172
    /**
173
     * @return string
174
     * @internal
175
     */
176 923
    protected function getParamString()
177
    {
178 923
        $params = $this->convertToString($this->params);
179
180 923
        return (!empty($params) ? '?' . $params : '');
181
    }
182
183
    /**
184
     * @return string
185
     * @internal
186
     */
187 806
    protected function getPath()
188
    {
189 806
        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
     * @deprecated Use map() instead
212
     */
213 407
    public function mapResult(array $result, Context $context = null)
214
    {
215 407
        return $this->map($result, $context);
216
    }
217
218
    /**
219
     * @param ApiResponseInterface $response
220
     * @return JsonDeserializeInterface|null
221
     * @deprecated Use mapFromResponse() instead
222
     */
223 203
    public function mapResponse(ApiResponseInterface $response)
224
    {
225 203
        return $this->mapFromResponse($response);
226
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231 740
    public function mapFromResponse($response, MapperInterface $mapper = null)
232
    {
233 740
        if ($response instanceof ResponseInterface) {
234 550
            $response = $this->buildResponse($response);
235
        }
236 740
        if ($response->isError()) {
237 7
            return null;
238
        }
239 739
        $result = $response->toArray();
240 739
        if ($response instanceof ContextAwareInterface) {
241 739
            return $this->map($result, $response->getContext(), $mapper);
242
        }
243
244
        return $this->map($result, $this->getContext(), $mapper);
245
    }
246
247 1074
    public function map(array $data, Context $context = null, MapperInterface $mapper = null)
248
    {
249 1074
        if (!empty($data)) {
250 905
            if (is_null($mapper)) {
251 905
                $mapper = JsonObjectMapper::of($context);
252
            }
253 905
            return $mapper->map($data, $this->resultClass);
254
        }
255
256 169
        return null;
257
    }
258
259
    /**
260
     * @param Client $client
261
     * @param array|null $headers
262
     * @return ApiResponseInterface
263
     * @throws \Commercetools\Core\Error\ApiException
264
     */
265 203
    public function executeWithClient(Client $client, array $headers = null)
266
    {
267 203
        if (!is_null($this->externalUserId) && !isset($headers[self::EXTERNAL_USER_HEADER])) {
268
            $headers[self::EXTERNAL_USER_HEADER] = $this->externalUserId;
269
        }
270 203
        return $client->execute($this, $headers);
271
    }
272
}
273