RequestBuilder   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 480
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 39
eloc 81
c 1
b 0
f 0
dl 0
loc 480
rs 9.28

36 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setUrl() 0 5 1
A addRawHeader() 0 7 1
A getDefaultHeaders() 0 4 1
A getHeader() 0 7 2
A containsParam() 0 3 1
A post() 0 3 1
A getResponseEntity() 0 3 1
A getRequestEntity() 0 3 1
A addParam() 0 5 1
A contentTypeIsJson() 0 5 1
A getParam() 0 7 2
A getParams() 0 3 1
A removeHeader() 0 5 1
A patch() 0 3 1
A setHeaders() 0 5 1
A removeParam() 0 5 1
A setFormUrlencoded() 0 5 1
A get() 0 3 1
A getMethod() 0 3 1
A setResponseEntity() 0 5 1
A getHeaders() 0 3 1
A getUrl() 0 3 1
A cleanHeaders() 0 5 1
A build() 0 17 1
A setProtocolVersion() 0 5 1
A getProtocolVersion() 0 3 1
A cleanParams() 0 5 1
A setMethod() 0 7 2
A setParams() 0 5 1
A containsHeader() 0 3 1
A delete() 0 3 1
A addHeader() 0 5 1
A put() 0 3 1
A setRequestEntity() 0 5 1
A getMethods() 0 3 1
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\Engine;
17
18
/**
19
 * This class represents Request Builder
20
 * @SuppressWarnings(PHPMD.StaticAccess)
21
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
22
 */
