Request::setRequestTarget()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
10
namespace miBadger\Http;
11
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\StreamInterface;
14
use Psr\Http\Message\UriInterface;
15
16
/**
17
 * The request class.
18
 *
19
 * @see http://www.php-fig.org/psr/psr-7/
20
 * @since 1.0.0
21
 */
22
class Request extends Message implements RequestInterface
23
{
24
	/** @var string The request target. */
25
	private $requestTarget;
26
27
	/** @var string The method. */
28
	private $method;
29
30
	/** @var UriInterface The URI. */
31
	private $uri;
32
33
	/**
34
	 * Construct a Request object with the given method, uri, version, headers & body.
35
	 *
36
	 * @param string $method
37
	 * @param UriInterface $uri
38
	 * @param string $version = self::DEFAULT_VERSION
39
	 * @param array $headers = []
40
	 * @param StreamInterface|null $body = null
41
	 */
42 26
	public function __construct($method, UriInterface $uri, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null)
43
	{
44 26
		parent::__construct($version, $headers, $body);
45
46 26
		$this->setMethod($method);
47 26
		$this->setUri($uri);
48 26
	}
49
50
	/**
51
	 * {@inheritdoc}
52
	 */
53 2
	public function getRequestTarget()
54
	{
55 2
		if (isset($this->requestTarget)) {
56 1
			return $this->requestTarget;
57
		}
58
59 1
		$result = $this->getUri()->getPath() ?: URI::DELIMITER_PATH;
60
61 1
		if ($this->getUri()->getQuery()) {
62 1
			$result .= URI::DELIMITER_QUERY . $this->getUri()->getQuery();
63
		}
64
65 1
		return $result;
66
	}
67
68
	/**
69
	 * Set the request target.
70
	 *
71
	 * @param string $requestTarget
72
	 * @return $this
73
	 */
74 1
	private function setRequestTarget($requestTarget)
75
	{
76 1
		$this->requestTarget = $requestTarget;
77
78 1
		return $this;
79
	}
80
81
	/**
82
	 * {@inheritdoc}
83
	 */
84 1
	public function withRequestTarget($requestTarget)
85
	{
86 1
		$result = clone $this;
87
88 1
		return $result->setRequestTarget($requestTarget);
89
	}
90
91
	/**
92
	 * {@inheritdoc}
93
	 */
94 8
	public function getMethod()
95
	{
96 8
		return $this->method;
97
	}
98
99
	/**
100
	 * Set the method.
101
	 *
102
	 * @param string $method
103
	 * @return $this
104
	 */
105 26
	private function setMethod($method)
106
	{
107 26
		$this->method = $method;
108
109 26
		return $this;
110
	}
111
112
	/**
113
	 * {@inheritdoc}
114
	 */
115 1
	public function withMethod($method)
116
	{
117 1
		$result = clone $this;
118
119 1
		return $result->setMethod($method);
120
	}
121
122
	/**
123
	 * {@inheritdoc}
124
	 */
125 3
	public function getUri()
126
	{
127 3
		return $this->uri;
128
	}
129
130
	/**
131
	 * Set the uri.
132
	 *
133
	 * @param UriInterface $uri
134
	 * @param bool $preserveHost = false
135
	 * @return $this
136
	 */
137 26
	private function setUri(UriInterface $uri, $preserveHost = false)
138
	{
139 26
		$this->uri = $uri;
140
141 26
		if (!$preserveHost && ($host = $uri->getHost())) {
142 1
			if ($uri->getPort() !== null) {
143 1
				$host .= URI::DELIMITER_PORT . $uri->getPort();
144
			}
145
146 1
			$this->setHeader('Host', $host);
147
		}
148
149 26
		return $this;
150
	}
151
152
	/**
153
	 * {@inheritdoc}
154
	 */
155 1
	public function withUri(UriInterface $uri, $preserveHost = false)
156
	{
157 1
		$result = clone $this;
158
159 1
		return $result->setUri($uri, $preserveHost);
160
	}
161
}
162