Passed
Push — master ( 0ce391...b65012 )
by Zlatin
01:44
created

RequestTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 17.95%

Importance

Changes 0
Metric Value
dl 0
loc 117
ccs 7
cts 39
cp 0.1795
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 3 1
A getRequestTarget() 0 18 4
A withRequestTarget() 0 10 2
A withMethod() 0 10 2
A getUri() 0 3 1
A withUri() 0 17 4
1
<?php
2
namespace DevOp\Core\Http\Traits;
3
4
use Psr\Http\Message\UriInterface;
5
6
trait RequestTrait
7
{
8
9
    /**
10
     * @var string
11
     */
12
    protected $method;
13
14
    /**
15
     * @var string
16
     */
17
    protected $requestTarget;
18
19
    /**
20
     * @var UriInterface
21
     */
22
    protected $uri;
23
24
    /**
25
     * @param string $name
26
     * @return boolean
27
     */
28
    abstract public function hasHeader($name);
29
30
    /**
31
     * @return string
32
     */
33 4
    public function getMethod()
34
    {
35 4
        return $this->method;
36
    }
37
38
    /**
39
     * @param string $method
40
     * @return self
41
     */
42 2
    public function withMethod($method)
43
    {
44 2
        if ($method === $this->method) {
45
            return $this;
46
        }
47
48 2
        $clone = clone $this;
49 2
        $clone->method = $method;
50
51 2
        return $clone;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getRequestTarget()
58
    {
59
60
        if ($this->requestTarget) {
61
            return $this->requestTarget;
62
        }
63
64
        $target = $this->uri->getPath();
65
66
        if ($target === '') {
67
            $target .= '/';
68
        }
69
70
        if ($this->uri->getQuery()) {
71
            $target .= '?' . $this->uri->getQuery();
72
        }
73
74
        return $target;
75
    }
76
77
    /**
78
     * @param string $requestTarget
79
     * @return self
80
     */
81
    public function withRequestTarget($requestTarget)
82
    {
83
        if ($requestTarget === $this->requestTarget) {
84
            return $this;
85
        }
86
87
        $clone = clone $this;
88
        $clone->requestTarget = $requestTarget;
89
90
        return $clone;
91
    }
92
93
    /**
94
     * @return UriInterface
95
     */
96
    public function getUri()
97
    {
98
        return $this->uri;
99
    }
100
101
    /**
102
     * @param UriInterface $uri
103
     * @param boolean $preserveHost
104
     * @return self
105
     */
106
    public function withUri(UriInterface $uri, $preserveHost = false)
107
    {
108
        if ($uri === $this->uri) {
109
            return $this;
110
        }
111
112
        $clone = clone $this;
113
        $clone->uri = $uri;
114
115
        if ($preserveHost) {
116
            if (!$clone->hasHeader('host')) {
117
                $clone->headers['host'] = 'Host';
118
                $clone->headers['Host'] = [$uri->getHost()];
119
            }
120
        }
121
122
        return $clone;
123
    }
124
}
125