Passed
Push — master ( 8fd177...8c2cea )
by Rougin
03:08
created

Request::getRequestTarget()   A

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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Zapheus\Bridge\Psr;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\StreamInterface;
7
use Psr\Http\Message\UriInterface;
8
9
/**
10
 * Request
11
 *
12
 * @package Zapheus
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class Request extends Message implements RequestInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $target = '/';
21
22
    /**
23
     * @var string
24
     */
25
    protected $method = 'GET';
26
27
    /**
28
     * @var \Psr\Http\Message\UriInterface
29
     */
30
    protected $uri;
31
32
    /**
33
     * Initializes the request instance.
34
     *
35
     * @param string                                 $method
36
     * @param string                                 $target
37
     * @param \Psr\Http\Message\UriInterface|null    $uri
38
     * @param \Psr\Http\Message\StreamInterface|null $body
39
     * @param array                                  $headers
40
     * @param string                                 $version
41
     */
42
    public function __construct($method = 'GET', $target = '/', UriInterface $uri = null, StreamInterface $body = null, array $headers = array(), $version = '1.1')
43
    {
44
        parent::__construct($body, $headers, $version);
45
46
        $this->method = $method;
47
48
        $this->target = $target;
49
50
        $this->uri = $uri === null ? new Uri : $uri;
51
    }
52
53
    /**
54
     * Retrieves the HTTP method of the request.
55
     *
56
     * @return string
57
     */
58
    public function getMethod()
59
    {
60
        return $this->method;
61
    }
62
63
    /**
64
     * Retrieves the message's request target.
65
     *
66
     * @return string
67
     */
68
    public function getRequestTarget()
69
    {
70
        return $this->target;
71
    }
72
73
    /**
74
     * Retrieves the URI instance.
75
     *
76
     * @return \Psr\Http\Message\UriInterface
77
     */
78
    public function getUri()
79
    {
80
        return $this->uri;
81
    }
82
83
    /**
84
     * Returns an instance with the provided HTTP method.
85
     *
86
     * @param  string $method
87
     * @return static
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91
    public function withMethod($method)
92
    {
93
        // TODO: Add \InvalidArgumentException
94
95
        $static = clone $this;
96
97
        $static->method = $method;
98
99
        return $static;
100
    }
101
102
    /**
103
     * Returns an instance with the specific request-target.
104
     *
105
     * @param  mixed $target
106
     * @return static
107
     */
108
    public function withRequestTarget($target)
109
    {
110
        $static = clone $this;
111
112
        $static->target = $target;
113
114
        return $static;
115
    }
116
117
    /**
118
     * Returns an instance with the provided URI.
119
     *
120
     * @param  \Psr\Http\Message\UriInterface $uri
121
     * @param  boolean                        $preserve
122
     * @return static
123
     */
124
    public function withUri(UriInterface $uri, $preserve = false)
125
    {
126
        $static = clone $this;
127
128
        $static->uri = $uri;
129
130
        if (! $preserve && $host = $uri->getHost()) {
131
            $port = $host . ':' . $uri->getPort();
132
133
            $host = $uri->getPort() ? $port : $host;
134
135
            $static->headers['Host'] = (array) $host;
136
        }
137
138
        return $static;
139
    }
140
}
141