Completed
Push — master ( 2d0c9c...1268a4 )
by Jens
10:44
created

AbstractApiRequest::addParamObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
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 Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Commercetools\Core\Client;
12
use Commercetools\Core\Client\JsonEndpoint;
13
use Commercetools\Core\Model\Common\Context;
14
use Commercetools\Core\Model\Common\ContextAwareInterface;
15
use Commercetools\Core\Model\Common\ContextTrait;
16
use Commercetools\Core\Model\Common\JsonDeserializeInterface;
17
use Commercetools\Core\Request\Query\MultiParameter;
18
use Commercetools\Core\Request\Query\Parameter;
19
use Commercetools\Core\Request\Query\ParameterInterface;
20
use Commercetools\Core\Response\ApiResponseInterface;
21
22
/**
23
 * @package Commercetools\Core\Request
24
 */
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()
58
    {
59
        return $this->resultClass;
60
    }
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()
102
    {
103 407
        return $this->endpoint;
104
    }
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)
128
    {
129 383
        $this->params[$param->getId()] = $param;
130
131 383
        return $this;
132
    }
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()
152
    {
153 355
        $params = $this->convertToString($this->params);
154
155 355
        return (!empty($params) ? '?' . $params : '');
156
    }
157
158
    /**
159
     * @return string
160
     * @internal
161
     */
162 317
    protected function getPath()
163
    {
164 317
        return (string)$this->getEndpoint() . $this->getParamString();
165
    }
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)
188
    {
189 456
        if (!empty($result)) {
190 358
            $object = forward_static_call_array([$this->resultClass, 'fromArray'], [$result, $context]);
191 358
            return $object;
192
        }
193 98
        return null;
194
    }
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)
218
    {
219 265
        return $client->execute($this);
220
    }
221
}
222