Request   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
eloc 70
c 1
b 0
f 0
dl 0
loc 301
rs 9.68

27 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequestEntity() 0 5 1
A getResponseEntity() 0 3 1
A addParam() 0 5 1
A setParams() 0 5 1
A setMethod() 0 5 1
A toArray() 0 18 5
A validate() 0 3 1
A getParam() 0 7 2
A getUrl() 0 3 1
A setUrl() 0 5 1
A getParams() 0 3 1
A getHeader() 0 7 2
A getMethods() 0 5 1
A containsHeader() 0 3 1
A getProtocolVersion() 0 3 1
A getMethod() 0 3 1
A addHeader() 0 5 1
A cleanHeaders() 0 5 1
A cleanParams() 0 5 1
A getRequestEntity() 0 3 1
A removeParam() 0 5 1
A setProtocolVersion() 0 5 1
A containsParam() 0 3 1
A removeHeader() 0 5 1
A setHeaders() 0 5 1
A setResponseEntity() 0 5 1
A getHeaders() 0 9 2
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Http
7
 * @package   Payever\Core
8
 * @author    payever GmbH <[email protected]>
9
 * @copyright 2017-2021 payever GmbH
10
 * @license   MIT <https://opensource.org/licenses/MIT>
11
 * @link      https://docs.payever.org/shopsystems/api/getting-started
12
 */
13
14
namespace Payever\ExternalIntegration\Core\Http;
15
16
use Payever\ExternalIntegration\Core\Base\RequestInterface;
17
18
/**
19
 * This class represents RequestInterface
20
 */
21
class Request implements RequestInterface
22
{
23
    const METHOD_GET     = 'GET';
24
    const METHOD_POST    = 'POST';
25
    const METHOD_PUT     = 'PUT';
26
    const METHOD_PATCH   = 'PATCH';
27
    const METHOD_DELETE  = 'DELETE';
28
29
    /** @var null|string $url */
30
    protected $url;
31
32
    /** @var string $method */
33
    protected $method = self::METHOD_GET;
34
35
    /** @var array $headers */
36
    protected $headers = [];
37
38
    /** @var array $params */
39
    protected $params = [];
40
41
    /** @var string $protocolVersion */
42
    protected $protocolVersion = '1.1';
43
44
    /** @var RequestEntity $requestEntity */
45
    protected $requestEntity;
46
47
    /** @var ResponseEntity $responseEntity */
48
    protected $responseEntity;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function addHeader($name, $value = null)
54
    {
55
        $this->headers[$name] = $value;
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function setHeaders(array $headers)
64
    {
65
        $this->headers = $headers;
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getHeader($name)
74
    {
75
        if ($this->containsHeader($name)) {
76
            return $this->headers[$name];
77
        }
78
79
        return false;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getHeaders()
86
    {
87
        $prettyHeaders = [];
88
89
        foreach ($this->headers as $name => $value) {
90
            $prettyHeaders[] = sprintf('%s: %s', $name, $value);
91
        }
92
93
        return $prettyHeaders;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function cleanHeaders()
100
    {
101
        $this->headers = [];
102
103
        return $this;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function containsHeader($name)
110
    {
111
        return isset($this->headers[$name]);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function removeHeader($name)
118
    {
119
        unset($this->headers[$name]);
120
121
        return $this;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function addParam($name, $value = null)
128
    {
129
        $this->params[$name] = $value;
130
131
        return $this;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function setParams(array $params)
138
    {
139
        $this->params = $params;
140
141
        return $this;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getParam($name)
148
    {
149
        if ($this->containsParam($name)) {
150
            return $this->params[$name];
151
        }
152
153
        return false;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getParams()
160
    {
161
        return $this->params;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function cleanParams()
168
    {
169
        $this->params = [];
170
171
        return $this;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function containsParam($name)
178
    {
179
        return isset($this->params[$name]);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function removeParam($name)
186
    {
187
        unset($this->params[$name]);
188
189
        return $this;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function setUrl($url)
196
    {
197
        $this->url = $url;
198
199
        return $this;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getUrl()
206
    {
207
        return $this->url;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function setMethod($method = self::METHOD_GET)
214
    {
215
        $this->method = $method;
216
217
        return $this;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getMethod()
224
    {
225
        return $this->method;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function setRequestEntity(RequestEntity $requestEntity)
232
    {
233
        $this->requestEntity = $requestEntity;
234
235
        return $this;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function setResponseEntity(ResponseEntity $responseEntity)
242
    {
243
        $this->responseEntity = $responseEntity;
244
245
        return $this;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function getRequestEntity()
252
    {
253
        return $this->requestEntity;
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function getResponseEntity()
260
    {
261
        return $this->responseEntity;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function getProtocolVersion()
268
    {
269
        return $this->protocolVersion;
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function setProtocolVersion($protocolVersion)
276
    {
277
        $this->protocolVersion = $protocolVersion;
278
279
        return $this;
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285
    public function validate()
286
    {
287
        return $this->getRequestEntity()->isValid();
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public static function getMethods()
294
    {
295
        return [
296
            self::METHOD_GET,
297
            self::METHOD_POST,
298
        ];
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     */
304
    public function toArray()
305
    {
306
        $array = $this->getRequestEntity()->toArray();
307
308
        if (
309
            $this->getHeader('Content-Type') == 'application/x-www-form-urlencoded'
310
            || $this->getHeader('Content-Type') == 'application/json'
311
        ) {
312
            return $array;
313
        }
314
315
        foreach ($array as $key => $value) {
316
            if (is_array($value)) {
317
                $array[$key] = json_encode($value);
318
            }
319
        }
320
321
        return $array;
322
    }
323
}
324