Completed
Push — master ( 16c8a5...965897 )
by Michael
02:58
created

Request   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
c 1
b 0
f 1
lcom 2
cbo 2
dl 0
loc 140
ccs 41
cts 41
cp 1
rs 10

10 Methods

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