Passed
Push — master ( 6a9989...c85e47 )
by Jean-Christophe
10:21
created

UResponse   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 36.07%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 22
eloc 31
c 1
b 1
f 0
dl 0
loc 184
ccs 22
cts 61
cp 0.3607
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setAccessControlMethods() 0 2 1
A getResponseCode() 0 2 1
A _headerArray() 0 5 2
A noCache() 0 3 1
A setAccessControlHeaders() 0 2 1
A asJSON() 0 2 1
A asXml() 0 2 1
A setAccessControlOrigin() 0 4 2
A header() 0 3 1
A setAuthorization() 0 2 1
A enableCors() 0 4 1
A setResponseCode() 0 2 1
A setAccept() 0 2 1
A isJSON() 0 2 2
A asText() 0 2 1
A asHtml() 0 2 1
A isSent() 0 2 1
A setContentType() 0 5 2
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * Http Response utilities
9
 * Ubiquity\utils\http$UResponse
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.1.1
14
 *
15
 */
16
class UResponse {
17
	public static $headers = [ ];
18
19
	/**
20
	 * Send a raw HTTP header
21
	 *
22
	 * @param string $headerField the header field
23
	 * @param string $value the header value
24
	 * @param boolean $replace The optional replace parameter indicates whether the header should replace a previous similar header
25
	 * @param int $responseCode Forces the HTTP response code to the specified value
26
	 */
27 2
	public static function header($headerField, $value, bool $replace = true, int $responseCode = null): void {
28 2
		self::$headers [trim ( $headerField )] = trim ( $value );
29 2
		Startup::getHttpInstance ()->header ( trim ( $headerField ), trim ( $value ), $replace, $responseCode );
30 2
	}
31
32
	/**
33
	 *
34
	 * @param string $headerField
35
	 * @param mixed $values
36
	 */
37
	private static function _headerArray($headerField, $values): void {
38
		if (\is_array ( $values )) {
39
			$values = \implode ( ', ', $values );
40
		}
41
		self::header ( $headerField, $values );
42
	}
43
44
	/**
45
	 * Sets header content-type
46
	 *
47
	 * @param string $contentType
48
	 * @param string $encoding
49
	 */
50 1
	public static function setContentType($contentType, $encoding = null): void {
51 1
		$value = $contentType;
52 1
		if (isset ( $encoding ))
53 1
			$value .= '; charset=' . $encoding;
54 1
		self::header ( 'Content-Type', $value, true );
55 1
	}
56
57
	/**
58
	 * Forces the disabling of the browser cache
59
	 */
60 1
	public static function noCache(): void {
61 1
		self::header ( 'Cache-Control', 'no-cache, must-revalidate' );
62 1
		self::header ( 'Expires', 'Sat, 26 Jul 1997 05:00:00 GMT' );
63 1
	}
64
65
	/**
66
	 * Checks if or where headers have been sent
67
	 *
68
	 * @param string $file If the optional file and line parameters are set,headers_sent will put the PHP source file nameand line number where output started in the fileand line variables.
69
	 * @param int $line The line number where the output started.
70
	 * @return boolean
71
	 */
72
	public static function isSent(&$file = null, &$line = null): bool {
73
		return Startup::getHttpInstance ()->headersSent ( $file, $line );
74
	}
75
76
	/**
77
	 * Sets the response content-type to application/json
78
	 */
79 1
	public static function asJSON(): void {
80 1
		self::header ( 'Content-Type', 'application/json' );
81 1
	}
82
83
	/**
84
	 * Tests if response content-type is application/json
85
	 * Only Works if UResponse has been used for setting headers
86
	 *
87
	 * @return boolean
88
	 */
89 1
	public static function isJSON(): bool {
90 1
		return isset ( self::$headers ['Content-Type'] ) && self::$headers ['Content-Type'] === 'application/json';
91
	}
92
93
	/**
94
	 * Sets the response content-type to text/html
95
	 *
96
	 * @param string $encoding default: utf-8
97
	 */
98
	public static function asHtml($encoding = 'utf-8'): void {
99
		self::setContentType ( 'text/html', $encoding );
100
	}
101
102
	/**
103
	 * Sets the response content-type to application/xml
104
	 *
105
	 * @param string $encoding default: utf-8
106
	 */
107 1
	public static function asXml($encoding = 'utf-8'): void {
108 1
		self::setContentType ( 'application/xml', $encoding );
109 1
	}
110
111
	/**
112
	 * Sets the response content-type to plain/text
113
	 *
114
	 * @param string $encoding default: utf-8
115
	 */
116
	public static function asText($encoding = 'utf-8'): void {
117
		self::setContentType ( 'plain/text', $encoding );
118
	}
119
120
	/**
121
	 * Sets the Accept header
122
	 *
123
	 * @param string $value one of Http accept values
124
	 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values
125
	 */
126
	public static function setAccept($value): void {
127
		self::header ( 'Accept', $value );
128
	}
129
130
	/**
131
	 * Enables CORS
132
	 *
133
	 * @param string $origin The allowed origin (default: '*')
134
	 * @param string $methods The allowed methods (default: 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
135
	 * @param string $headers The allowed headers (default: 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
136
	 * @since Ubiquity 2.0.11
137
	 */
138
	public static function enableCors($origin = '*', $methods = 'GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers = 'X-Requested-With, Content-Type, Accept, Origin, Authorization'): void {
139
		self::setAccessControlOrigin ( $origin );
140
		self::setAccessControlMethods ( $methods );
141
		self::setAccessControlHeaders ( $headers );
142
	}
143
144
	/**
145
	 * Sets the Access-Control-Allow-Origin field value
146
	 * Only a single origin can be specified.
147
	 *
148
	 * @param string $origin
149
	 */
150
	public static function setAccessControlOrigin($origin = '*'): void {
151
		self::header ( 'Access-Control-Allow-Origin', $origin );
152
		if ($origin !== '*') {
153
			self::header ( 'Vary', 'Origin' );
154
		}
155
	}
156
157
	/**
158
	 * Sets the Access-Control-Allow-Methods field value
159
	 *
160
	 * @param string|array $methods
161
	 */
162
	public static function setAccessControlMethods($methods): void {
163
		self::_headerArray ( 'Access-Control-Allow-Methods', $methods );
164
	}
165
166
	/**
167
	 * Sets the Access-Control-Allow-Headers field value
168
	 *
169
	 * @param string|array $headers
170
	 */
171
	public static function setAccessControlHeaders($headers): void {
172
		self::_headerArray ( 'Access-Control-Allow-Headers', $headers );
173
	}
174
175
	/**
176
	 * Set the Authorization header field
177
	 *
178
	 * @param string $authorization
179
	 */
180
	public static function setAuthorization($authorization): void {
181
		self::header ( 'Authorization', $authorization );
182
	}
183
184
	/**
185
	 * Sets the response code
186
	 *
187
	 * @param int $value
188
	 */
189
	public static function setResponseCode($value) {
190
		return \http_response_code ( $value );
191
	}
192
193
	/**
194
	 * Get the response code
195
	 *
196
	 * @return int
197
	 */
198
	public static function getResponseCode() {
199
		return \http_response_code ();
200
	}
201
}
202