Completed
Push — master ( 6b8416...c520c0 )
by Filipe
02:12
created

Request   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 3
dl 0
loc 165
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getRequestTarget() 0 6 4
A withRequestTarget() 0 6 1
A getMethod() 0 4 1
A withMethod() 0 18 2
A getUri() 0 4 1
A withUri() 0 6 1
A setUri() 0 9 3
A getTargetFromUri() 0 11 3
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);
0 ignored issues
show
Bug introduced by
It seems like $body defined by parameter $body on line 47 can also be of type object<Psr\Http\Message\StreamInterface>; however, Slick\Http\Message\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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