Passed
Push — master ( d0466a...c02546 )
by Zlatin
01:22
created

RequestTrait::withRequestTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http;
3
4
use Psr\Http\Message\UriInterface;
5
6
trait RequestTrait
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private $method;
13
14
    /**
15
     * @var string
16
     */
17
    private $requestTarget;
18
19
    /**
20
     * @var UriInterface
21
     */
22
    private $uri;
23
24
    /**
25
     * @return string
26
     */
27 4
    public function getMethod()
28
    {
29 4
        return $this->method;
30
    }
31
32
    /**
33
     * @param string $method
34
     * @return \DevOp\Core\Http\Request|$this
35
     */
36 2
    public function withMethod($method)
37
    {
38 2
        if ($method === $this->method) {
39
            return $this;
40
        }
41
42 2
        $clone = clone $this;
43 2
        $clone->method = $method;
44
45 2
        return $clone;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getRequestTarget()
52
    {
53
        
54
        if ($this->requestTarget) {
55
            return $this->requestTarget;
56
        }
57
58
        $target = $this->uri->getPath();
59
60
        if ($target === '') {
61
            $target .= '/';
62
        }
63
64
        if ($this->uri->getQuery()) {
65
            $target .= '?' . $this->uri->getQuery();
66
        }
67
68
        return $target;
69
    }
70
71
    /**
72
     * @param string $requestTarget
73
     * @return \DevOp\Core\Http\Request|$this
74
     */
75
    public function withRequestTarget($requestTarget)
76
    {
77
        if ($requestTarget === $this->requestTarget) {
78
            return $this;
79
        }
80
81
        $clone = clone $this;
82
        $clone->requestTarget = $requestTarget;
83
84
        return $clone;
85
    }
86
87
    /**
88
     * @return UriInterface
89
     */
90
    public function getUri()
91
    {
92
        return $this->uri;
93
    }
94
95
    /**
96
     * @param UriInterface $uri
97
     * @param boolean $preserveHost
98
     * @return \DevOp\Core\Http\Request|$this
99
     */
100
    public function withUri(UriInterface $uri, $preserveHost = false)
101
    {
102
        if ($uri === $this->uri) {
103
            return $this;
104
        }
105
106
        $clone = clone $this;
107
        $clone->uri = $uri;
108
109
        if ($preserveHost) {
110
            if (!$clone->hasHeader('host')) {
0 ignored issues
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            if (!$clone->/** @scrutinizer ignore-call */ hasHeader('host')) {
Loading history...
111
                $clone->headers['host'] = 'Host';
1 ignored issue
show
Bug Best Practice introduced by
The property headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
                $clone->headers['Host'] = [$uri->getHost()];
113
            }
114
        }
115
116
        return $clone;
117
    }
118
}
119