Passed
Push — develop ( 2d1f6c...f22bd6 )
by Jens
09:26
created

AbstractApiRequest::addParamObject()   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
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
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
    use ContextTrait;
31
32
    /**
33
     * @var JsonEndpoint
34
     */
35
    protected $endpoint;
36
37
    /**
38
     * @var array
39
     */
40
    protected $params = [];
41
42
    protected $identifier;
43
44
    protected $resultClass = JsonObject::class;
45
46
    /**
47
     * @param JsonEndpoint $endpoint
48
     * @param Context $context
49
     */
50 1047
    public function __construct(JsonEndpoint $endpoint, Context $context = null)
51
    {
52 1047
        $this->setContext($context);
53 1047
        $this->setEndpoint($endpoint);
54 1047
    }
55
56
    /**
57
     * @return int
58
     */
59 9
    public function getParamCount()
60
    {
61 9
        return count($this->params);
62
    }
63
64
    /**
65
     * @return string
66
     * @internal
67
     */
68
    public function getResultClass()
69
    {
70
        return $this->resultClass;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 475
    public function getIdentifier()
77
    {
78 475
        if (is_null($this->identifier)) {
79 474
            $this->identifier = uniqid();
80
        }
81
82 475
        return $this->identifier;
83
    }
84
85
    /**
86
     * @param string $identifier
87
     * @return $this
88
     */
89 1
    public function setIdentifier($identifier)
90
    {
91 1
        $this->identifier = $identifier;
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * @param JsonEndpoint $endpoint
98
     * @return $this
99
     * @internal
100
     */
101 1047
    protected function setEndpoint(JsonEndpoint $endpoint)
102
    {
103 1047
        $this->endpoint = $endpoint;
104
105 1047
        return $this;
106
    }
107
108
    /**
109
     * @return JsonEndpoint
110
     * @internal
111
     */
112 713
    public function getEndpoint()
113
    {
114 713
        return $this->endpoint;
115
    }
116
117
    /**
118
     * @param $key
119
     * @param $value
120
     * @param bool $replace
121
     * @return $this
122
     */
123 37
    public function addParam($key, $value = null, $replace = true)
124
    {
125 37
        if ($replace) {
126 24
            $param = new Parameter($key, $value);
127
        } else {
128 14
            $param = new MultiParameter($key, $value);
129
        }
130
131 36
        return $this->addParamObject($param);
132
    }
133
134
    /**
135
     * @param ParameterInterface $param
136
     * @return $this
137
     */
138 675
    public function addParamObject(ParameterInterface $param)
139
    {
140 675
        $this->params[$param->getId()] = $param;
141
142 675
        return $this;
143
    }
144
145 718
    protected function convertToString($params)
146
    {
147 718
        $params = array_map(
148 718
            function ($param) {
149 611
                return (string)$param;
150 718
            },
151 718
            $params
152
        );
153 718
        ksort($params);
154 718
        $params = implode('&', $params);
155
156 718
        return $params;
157
    }
158
    /**
159
     * @return string
160
     * @internal
161
     */
162 677
    protected function getParamString()
163
    {
164 677
        $params = $this->convertToString($this->params);
165
166 677
        return (!empty($params) ? '?' . $params : '');
167
    }
168
169
    /**
170
     * @return string
171
     * @internal
172
     */
173 597
    protected function getPath()
174
    {
175 597
        return (string)$this->getEndpoint() . $this->getParamString();
176
    }
177
178
    /**
179
     * @param ResponseInterface $response
180
     * @return ApiResponseInterface
181
     * @internal
182
     */
183
    abstract public function buildResponse(ResponseInterface $response);
184
185
    /**
186
     * @return RequestInterface
187
     * @internal
188
     */
189
    abstract public function httpRequest();
190
191
192
    /**
193
     * @param array $result
194
     * @param Context $context
195
     * @return JsonDeserializeInterface|null
196
     * @internal
197
     */
198 251
    public function mapResult(array $result, Context $context = null)
199
    {
200 251
        return $this->map($result, $context);
201
    }
202
203
    /**
204
     * @param ApiResponseInterface $response
205
     * @return JsonDeserializeInterface|null
206
     */
207 536
    public function mapResponse(ApiResponseInterface $response)
208
    {
209 536
        return $this->mapFromResponse($response);
210
    }
211
212 538
    public function mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
213
    {
214 538
        if ($response->isError()) {
215 5
            return null;
216
        }
217 535
        $result = $response->toArray();
218 535
        if ($response instanceof ContextAwareInterface) {
219 535
            return $this->map($result, $response->getContext(), $mapper);
220
        }
221
222
        return $this->map($result, $this->getContext(), $mapper);
223
    }
224
225 734
    public function map(array $data, Context $context = null, MapperInterface $mapper = null)
226
    {
227 734
        if (!empty($data)) {
228 636
            if (is_null($mapper)) {
229 636
                $mapper = JsonObjectMapper::of($context);
230
            }
231 636
            return $mapper->map($data, $this->resultClass);
232
        }
233
234 98
        return null;
235
    }
236
237
    /**
238
     * @param Client $client
239
     * @param array $headers
240
     * @return ApiResponseInterface
241
     */
242 543
    public function executeWithClient(Client $client, array $headers = null)
243
    {
244 543
        return $client->execute($this, $headers);
245
    }
246
}
247