Request::getUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of slick/http
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Http\Message;
11
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\StreamInterface;
14
use Psr\Http\Message\UriInterface;
15
use Slick\Http\Message\Exception\InvalidArgumentException;
16
17
/**
18
 * Request
19
 *
20
 * @package Slick\Http\Message
21
*/
22
class Request extends Message implements RequestInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $method;
28
29
    /**
30
     * @var string
31
     */
32
    protected $target;
33
34
    /**
35
     * @var UriInterface
36
     */
37
    private $uri;
38
39
    /**
40
     * Creates an HTTP Request message
41
     *
42
     * @param string                   $method
43
     * @param string|StreamInterface   $body
44
     * @param null|string|UriInterface $target
45
     * @param array                    $headers
46
     */
47
    public function __construct($method, $target = null, $body = '', array $headers = [])
48
    {
49
        parent::__construct($body);
50
        $this->method = $method;
51
52
        $this->target = $target instanceof UriInterface
53
            ? $this->setUri($target)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setUri($target) targeting Slick\Http\Message\Request::setUri() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54
            : $target;
55
56
        foreach ($headers as $name => $header) {
57
            $this->headers[$name] = [$header];
58
        }
59
    }
60
61
    /**
62
     * Retrieves the message's request target.
63
     *
64
     * If no URI is available, and no request-target has been specifically
65
     * provided, this method MUST return the string "/".
66
     *
67
     * @return string
68
     */
69
    public function getRequestTarget()
70
    {
71
        if (! $this->target && ! $this->uri) {
72
            return '/';
73
        }
74
75
        return $this->target ? $this->target : $this->getTargetFromUri();
76
    }
77
78
    /**
79
     * Return an instance with the specific request-target.
80
     *
81
     * @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various
82
     *     request-target forms allowed in request messages)
83
     * @param mixed $requestTarget
84
     * @return static
85
     */
86
    public function withRequestTarget($requestTarget)
87
    {
88
        $message = clone $this;
89
        $message->target = $requestTarget;
90
        return $message;
91
    }
92
93
    /**
94
     * Retrieves the HTTP method of the request.
95
     *
96
     * @return string Returns the request method.
97
     */
98
    public function getMethod()
99
    {
100
        return $this->method;
101
    }
102
103
    /**
104
     * Return an instance with the provided HTTP method.
105
     *
106
     * @param string $method Case-sensitive method.
107
     * @return static
108
     * @throws InvalidArgumentException for invalid HTTP methods.
109
     */
110
    public function withMethod($method)
111
    {
112
        $method = strtoupper($method);
113
        $knownMethods = [
114
            'HEAD', 'OPTIONS', 'GET', 'POST', 'PUT',
115
            'DELETE', 'CONNECT', 'TRACE', 'PATCH', 'PURGE'
116
        ];
117
118
        if (! in_array($method, $knownMethods)) {
119
            throw new InvalidArgumentException(
120
                "Invalid or unknown method name."
121
            );
122
        }
123
124
        $message = clone $this;
125
        $message->method = $method;
126
        return $message;
127
    }
128
129
    /**
130
     * Retrieves the URI instance.
131
     *
132
     * @link http://tools.ietf.org/html/rfc3986#section-4.3
133
     * @return UriInterface Returns a UriInterface instance
134
     *     representing the URI of the request.
135
     */
136
    public function getUri()
137
    {
138
        return $this->uri;
139
    }
140
141
    /**
142
     * Returns an instance with the provided URI.
143
     *
144
     * @link http://tools.ietf.org/html/rfc3986#section-4.3
145
     * @param UriInterface $uri New request URI to use.
146
     * @param bool $preserveHost Preserve the original state of the Host header.
147
     * @return static
148
     */
149
    public function withUri(UriInterface $uri, $preserveHost = false)
150
    {
151
        $message = clone $this;
152
        $message->setUri($uri, $preserveHost);
153
        return $message;
154
    }
155
156
    /**
157
     * Sets the request URI
158
     *
159
     * @param UriInterface $uri
160
     * @param bool $preserveHost
161
     */
162
    protected function setUri(UriInterface $uri, $preserveHost = false)
163
    {
164
        if (! $preserveHost && $uri->getHost() !== '') {
165
            $key = $this->headerKey('Host');
166
            $this->headers[$key] = [$uri->getHost()];
167
        }
168
169
        $this->uri = $uri;
170
    }
171
172
    /**
173
     * Get the target from the uri
174
     *
175
     * @return string
176
     */
177
    private function getTargetFromUri()
178
    {
179
        $target  = "/{$this->uri->getPath()}";
180
        $target .= $this->uri->getQuery() !== ''
181
            ? "?{$this->uri->getQuery()}"
182
            : '';
183
        $target .= $this->uri->getFragment() !== ''
184
            ? "#{$this->uri->getFragment()}"
185
            : '';
186
        return str_replace('//', '/', $target);
187
    }
188
}
189