Response::getStatusCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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