Completed
Push — develop ( 94ac5e...9b8817 )
by Jens
14:35
created

AbstractApiRequest::mapResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 907
    public function __construct(JsonEndpoint $endpoint, Context $context = null)
50
    {
51 907
        $this->setContext($context);
52 907
        $this->setEndpoint($endpoint);
53 907
    }
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 346
    public function getIdentifier()
68
    {
69 346
        if (is_null($this->identifier)) {
70 345
            $this->identifier = uniqid();
71
        }
72
73 346
        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 907
    protected function setEndpoint(JsonEndpoint $endpoint)
93
    {
94 907
        $this->endpoint = $endpoint;
95
96 907
        return $this;
97
    }
98
99
    /**
100
     * @return JsonEndpoint
101
     * @internal
102
     */
103 574
    public function getEndpoint()
104
    {
105 574
        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 547
    public function addParamObject(ParameterInterface $param)
130
    {
131 547
        $this->params[$param->getId()] = $param;
132
133 547
        return $this;
134
    }
135
136 573
    protected function convertToString($params)
137
    {
138 573
        $params = array_map(
139 573
            function ($param) {
140 479
                return (string)$param;
141 573
            },
142
            $params
143
        );
144 573
        sort($params);
145 573
        $params = implode('&', $params);
146
147 573
        return $params;
148
    }
149
    /**
150
     * @return string
151
     * @internal
152
     */
153 548
    protected function getParamString()
154
    {
155 548
        $params = $this->convertToString($this->params);
156
157 548
        return (!empty($params) ? '?' . $params : '');
158
    }
159
160
    /**
161
     * @return string
162
     * @internal
163
     */
164 473
    protected function getPath()
165
    {
166 473
        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 410
    public function mapResponse(ApiResponseInterface $response)
199
    {
200 410
        return $this->mapFromResponse($response);
201
    }
202
203 412
    public function mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
204
    {
205 412
        if ($response->isError()) {
206 4
            return null;
207
        }
208 410
        $result = $response->toArray();
209 410
        if ($response instanceof ContextAwareInterface) {
210 410
            return $this->map($result, $response->getContext(), $mapper);
211
        }
212
213
        return $this->map($result, $this->getContext(), $mapper);
214
    }
215
216 609
    public function map(array $data, Context $context = null, MapperInterface $mapper = null)
217
    {
218 609
        if (!empty($data)) {
219 511
            if (is_null($mapper)) {
220 511
                $mapper = JsonObjectMapper::of($context);
221
            }
222 511
            return $mapper->map($data, $this->resultClass);
223
        }
224
225 98
        return null;
226
    }
227
228
    /**
229
     * @param Client $client
230
     * @return ApiResponseInterface
231
     */
232 417
    public function executeWithClient(Client $client)
233
    {
234 417
        return $client->execute($this);
235
    }
236
}
237