Request::getMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CodeblogPro\GeoCoder\Http;
6
7
use CodeblogPro\GeoCoder\Exceptions\InvalidArgumentException;
8
9
class Request implements RequestInterface
10
{
11
    private const AVAILABLE_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
12
    private const AVAILABLE_VERSIONS = ['1.0', '1.1', '2.0'];
13
    private string $method;
14
    private string $url;
15
    private string $body;
16
    private array $headers;
17
    private string $version;
18
19 4
    public function __construct(string $method, string $url, string $body, array $headers = [], string $version = '1.1')
20
    {
21 4
        $this->setMethodIsValid($method);
22 3
        $this->setUrlIsValid($url);
23 2
        $this->setBody($body);
24 2
        $this->setHeadersIsValid($headers);
25 2
        $this->setVersionIsValid($version);
26 1
    }
27
28
    public function getMethod(): string
29
    {
30
        return $this->method;
31
    }
32
33
    public function getUri(): string
34
    {
35
        return $this->url;
36
    }
37
38
    public function getBody(): string
39
    {
40
        return $this->body;
41
    }
42
43
    public function getHeaders(): array
44
    {
45
        return $this->headers;
46
    }
47
48
    public function getVersion(): string
49
    {
50
        return $this->version;
51
    }
52
53 4
    private function setMethodIsValid(string $method): void
54
    {
55 4
        $method = mb_strtoupper(trim((string)$method));
56
57 4
        if (!in_array($method, self::AVAILABLE_METHODS)) {
58 1
            throw new InvalidArgumentException(
59
                'Method can only accept to: '
60 1
                . implode(',', self::AVAILABLE_METHODS)
61
            );
62
        }
63
64 3
        $this->method = $method;
65 3
    }
66
67 3
    private function setUrlIsValid(string $url): void
68
    {
69 3
        $url = trim((string)$url);
70
71 3
        if (empty($url)) {
72 1
            throw new InvalidArgumentException('Url cannot be empty.');
73
        }
74
75 2
        $this->url = $url;
76 2
    }
77
78 2
    private function setBody(string $body): void
79
    {
80 2
        $this->body = $body;
81 2
    }
82
83 2
    private function setHeadersIsValid(array $headers): void
84
    {
85 2
        if (!is_array($headers)) {
0 ignored issues
show
introduced by
The condition is_array($headers) is always true.
Loading history...
86
            throw new InvalidArgumentException('Headers must be an array type.');
87
        }
88
89 2
        $this->headers = $headers;
90 2
    }
91
92 2
    private function setVersionIsValid(string $version): void
93
    {
94 2
        $version = trim((string)$version);
95
96 2
        if (!in_array($version, self::AVAILABLE_VERSIONS)) {
97 1
            throw new InvalidArgumentException(
98
                'Version can only accept to: '
99 1
                . implode(',', self::AVAILABLE_VERSIONS)
100
            );
101
        }
102
103 1
        $this->version = $version;
104 1
    }
105
}
106