Passed
Push — master ( da0795...78880b )
by Jens
14:45 queued 18s
created

AbstractApiRequest::setExternalUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 1560
    public function __construct(JsonEndpoint $endpoint, Context $context = null)
54
    {
55 1560
        $this->setContext($context);
56 1560
        $this->setEndpoint($endpoint);
57 1560
    }
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 635
    public function getIdentifier()
80
    {
81 635
        if (is_null($this->identifier)) {
82 634
            $this->identifier = uniqid();
83
        }
84
85 635
        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 1560
    protected function setEndpoint(JsonEndpoint $endpoint)
116
    {
117 1560
        $this->endpoint = $endpoint;
118
119 1560
        return $this;
120
    }
121
122
    /**
123
     * @return JsonEndpoint
124
     * @internal
125
     */
126 909
    public function getEndpoint()
127
    {
128 909
        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 919
    public function addParamObject(ParameterInterface $param)
153
    {
154 919
        $this->params[$param->getId()] = $param;
155
156 919
        return $this;
157
    }
158
159 914
    protected function convertToString($params)
160
    {
161 914
        $params = array_map(
162
            function ($param) {
163 787
                return (string)$param;
164 914
            },
165 914
            $params
166
        );
167 914
        ksort($params);
168 914
        $params = implode('&', $params);
169
170 914
        return $params;
171
    }
172
    /**
173
     * @return string
174
     * @internal
175
     */
176 873
    protected function getParamString()
177
    {
178 873
        $params = $this->convertToString($this->params);
179
180 873
        return (!empty($params) ? '?' . $params : '');
181
    }
182
183
    /**
184
     * @return string
185
     * @internal
186
     */
187 766
    protected function getPath()
188
    {
189 766
        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 401
    public function mapResult(array $result, Context $context = null)
214
    {
215 401
        return $this->map($result, $context);
216
    }
217
218
    /**
219
     * @param ApiResponseInterface $response
220
     * @return JsonDeserializeInterface|null
221
     * @deprecated Use mapFromResponse() instead
222
     */
223 701
    public function mapResponse(ApiResponseInterface $response)
224
    {
225 701
        return $this->mapFromResponse($response);
226
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231 706
    public function mapFromResponse($response, MapperInterface $mapper = null)
232
    {
233 706
        if ($response instanceof ResponseInterface) {
234 3
            $response = $this->buildResponse($response);
235
        }
236 706
        if ($response->isError()) {
237 9
            return null;
238
        }
239 702
        $result = $response->toArray();
240 702
        if ($response instanceof ContextAwareInterface) {
241 702
            return $this->map($result, $response->getContext(), $mapper);
242
        }
243
244
        return $this->map($result, $this->getContext(), $mapper);
245
    }
246
247 1039
    public function map(array $data, Context $context = null, MapperInterface $mapper = null)
248
    {
249 1039
        if (!empty($data)) {
250 871
            if (is_null($mapper)) {
251 871
                $mapper = JsonObjectMapper::of($context);
252
            }
253 871
            return $mapper->map($data, $this->resultClass);
254
        }
255
256 168
        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 709
    public function executeWithClient(Client $client, array $headers = null)
266
    {
267 709
        if (!is_null($this->externalUserId) && !isset($headers[self::EXTERNAL_USER_HEADER])) {
268 1
            $headers[self::EXTERNAL_USER_HEADER] = $this->externalUserId;
269
        }
270 709
        return $client->execute($this, $headers);
271
    }
272
}
273