Request   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 94%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 43
c 1
b 1
f 0
dl 0
loc 152
ccs 47
cts 50
cp 0.94
rs 10
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A withUri() 0 15 4
A getInnerObject() 0 13 2
A getUri() 0 7 2
A getRequestTarget() 0 3 1
A updateHostFromUri() 0 14 3
A withRequestTarget() 0 8 1
A getMethod() 0 3 2
A withMethod() 0 10 2
1
<?php
2
3
namespace DMT\Aura\Psr\Message;
4
5
use Aura\Web\Request as AuraRequest;
6
use Aura\Web\WebFactory;
7
use DMT\Aura\Psr\Factory\StreamFactory;
8
use DMT\Aura\Psr\Factory\UriFactory;
9
use InvalidArgumentException;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\UriInterface;
12
13
/**
14
 * Class Request
15
 *
16
 * @package DMT\Aura\Psr\Message
17
 */
18
class Request implements RequestInterface
19
{
20
    use MessageTrait;
0 ignored issues
show
introduced by
The trait DMT\Aura\Psr\Message\MessageTrait requires some properties which are not provided by DMT\Aura\Psr\Message\Request: $server, $status, $content, $headers
Loading history...
21
22
    /** @var UriInterface $uri */
23
    protected $uri;
24
25
    /**
26
     * Request constructor.
27
     *
28
     * @param string $method The HTTP method associated with the request.
29
     * @param UriInterface|string $uri The URI associated with the request.
30
     */
31 54
    public function __construct(string $method, $uri)
32
    {
33 54
        $request = $this->getInnerObject();
34 54
        $this->setObjectProperty($request->method , 'value', $method);
35
36 54
        $this->uri = (new UriFactory())->createUri($uri);
37 54
        $this->body = (new StreamFactory())->createStream();
38 54
    }
39
40
    /**
41
     * @return AuraRequest
42
     */
43 54
    public function getInnerObject(): AuraRequest
44
    {
45 54
        if (!$this->object) {
46 54
            $this->object = (new WebFactory([]))->newRequest();
47
        }
48
49 54
        $this->setObjectProperty(
50 54
            $this->object ,
51 54
            'client',
52 54
            new AuraRequest\Client($this->object->server->get())
53
        );
54
55 54
        return $this->object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->object could return the type Aura\Web\Response which is incompatible with the type-hinted return Aura\Web\Request. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58
    /**
59
     * Retrieves the message's request target.
60
     *
61
     * @return string
62
     */
63 1
    public function getRequestTarget(): string
64
    {
65 1
        return $this->getInnerObject()->server->get('REQUEST_URI', '/');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getInnerOb...get('REQUEST_URI', '/') could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
68
    /**
69
     * Return an instance with the specific request-target.
70
     *
71
     * @param mixed $requestTarget
72
     * @return static
73
     */
74 1
    public function withRequestTarget($requestTarget): self
75
    {
76 1
        $instance = clone($this);
77
78 1
        $server = $instance->getInnerObject()->server;
79 1
        $server['REQUEST_URI'] = $requestTarget;
80
81 1
        return $instance;
82
    }
83
84
    /**
85
     * Retrieves the HTTP method of the request.
86
     *
87
     * @return string Returns the request method.
88
     */
89 2
    public function getMethod(): string
90
    {
91 2
        return $this->getInnerObject()->method->get() ?: 'GET';
92
    }
93
94
    /**
95
     * Return an instance with the provided HTTP method.
96
     *
97
     * @param string $method Case-sensitive method.
98
     * @return static
99
     * @throws InvalidArgumentException for invalid HTTP methods.
100
     */
101 7
    public function withMethod($method): self
102
    {
103 7
        if (!is_string($method)) {
0 ignored issues
show
introduced by
The condition is_string($method) is always true.
Loading history...
104 6
            throw new InvalidArgumentException('Invalid method given');
105
        }
106
107 1
        $instance = clone($this);
108 1
        $instance->setObjectProperty($instance->getInnerObject()->method, 'value', $method);
109
110 1
        return $instance;
111
    }
112
113
    /**
114
     * Retrieves the URI instance.
115
     *
116
     * @return UriInterface
117
     */
118 2
    public function getUri(): UriInterface
119
    {
120 2
        if (!$this->uri instanceof Uri) {
121
            $this->uri = new Uri($this->getInnerObject()->url);
0 ignored issues
show
Bug introduced by
$this->getInnerObject()->url of type Aura\Web\Request\Url is incompatible with the type null|string expected by parameter $uri of DMT\Aura\Psr\Message\Uri::__construct(). ( Ignorable by Annotation )

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

121
            $this->uri = new Uri(/** @scrutinizer ignore-type */ $this->getInnerObject()->url);
Loading history...
122
        }
123
124 2
        return $this->uri;
125
    }
126
127
    /**
128
     * Returns an instance with the provided URI.
129
     *
130
     * @param UriInterface $uri
131
     * @param bool $preserveHost
132
     * @return static
133
     */
134 4
    public function withUri(UriInterface $uri, $preserveHost = false): self
135
    {
136 4
        if (!$uri instanceof Uri) {
137
            $uri = (new UriFactory())->createUri((string)$uri);
138
        }
139
140 4
        $instance = clone($this);
141 4
        $instance->uri = $uri;
142 4
        $instance->setObjectProperty($instance->getInnerObject(), 'url', $uri->getInnerObject());
143
144 4
        if (!$preserveHost || !$this->hasHeader('host')) {
145 4
            return $instance->updateHostFromUri();
146
        }
147
148 1
        return $instance;
149
    }
150
151
    /**
152
     * Update the host whilst setting a new URI.
153
     *
154
     * @return static
155
     */
156 4
    protected function updateHostFromUri(): self
157
    {
158 4
        $host = $this->uri->getHost();
159 4
        $port = $this->uri->getPort();
160
161 4
        if ($host === '') {
162 2
            return $this;
163
        }
164
165 3
        if ($port !== null) {
166
            $host .= ':' . $port;
167
        }
168
169 3
        return $this->withHeader('host', $host);
170
    }
171
}
172