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

Response   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 130
ccs 18
cts 18
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getStatusCode() 0 4 1
A setStatus() 0 11 3
A withStatus() 0 6 1
A getReasonPhrase() 0 4 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\ResponseInterface;
14
use Psr\Http\Message\StreamInterface;
15
16
/**
17
 * The response class.
18
 *
19
 * @see http://www.php-fig.org/psr/psr-7/
20
 * @since 1.0.0
21
 */
22
class Response extends Message implements ResponseInterface
23
{
24
	/** @var int The status code. */
25
	private $statusCode;
26
27
	/** @var string The reason phrase. */
28
	private $reasonPhrase;
29
30
	/** @var array The reason phrases */
31
	private static $reasonPhrases = [
32
		100 => 'Continue',
33
		101 => 'Switching Protocols',
34
		102 => 'Processing',
35
		200 => 'OK',
36
		201 => 'Created',
37
		202 => 'Accepted',
38
		203 => 'Non-Authoritative Information',
39
		204 => 'No Content',
40
		205 => 'Reset Content',
41
		206 => 'Partial Content',
42
		207 => 'Multi-status',
43
		208 => 'Already Reported',
44
		300 => 'Multiple Choices',
45
		301 => 'Moved Permanently',
46
		302 => 'Found',
47
		303 => 'See Other',
48
		304 => 'Not Modified',
49
		305 => 'Use Proxy',
50
		306 => 'Switch Proxy',
51
		307 => 'Temporary Redirect',
52
		400 => 'Bad Request',
53
		401 => 'Unauthorized',
54
		402 => 'Payment Required',
55
		403 => 'Forbidden',
56
		404 => 'Not Found',
57
		405 => 'Method Not Allowed',
58
		406 => 'Not Acceptable',
59
		407 => 'Proxy Authentication Required',
60
		408 => 'Request Time-out',
61
		409 => 'Conflict',
62
		410 => 'Gone',
63
		411 => 'Length Required',
64
		412 => 'Precondition Failed',
65
		413 => 'Request Entity Too Large',
66
		414 => 'Request-URI Too Large',
67
		415 => 'Unsupported Media Type',
68
		416 => 'Requested range not satisfiable',
69
		417 => 'Expectation Failed',
70
		418 => 'I\'m a teapot',
71
		422 => 'Unprocessable Entity',
72
		423 => 'Locked',
73
		424 => 'Failed Dependency',
74
		425 => 'Unordered Collection',
75
		426 => 'Upgrade Required',
76
		428 => 'Precondition Required',
77
		429 => 'Too Many Requests',
78
		431 => 'Request Header Fields Too Large',
79
		500 => 'Internal Server Error',
80
		501 => 'Not Implemented',
81
		502 => 'Bad Gateway',
82
		503 => 'Service Unavailable',
83
		504 => 'Gateway Time-out',
84
		505 => 'HTTP Version not supported',
85
		506 => 'Variant Also Negotiates',
86
		507 => 'Insufficient Storage',
87
		508 => 'Loop Detected',
88
		511 => 'Network Authentication Required',
89
	];
90
91
	/**
92
	 * Construct a Response object with the given status code, reason phrase, version, headers & body.
93
	 *
94
	 * @param int $statusCode
95
	 * @param string $reasonPhrase = ''
96
	 * @param string $version = self::DEFAULT_VERSION
97
	 * @param array $headers = []
98
	 * @param StreamInterface|null $body = null
99
	 */
100 6
	public function __construct($statusCode, $reasonPhrase = '', $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null)
101
	{
102 6
		parent::__construct($version, $headers, $body);
103
104 6
		$this->setStatus($statusCode, $reasonPhrase);
105 6
	}
106
107
	/**
108
	 * {@inheritdoc}
109
	 */
110 5
	public function getStatusCode()
111
	{
112 5
		return $this->statusCode;
113
	}
114
115
	/**
116
	 * Set the status.
117
	 *
118
	 * @param int $statusCode
119
	 * @param string $reasonPhrase = ''
120
	 * @return $this
121
	 */
122 6
	private function setStatus($statusCode, $reasonPhrase = '')
123
	{
124 6
		if ($reasonPhrase === '' && isset(self::$reasonPhrases[$statusCode])) {
125 6
			$reasonPhrase = self::$reasonPhrases[$statusCode];
126 6
		}
127
128 6
		$this->statusCode = $statusCode;
129 6
		$this->reasonPhrase = $reasonPhrase;
130
131 6
		return $this;
132
	}
133
134
	/**
135
	 * {@inheritdoc}
136
	 */
137 1
	public function withStatus($statusCode, $reasonPhrase = '')
138
	{
139 1
		$result = clone $this;
140
141 1
		return $result->setStatus($statusCode, $reasonPhrase);
142
	}
143
144
	/**
145
	 * {@inheritdoc}
146
	 */
147 5
	public function getReasonPhrase()
148
	{
149 5
		return $this->reasonPhrase;
150
	}
151
}
152