Passed
Pull Request — master (#41)
by kenny
02:45
created

Request::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 8
nop 5
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace One\Http;
4
5
use One\Http\Message;
6
use One\Uri;
7
use function One\createUriFromString;
8
use function One\stream_for;
9
10
/**
11
 *
12
 */
13
class Request extends Message implements \Psr\Http\Message\RequestInterface
14
{
15
    private $method;
16
   
17
    private $requestTarget;
18
    
19
    private $uri;
20
21
    public function __construct(
22
        $method,
23
        $uri,
24
        array $headers = [],
25
        $body = null,
26
        $version = '1.1'
27
    ) {
28
        if (!($uri instanceof UriInterface)) {
0 ignored issues
show
Bug introduced by
The type One\Http\UriInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
            $uri = createUriFromString($uri);
30
        }
31
        $this->method = strtoupper($method);
32
        $this->uri    = $uri;
33
        $this->setHeaders($headers);
34
        $this->protocol = $version;
35
        if (!$this->hasHeader('Host')) {
36
            $this->updateHostFromUri();
37
        }
38
        if ($body !== '' && $body !== null) {
39
            $this->stream = stream_for($body);
40
        }
41
    }
42
43
    public function getRequestTarget()
44
    {
45
        if ($this->requestTarget !== null) {
46
            return $this->requestTarget;
47
        }
48
        $target = $this->uri->getPath();
49
        if ($target == '') {
50
            $target = '/';
51
        }
52
        if ($this->uri->getQuery() != '') {
53
            $target .= '?' . $this->uri->getQuery();
54
        }
55
        return $target;
56
    }
57
    public function withRequestTarget($requestTarget)
58
    {
59
        if (preg_match('#\s#', $requestTarget)) {
60
            throw new InvalidArgumentException(
0 ignored issues
show
Bug introduced by
The type One\Http\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
61
                'Invalid request target provided; cannot contain whitespace'
62
            );
63
        }
64
        $new                = clone $this;
65
        $new->requestTarget = $requestTarget;
66
        return $new;
67
    }
68
    public function getMethod()
69
    {
70
        return $this->method;
71
    }
72
    public function withMethod($method)
73
    {
74
        $new         = clone $this;
75
        $new->method = strtoupper($method);
76
        return $new;
77
    }
78
    public function getUri()
79
    {
80
        return $this->uri;
81
    }
82
    public function withUri(\Psr\Http\message\UriInterface $uri, $preserveHost = false)
83
    {
84
        if ($uri === $this->uri) {
0 ignored issues
show
introduced by
The condition $uri === $this->uri is always false.
Loading history...
85
            return $this;
86
        }
87
        $new      = clone $this;
88
        $new->uri = $uri;
89
        if (!$preserveHost) {
90
            $new->updateHostFromUri();
91
        }
92
        return $new;
93
    }
94
95
    private function updateHostFromUri()
96
    {
97
        $host = $this->uri->getHost();
98
        if ($host == '') {
99
            return;
100
        }
101
        if (($port = $this->uri->getPort()) !== null) {
102
            $host .= ':' . $port;
103
        }
104
        if (isset($this->headerNames['host'])) {
105
            $header = $this->headerNames['host'];
106
        } else {
107
            $header                    = 'Host';
108
            $this->headerNames['host'] = 'Host';
109
        }
110
        // Ensure Host is the first header.
111
        // See: http://tools.ietf.org/html/rfc7230#section-5.4
112
        $this->headers = [$header => [$host]] + $this->headers;
113
    }
114
}
115