Passed
Push — master ( cea48f...3c52fb )
by Jean-Christophe
05:56
created

UResponse   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 35.48%

Importance

Changes 0
Metric Value
wmc 22
eloc 31
dl 0
loc 197
ccs 22
cts 62
cp 0.3548
rs 10
c 0
b 0
f 0

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 setContentType() 0 5 2
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
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
/**
6
 * Http Response utilities
7
 * Ubiquity\utils\http$UResponse
8
 * This class is part of Ubiquity
9
 *
10
 * @author jcheron <[email protected]>
11
 * @version 1.0.2
12
 *
13
 */
14
class UResponse {
15
	public static $headers = [ ];
16
17
	/**
18
	 * Send a raw HTTP header
19
	 *
20
	 * @param string $headerField
21
	 *        	the header field
22
	 * @param string $value
23
	 *        	the header value
24
	 * @param boolean $replace
25
	 *        	The optional replace parameter indicates whether the header should replace a previous similar header
26
	 * @param int $responseCode
27
	 *        	Forces the HTTP response code to the specified value
28
	 */
29 2
	public static function header($headerField, $value, $replace = null, $responseCode = null) {
30 2
		self::$headers [trim ( $headerField )] = trim ( $value );
31 2
		\header ( trim ( $headerField ) . ": " . trim ( $value ), $replace, $responseCode );
32 2
	}
33
34
	/**
35
	 *
36
	 * @param string $headerField
37
	 * @param mixed $values
38
	 */
39
	private static function _headerArray($headerField, $values) {
40
		if (\is_array ( $values )) {
41
			$values = \implode ( ", ", $values );
42
		}
43
		self::header ( $headerField, $values );
44
	}
45
46
	/**
47
	 * Sets header content-type
48
	 *
49
	 * @param string $contentType
50
	 * @param string $encoding
51
	 */
52 1
	public static function setContentType($contentType, $encoding = null) {
53 1
		$value = $contentType;
54 1
		if (isset ( $encoding ))
55 1
			$value .= ' ;' . $encoding;
56 1
		self::header ( 'content-type', $value );
57 1
	}
58
59
	/**
60
	 * Forces the disabling of the browser cache
61
	 */
62 1
	public static function noCache() {
63 1
		self::header ( 'Cache-Control', 'no-cache, must-revalidate' );
64 1
		self::header ( 'Expires', 'Sat, 26 Jul 1997 05:00:00 GMT' );
65 1
	}
66
67
	/**
68
	 * Checks if or where headers have been sent
69
	 *
70
	 * @param string $file
71
	 *        	If the optional file andline parameters are set,headers_sent will put the PHP source file nameand line number where output started in the fileand line variables.
72
	 * @param int $line
73
	 *        	The line number where the output started.
74
	 * @return boolean
75
	 */
76
	public static function isSent(&$file = null, &$line = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
	public static function isSent(/** @scrutinizer ignore-unused */ &$file = null, &$line = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $line is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
	public static function isSent(&$file = null, /** @scrutinizer ignore-unused */ &$line = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
		return \headers_sent ();
78
	}
79
80
	/**
81
	 * Sets the response content-type to application/json
82
	 */
83 1
	public static function asJSON() {
84 1
		self::header ( 'Content-Type', 'application/json' );
85 1
	}
86
87
	/**
88
	 * Tests if response content-type is application/json
89
	 * Only Works if UResponse has been used for setting headers
90
	 *
91
	 * @return boolean
92
	 */
93 1
	public static function isJSON() {
94 1
		return isset ( self::$headers ["Content-Type"] ) && self::$headers ["Content-Type"] === 'application/json';
95
	}
96
97
	/**
98
	 * Sets the response content-type to text/html
99
	 *
100
	 * @param string $encoding
101
	 *        	default: utf-8
102
	 */
103
	public static function asHtml($encoding = 'utf-8') {
104
		self::setContentType ( 'text/html', $encoding );
105
	}
106
107
	/**
108
	 * Sets the response content-type to application/xml
109
	 *
110
	 * @param string $encoding
111
	 *        	default: utf-8
112
	 */
113 1
	public static function asXml($encoding = 'utf-8') {
114 1
		self::setContentType ( 'application/xml', $encoding );
115 1
	}
116
117
	/**
118
	 * Sets the response content-type to plain/text
119
	 *
120
	 * @param string $encoding
121
	 *        	default: utf-8
122
	 */
123
	public static function asText($encoding = 'utf-8') {
124
		self::setContentType ( 'plain/text', $encoding );
125
	}
126
127
	/**
128
	 * Sets the Accept header
129
	 *
130
	 * @param string $value
131
	 *        	one of Http accept values
132
	 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values
133
	 */
134
	public static function setAccept($value) {
135
		self::header ( 'Accept', $value );
136
	}
137
138
	/**
139
	 * Enables CORS
140
	 *
141
	 * @param string $origin
142
	 *        	The allowed origin (default: '*')
143
	 * @param string $methods
144
	 *        	The allowed methods (default: 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
145
	 * @param string $headers
146
	 *        	The allowed headers (default: 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
147
	 * @since Ubiquity 2.0.11
148
	 */
149
	public static function enableCors($origin = '*', $methods = 'GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers = 'X-Requested-With, Content-Type, Accept, Origin, Authorization') {
150
		self::setAccessControlOrigin ( $origin );
151
		self::setAccessControlMethods ( $methods );
152
		self::setAccessControlHeaders ( $headers );
153
	}
154
155
	/**
156
	 * Sets the Access-Control-Allow-Origin field value
157
	 * Only a single origin can be specified.
158
	 *
159
	 * @param string $origin
160
	 */
161
	public static function setAccessControlOrigin($origin = '*') {
162
		self::header ( 'Access-Control-Allow-Origin', $origin );
163
		if ($origin !== '*') {
164
			self::header ( 'Vary', 'Origin' );
165
		}
166
	}
167
168
	/**
169
	 * Sets the Access-Control-Allow-Methods field value
170
	 *
171
	 * @param string|array $methods
172
	 */
173
	public static function setAccessControlMethods($methods) {
174
		self::_headerArray ( 'Access-Control-Allow-Methods', $methods );
175
	}
176
177
	/**
178
	 * Sets the Access-Control-Allow-Headers field value
179
	 *
180
	 * @param string|array $headers
181
	 */
182
	public static function setAccessControlHeaders($headers) {
183
		self::_headerArray ( 'Access-Control-Allow-Headers', $headers );
184
	}
185
186
	/**
187
	 * Set the Authorization header field
188
	 *
189
	 * @param string $authorization
190
	 */
191
	public static function setAuthorization($authorization) {
192
		self::header ( 'Authorization', $authorization );
193
	}
194
195
	/**
196
	 * Sets the response code
197
	 *
198
	 * @param int $value
199
	 */
200
	public static function setResponseCode($value) {
201
		\http_response_code ( $value );
202
	}
203
204
	/**
205
	 * Get the response code
206
	 *
207
	 * @return int
208
	 */
209
	public static function getResponseCode() {
210
		return \http_response_code ();
211
	}
212
}
213