Completed
Push — master ( 01f593...6661e3 )
by Henry
10:15
created

Header   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 138
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 1
A add() 0 13 3
A remove() 0 12 3
A isSent() 0 4 1
A getArray() 0 4 1
A statusCode() 0 4 1
A doRedirect() 0 4 1
A contentType() 0 4 1
1
<?php
2
namespace Redaxscript;
3
4
/**
5
 * children class to add and remove the header
6
 *
7
 * @since 3.3.0
8
 *
9
 * @package Redaxscript
10
 * @category Header
11
 * @author Henry Ruhs
12
 */
13
14
class Header
15
{
16
	/**
17
	 * init the class
18
	 *
19
	 * @since 3.3.0
20
	 */
21
22
	public static function init()
23
	{
24
		self::add(
25
		[
26
			'X-Content-Type-Options: nosniff',
27
			'X-Frame-Options: sameorigin',
28
			'X-XSS-Protection: 1; mode=block'
29
		]);
30
		self::remove('X-Powered-By');
31
	}
32
33
	/**
34
	 * add the header
35
	 *
36
	 * @since 3.3.0
37
	 *
38
	 * @param string|array $header
39
	 * @param bool $replace
40
	 *
41
	 * @return bool
42
	 */
43
44
	public static function add($header = null, bool $replace = true) : bool
45
	{
46
		if (!self::isSent())
47
		{
48
			foreach ((array)$header as $value)
49
			{
50
				header($value, $replace);
51
			}
52
			return true;
53
		}
54
		return false;
55
56
	}
57
58
	/**
59
	 * remove the header
60
	 *
61
	 * @since 3.3.0
62
	 *
63
	 * @param string|array $header
64
	 *
65
	 * @return bool
66
	 */
67
68
	public static function remove($header = null) : bool
69
	{
70
		if (!self::isSent())
71
		{
72
			foreach ((array)$header as $value)
73
			{
74
				header_remove($value);
75
			}
76
			return true;
77
		}
78
		return false;
79
	}
80
81
	/**
82
	 * is header sent
83
	 *
84
	 * @since 3.3.0
85
	 *
86
	 * @return bool
87
	 */
88
89
	public static function isSent() : bool
90
	{
91
		return headers_sent();
92
	}
93
94
	/**
95
	 * get the header array
96
	 *
97
	 * @since 3.3.0
98
	 *
99
	 * @return array
100
	 */
101
102
	public static function getArray() : array
103
	{
104
		return headers_list();
105
	}
106
107
	/**
108
	 * status code
109
	 *
110
	 * @since 3.3.0
111
	 *
112
	 * @param int $code
113
	 *
114
	 * @return int
115
	 */
116
117
	public static function statusCode(int $code = null) : int
118
	{
119
		return http_response_code($code);
120
	}
121
122
	/**
123
	 * redirect to location
124
	 *
125
	 * @since 3.3.0
126
	 *
127
	 * @param string $location
128
	 *
129
	 * @return bool
130
	 */
131
132
	public static function doRedirect(string $location = null) : bool
133
	{
134
		return self::add('Location: ' . $location);
135
	}
136
137
	/**
138
	 * content type
139
	 *
140
	 * @since 3.3.0
141
	 *
142
	 * @param string $type
143
	 *
144
	 * @return bool
145
	 */
146
147
	public static function contentType(string $type = null) : bool
148
	{
149
		return self::add('Content-Type: ' . $type);
150
	}
151
}
152