Passed
Pull Request — master (#56)
by Yasin
04:06 queued 01:57
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
8
/**
9
 *
10
 */
11
class Request extends Message implements \Psr\Http\Message\RequestInterface
12
{
13
    private $method;
14
   
15
    private $requestTarget;
16
    
17
    private $uri;
18
19
    /**
20
     * Default Constructor
21
     * @param string $method
22
     * @param string|\Psr\Http\Message\UriInterface $uri
23
     * @param array  $headers
24
     * @param mixed $body
25
     * @param string $version
26
     */
27
    public function __construct(
28
        $method,
29
        $uri,
30
        array $headers = [],
31
        $body = null,
32
        $version = '1.1'
33
    ) {
34
        if (!($uri instanceof \Psr\Http\Message\UriInterface)) {
35
            $uri = \One\createUriFromString($uri);
36
        }
37
        $this->method = strtoupper($method);
38
        $this->uri    = $uri;
39
        $this->setHeaders($headers);
40
        $this->protocol = $version;
41
        if (!$this->hasHeader('Host')) {
42
            $this->updateHostFromUri();
43
        }
44
        if ($body !== '' && $body !== null) {
45
            $this->stream = \One\stream_for($body);
46
        }
47
    }
48
    /**
49
     * @inheritDoc
50
     */
51
    public function getRequestTarget()
52
    {
53
        if ($this->requestTarget !== null) {
54
            return $this->requestTarget;
55
        }
56
        $target = $this->uri->getPath();
57
        if ($target == '') {
58
            $target = '/';
59
        }
60
        if ($this->uri->getQuery() != '') {
61
            $target .= '?' . $this->uri->getQuery();
62
        }
63
        return $target;
64
    }
65
    /**
66
     * @inheritDoc
67
     */
68
    public function withRequestTarget($requestTarget)
69
    {
70
        if (preg_match('#\s#', $requestTarget)) {
71
            throw new \InvalidArgumentException(
72
                'Invalid request target provided; cannot contain whitespace'
73
            );
74
        }
75
        $new                = clone $this;
76
        $new->requestTarget = $requestTarget;
77
        return $new;
78
    }
79
    /**
80
     * @inheritDoc
81
     */
82
    public function getMethod()
83
    {
84
        return $this->method;
85
    }
86
    /**
87
     * @inheritDoc
88
     */
89
    public function withMethod($method)
90
    {
91
        $new         = clone $this;
92
        $new->method = strtoupper($method);
93
        return $new;
94
    }
95
    /**
96
     * @inheritDoc
97
     */
98
    public function getUri()
99
    {
100
        return $this->uri;
101
    }
102
    /**
103
     * @inheritDoc
104
     */
105
    public function withUri(\Psr\Http\message\UriInterface $uri, $preserveHost = false)
106
    {
107
        if ($uri === $this->uri) {
108
            return $this;
109
        }
110
        $new      = clone $this;
111
        $new->uri = $uri;
112
        if (!$preserveHost) {
113
            $new->updateHostFromUri();
114
        }
115
        return $new;
116
    }
117
    /**
118
     * Ensure Host is the first header
119
     * See: http://tools.ietf.org/html/rfc7230#section-5.4
120
     */
121
    private function updateHostFromUri()
122
    {
123
        $host = $this->uri->getHost();
124
        if ($host == '') {
125
            return;
126
        }
127
        if (($port = $this->uri->getPort()) !== null) {
128
            $host .= ':' . $port;
129
        }
130
        if (isset($this->headerNames['host'])) {
131
            $header = $this->headerNames['host'];
132
        } else {
133
            $header                    = 'Host';
134
            $this->headerNames['host'] = 'Host';
135
        }
136
       
137
        $this->headers = [$header => [$host]] + $this->headers;
138
    }
139
}
140