23
class RequestBuilder
24
{
25
    /** @var null|string $url */
26
    protected $url;
27
28
    /** @var string $method */
29
    protected $method = Request::METHOD_GET;
30
31
    /** @var array $headers */
32
    protected $headers = [];
33
34
    /** @var array $params */
35
    protected $params = [];
36
37
    /** @var string $protocolVersion */
38
    protected $protocolVersion = '1.1';
39
40
    /** @var RequestEntity $requestEntity */
41
    protected $requestEntity;
42
43
    /** @var ResponseEntity $responseEntity */
44
    protected $responseEntity;
45
46
    /**
47
     * @param string $url
48
     * @param string $method
49
     */
50
    public function __construct($url = null, $method = Request::METHOD_GET)
51
    {
52
        $this->method = $method;
53
        $this->url = $url;
54
        $this->requestEntity = new RequestEntity();
55
        $this->responseEntity = new ResponseEntity();
56
        $this->headers = $this->getDefaultHeaders();
57
    }
58
59
    /**
60
     * Instantiates RequestBuilder with GET method set
61
     *
62
     * @param string|null $url
63
     *
64
     * @return self
65
     */
66
    public static function get($url = null)
67
    {
68
        return new static($url, Request::METHOD_GET);
69
    }
70
71
    /**
72
     * Instantiates RequestBuilder with POST method set
73
     *
74
     * @param string|null $url
75
     *
76
     * @return self
77
     */
78
    public static function post($url = null)
79
    {
80
        return new static($url, Request::METHOD_POST);
81
    }
82
83
    /**
84
     * Instantiates RequestBuilder with PUT method set
85
     *
86
     * @param string|null $url
87
     * @return self
88
     */
89
    public static function put($url = null)
90
    {
91
        return new static($url, Request::METHOD_PUT);
92
    }
93
94
    /**
95
     * Instantiates RequestBuilder with PATCH method set
96
     *
97
     * @param string|null $url
98
     * @return self
99
     */
100
    public static function patch($url = null)
101
    {
102
        return new static($url, Request::METHOD_PATCH);
103
    }
104
105
    /**
106
     * Instantiates RequestBuilder with DELETE method set
107
     *
108
     * @param string|null $url
109
     * @return self
110
     */
111
    public static function delete($url = null)
112
    {
113
        return new static($url, Request::METHOD_DELETE);
114
    }
115
116
    /**
117
     * Builds Request object
118
     *
119
     * @return Request
120
     */
121
    public function build()
122
    {
123
        $request = new Request();
124
125
        $request
126
            ->setUrl($this->getUrl())
127
            ->setMethod($this->getMethod())
128
            ->setHeaders($this->getHeaders())
129
            ->setParams($this->getParams())
130
            ->setProtocolVersion($this->getProtocolVersion())
131
            ->setRequestEntity($this->getRequestEntity())
132
            ->setResponseEntity($this->getResponseEntity())
133
            ;
134
135
        $request->getRequestEntity()->load($request->getParams());
136
137
        return $request;
138
    }
139
140
    /**
141
     * Adds Header to Headers array
142
     *
143
     * @param string      $name
144
     * @param string|null $value
145
     *
146
     * @return self
147
     */
148
    public function addHeader($name, $value = null)
149
    {
150
        $this->headers[$name] = $value;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Adds Header from a string
157
     *
158
     * @param string $string
159
     *
160
     * @return self
161
     */
162
    public function addRawHeader($string)
163
    {
164
        $pieces = explode(':', $string, 2);
165
166
        $this->addHeader($pieces[0], $pieces[1]);
167
168
        return $this;
169
    }
170
171
    /**
172
     * Replaces Headers with given array
173
     *
174
     * @param array $headers
175
     *
176
     * @return self
177
     */
178
    public function setHeaders($headers)
179
    {
180
        $this->headers = $headers;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Returns Header value from Headers array with given name
187
     *
188
     * @param string $name
189
     *
190
     * @return bool|string
191
     */
192
    public function getHeader($name)
193
    {
194
        if ($this->containsHeader($name)) {
195
            return $this->headers[$name];
196
        }
197
198
        return false;
199
    }
200
201
    /**
202
     * Returns Headers array
203
     *
204
     * @return array
205
     */
206
    public function getHeaders()
207
    {
208
        return $this->headers;
209
    }
210
211
    /**
212
     * Cleans Headers array
213
     *
214
     * @return self
215
     */
216
    public function cleanHeaders()
217
    {
218
        $this->headers = [];
219
220
        return $this;
221
    }
222
223
    /**
224
     * Returns if the Header with given name is set
225
     *
226
     * @param string $name
227
     *
228
     * @return bool
229
     */
230
    public function containsHeader($name)
231
    {
232
        return isset($this->headers[$name]);
233
    }
234
235
    /**
236
     * Removes Header with given name from Headers array
237
     *
238
     * @param string $name
239
     *
240
     * @return self
241
     */
242
    public function removeHeader($name)
243
    {
244
        unset($this->headers[$name]);
245
246
        return $this;
247
    }
248
249
    /**
250
     * Adds Param to Params array
251
     *
252
     * @param string      $name
253
     * @param string|null $value
254
     *
255
     * @return self
256
     */
257
    public function addParam($name, $value = null)
258
    {
259
        $this->params[$name] = $value;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Replaces Params with given array
266
     *
267
     * @param array $params
268
     *
269
     * @return self
270
     */
271
    public function setParams($params)
272
    {
273
        $this->params = $params;
274
275
        return $this;
276
    }
277
278
    /**
279
     * Returns Param value from Params array with given name
280
     *
281
     * @param string $name
282
     *
283
     * @return bool|string
284
     */
285
    public function getParam($name)
286
    {
287
        if ($this->containsParam($name)) {
288
            return $this->params[$name];
289
        }
290
291
        return false;
292
    }
293
294
    /**
295
     * Returns Params array
296
     *
297
     * @return array
298
     */
299
    public function getParams()
300
    {
301
        return $this->params;
302
    }
303
304
    /**
305
     * Cleans Params array
306
     *
307
     * @return self
308
     */
309
    public function cleanParams()
310
    {
311
        $this->params = [];
312
313
        return $this;
314
    }
315
316
    /**
317
     * Returns if the Param with given name is set
318
     *
319
     * @param string $name
320
     *
321
     * @return bool
322
     */
323
    public function containsParam($name)
324
    {
325
        return isset($this->params[$name]);
326
    }
327
328
    /**
329
     * Removes Param with given name from Params array
330
     *
331
     * @param string $name
332
     *
333
     * @return self
334
     */
335
    public function removeParam($name)
336
    {
337
        unset($this->params[$name]);
338
339
        return $this;
340
    }
341
342
    /**
343
     * Sets URL
344
     *
345
     * @param string $url
346
     *
347
     * @return self
348
     */
349
    public function setUrl($url)
350
    {
351
        $this->url = $url;
352
353
        return $this;
354
    }
355
356
    /**
357
     * Returns URL
358
     *
359
     * @return string
360
     */
361
    public function getUrl()
362
    {
363
        return $this->url;
364
    }
365
366
    /**
367
     * Sets Http Method
368
     *
369
     * @param string $method
370
     *
371
     * @return self
372
     */
373
    public function setMethod($method = Request::METHOD_GET)
374
    {
375
        if (in_array($method, self::getMethods())) {
376
            $this->method = $method;
377
        }
378
379
        return $this;
380
    }
381
382
    /**
383
     * Send payload as application/x-www-form-urlencoded
384
     *
385
     * @return $this
386
     */
387
    public function setFormUrlencoded()
388
    {
389
        $this->addHeader('Content-Type', 'application/x-www-form-urlencoded');
390
391
        return $this;
392
    }
393
394
    /**
395
     * Send payload as JSON string
396
     */
397
    public function contentTypeIsJson()
398
    {
399
        $this->addHeader('Content-Type', 'application/json');
400
401
        return $this;
402
    }
403
404
    /**
405
     * Returns Http method used
406
     *
407
     * @return string
408
     */
409
    public function getMethod()
410
    {
411
        return $this->method;
412
    }
413
414
    /**
415
     * Returns available Http Methods
416
     *
417
     * @return array
418
     */
419
    public static function getMethods()
420
    {
421
        return Request::getMethods();
422
    }
423
424
    /**
425
     * Sets Request Entity
426
     *
427
     * @param RequestEntity $requestEntity
428
     *
429
     * @return self
430
     */
431
    public function setRequestEntity($requestEntity)
432
    {
433
        $this->requestEntity = $requestEntity;
434
435
        return $this;
436
    }
437
438
    /**
439
     * Sets ResponseInterface Entity
440
     *
441
     * @param ResponseEntity $responseEntity
442
     *
443
     * @return self
444
     */
445
    public function setResponseEntity($responseEntity)
446
    {
447
        $this->responseEntity = $responseEntity;
448
449
        return $this;
450
    }
451
452
    /**
453
     * Returns Request Entity used
454
     *
455
     * @return RequestEntity
456
     */
457
    public function getRequestEntity()
458
    {
459
        return $this->requestEntity;
460
    }
461
462
    /**
463
     * Returns ResponseInterface Entity used
464
     *
465
     * @return ResponseEntity
466
     */
467
    public function getResponseEntity()
468
    {
469
        return $this->responseEntity;
470
    }
471
472
    /**
473
     * Get Protocol version
474
     *
475
     * @return string
476
     */
477
    public function getProtocolVersion()
478
    {
479
        return $this->protocolVersion;
480
    }
481
482
    /**
483
     * Set Protocol Version
484
     *
485
     * @param string $protocolVersion
486
     *
487
     * @return self
488
     */
489
    public function setProtocolVersion($protocolVersion)
490
    {
491
        $this->protocolVersion = $protocolVersion;
492
493
        return $this;
494
    }
495
496
    /**
497
     * @return array
498
     */
499
    public function getDefaultHeaders()
500
    {
501
        return [
502
            'User-agent' => sprintf('payever PHP SDK v%s / cURL client', Engine::SDK_VERSION),
503
        ];
504
    }
505
}
506