Request::withUri()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 8.8571
cc 6
eloc 8
nc 3
nop 2
crap 6.0702
1
<?php
2
3
namespace CodeJet\Http;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\UriInterface;
7
8
/**
9
 * Class Request
10
 * @package CodeJet\Http
11
 */
12
class Request extends Message implements RequestInterface
13
{
14
    const DEFAULT_REQUEST_TARGET = "/";
15
16
    /**
17
     * Request Method SHOULD be one of GET,PUT,POST,PATCH,DELETE
18
     * @var string
19
     */
20
    protected $method = 'GET';
21
22
    /**
23
     * @var UriInterface
24
     */
25
    protected $uri;
26
27
    /**
28
     * @var string
29
     */
30
    protected $requestTarget;
31
32
    /**
33
     * @inheritdoc
34
     */
35 9
    public function getRequestTarget()
36
    {
37 9
        if ($this->requestTarget) {
38 3
            return $this->requestTarget;
39
        }
40
41 6
        $requestTarget = self::DEFAULT_REQUEST_TARGET;
42
43 6
        if ($this->uri) {
44 3
            $requestTarget = $this->uri->getPath();
45
46 3
            if (!empty($this->uri->getQuery())) {
47 3
                $requestTarget .= '?' . $this->uri->getQuery();
48
            }
49
        }
50
51 6
        return $requestTarget;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 24
    public function getMethod()
58
    {
59 24
        return $this->method;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 3
    public function withRequestTarget($requestTarget)
66
    {
67 3
        $clone = clone $this;
68 3
        $clone->requestTarget = $requestTarget;
69
70 3
        return $clone;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 24
    public function withMethod($method)
77
    {
78 24
        $clone = clone $this;
79 24
        $clone->method = $method;
80
81 24
        return $clone;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 24
    public function getUri()
88
    {
89 24
        return $this->uri;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 36
    public function withUri(UriInterface $uri, $preserveHost = false)
96
    {
97 36
        $clone = clone $this;
98 36
        $clone->uri = $uri;
99
100 36
        if (!empty($uri->getHost()) && $preserveHost === false) {
101 30
            return $clone->withHeader('host', $uri->getHost());
102
        }
103
104 6
        if (empty($clone->getHeader('host')) && !empty($uri->getHost()) && $preserveHost === true) {
105
            $clone = $clone->withHeader('host', $uri->getHost());
106
        }
107
108 6
        return $clone;
109
    }
110
}
111