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 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http;
3
4
use Psr\Http\Message\UriInterface;
5
use Psr\Http\Message\StreamInterface;
6
use Psr\Http\Message\RequestInterface;
7
8
class Request implements RequestInterface
9
{
10
11
    use MessageTrait;
12
13
    /**
14
     * @var string
15
     */
16
    private $method;
17
18
    /**
19
     * @var string
20
     */
21
    private $requestTarget;
22
23
    /**
24
     * @var UriInterface
25
     */
26
    private $uri;
27
28
    /**
29
     * 
30
     * @param string $method
31
     * @param UriInterface $uri
32
     * @param array $headers
33
     * @param StreamInterface|null $body
34
     * @param string $protocolVersion
35
     */
36 22
    public function __construct($method, UriInterface $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
37
    {
38 22
        $this->method = $method;
39 22
        $this->uri = $uri;
40 22
        $this->body = $body;
41
42 22
        foreach ($headers AS $header => $value) {
43
            $this->headersName[strtolower($header)] = $header;
44
            $this->headers[$header] = !is_array($value) ? [$value] : $value;
45
        }
46
47 22
        if (!$this->hasHeader('Host')) {
48 22
            if (!$uri = $this->uri->getHost()) {
49 22
                $uri = 'localhost';
50
            }
51 22
            $this->headersName['host'] = 'Host';
52 22
            $this->headers['Host'] = [$uri];
53
        }
54
55 22
        $this->protocolVersion = $protocolVersion;
56 22
    }
57
58
    /**
59
     * @return string
60
     */
61 4
    public function getMethod()
62
    {
63 4
        return $this->method;
64
    }
65
66
    /**
67
     * @param string $method
68
     * @return self
69
     */
70 2
    public function withMethod($method)
71
    {
72 2
        if ($method === $this->method) {
73
            return $this;
74
        }
75
76 2
        $clone = clone $this;
77 2
        $clone->method = $method;
78
79 2
        return $clone;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getRequestTarget()
86
    {
87
88
        if ($this->requestTarget) {
89
            return $this->requestTarget;
90
        }
91
92
        $target = $this->uri->getPath();
93
94
        if ($target === '') {
95
            $target .= '/';
96
        }
97
98
        if ($this->uri->getQuery()) {
99
            $target .= '?' . $this->uri->getQuery();
100
        }
101
102
        return $target;
103
    }
104
105
    /**
106
     * @param string $requestTarget
107
     * @return self
108
     */
109
    public function withRequestTarget($requestTarget)
110
    {
111
        if ($requestTarget === $this->requestTarget) {
112
            return $this;
113
        }
114
115
        $clone = clone $this;
116
        $clone->requestTarget = $requestTarget;
117
118
        return $clone;
119
    }
120
121
    /**
122
     * @return UriInterface
123
     */
124
    public function getUri()
125
    {
126
        return $this->uri;
127
    }
128
129
    /**
130
     * @param UriInterface $uri
131
     * @param boolean $preserveHost
132
     * @return self
133
     */
134
    public function withUri(UriInterface $uri, $preserveHost = false)
135
    {
136
        if ($uri === $this->uri) {
137
            return $this;
138
        }
139
140
        $clone = clone $this;
141
        $clone->uri = $uri;
142
143
        if ($preserveHost) {
144
            if (!$clone->hasHeader('host')) {
145
                $clone->headers['host'] = 'Host';
146
                $clone->headers['Host'] = [$uri->getHost()];
147
            }
148
        }
149
150
        return $clone;
151
    }
152
}
153