Passed
Push — master ( 4b3582...7f619e )
by Julien
06:12
created

StatusCode   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 159
dl 0
loc 186
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Http;
13
14
/**
15
 * According to Wikipedia List of HTTP status codes
16
 *
17
 * Example:
18
 * ```php
19
 *  StatusCode::getMessage[StatusCode::OK] // 'OK'
20
 *  StatusCode::getMessage[200] // 'OK'
21
 *  StatusCode::$messages[200] // 'OK'
22
 *  StatusCode::OK // 200
23
 * ```
24
 *
25
 * @link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
26
 */
27
class StatusCode
28
{
29
    public const int CONTINUE = 100;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_CONTINUE, expecting '=' on line 29 at column 21
Loading history...
30
    public const int SWITCHING_PROTOCOLS = 101;
31
    public const int PROCESSING = 102;
32
    public const int OK = 200;
33
    public const int CREATED = 201;
34
    public const int ACCEPTED = 202;
35
    public const int NON_AUTHORITATIVE_INFORMATION = 203;
36
    public const int NO_CONTENT = 204;
37
    public const int RESET_CONTENT = 205;
38
    public const int PARTIAL_CONTENT = 206;
39
    public const int MULTI_STATUS = 207;
40
    public const int ALREADY_REPORTED = 208;
41
    public const int IM_USED = 226;
42
    public const int MULTIPLE_CHOICES = 300;
43
    public const int MOVED_PERMANENTLY = 301;
44
    public const int FOUND = 302;
45
    public const int SEE_OTHER = 303;
46
    public const int NOT_MODIFIED = 304;
47
    public const int USE_PROXY = 305;
48
    public const int SWITCH_PROXY = 306;
49
    public const int TEMPORARY_REDIRECT = 307;
50
    public const int PERMANENT_REDIRECT = 308;
51
    public const int BAD_REQUEST = 400;
52
    public const int UNAUTHORIZED = 401;
53
    public const int PAYMENT_REQUIRED = 402;
54
    public const int FORBIDDEN = 403;
55
    public const int NOT_FOUND = 404;
56
    public const int METHOD_NOT_ALLOWED = 405;
57
    public const int NOT_ACCEPTABLE = 406;
58
    public const int PROXY_AUTHENTICATION_REQUIRED = 407;
59
    public const int REQUEST_TIMEOUT = 408;
60
    public const int CONFLICT = 409;
61
    public const int GONE = 410;
62
    public const int LENGTH_REQUIRED = 411;
63
    public const int PRECONDITION_FAILED = 412;
64
    public const int REQUEST_ENTITY_TOO_LARGE = 413;
65
    public const int REQUEST_URI_TOO_LONG = 414;
66
    public const int UNSUPPORTED_MEDIA_TYPE = 415;
67
    public const int REQUESTED_RANGE_NOT_SATISFIABLE = 416;
68
    public const int EXPECTATION_FAILED = 417;
69
    public const int IM_A_TEAPOT = 418;
70
    public const int AUTHENTICATION_TIMEOUT = 419;
71
    public const int METHOD_FAILURE = 420;
72
    public const int UNPROCESSABLE_ENTITY = 422;
73
    public const int LOCKED = 423;
74
    public const int FAILED_DEPENDENCY = 424;
75
    public const int UPGRADE_REQUIRED = 426;
76
    public const int PRECONDITION_REQUIRED = 428;
77
    public const int TOO_MANY_REQUESTS = 429;
78
    public const int REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
79
    public const int LOGIN_TIMEOUT = 440;
80
    public const int NO_RESPONSE = 444;
81
    public const int RETRY_WITH = 449;
82
    public const int BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS = 450;
83
    public const int UNAVAILABLE_FOR_LEGAL_REASONS = 451;
84
    public const int REQUEST_HEADER_TOO_LARGE = 494;
85
    public const int CERT_ERROR = 495;
86
    public const int NO_CERT = 496;
87
    public const int HTTP_TO_HTTPS = 497;
88
    public const int TOKEN_EXPIREDINVALID = 498;
89
    public const int CLIENT_CLOSED_REQUEST = 499;
90
    public const int INTERNAL_SERVER_ERROR = 500;
91
    public const int NOT_IMPLEMENTED = 501;
92
    public const int BAD_GATEWAY = 502;
93
    public const int SERVICE_UNAVAILABLE = 503;
94
    public const int GATEWAY_TIMEOUT = 504;
95
    public const int HTTP_VERSION_NOT_SUPPORTED = 505;
96
    public const int VARIANT_ALSO_NEGOTIATES = 506;
97
    public const int INSUFFICIENT_STORAGE = 507;
98
    public const int LOOP_DETECTED = 508;
99
    public const int BANDWIDTH_LIMIT_EXCEEDED = 509;
100
    public const int NOT_EXTENDED = 510;
101
    public const int NETWORK_AUTHENTICATION_REQUIRED = 511;
102
    public const int NETWORK_READ_TIMEOUT_ERROR = 598;
103
    public const int NETWORK_CONNECT_TIMEOUT_ERROR = 599;
104
    
105
    public const int FATAL_ERROR = 500;
106
    public const int MAINTENANCE = 503;
107
    public const int OVERLOADED = 503;
108
    public const int BUSY = 503;
109
    
110
    public static array $messages = [
111
        self::CONTINUE => 'Continue',
112
        self::SWITCHING_PROTOCOLS => 'Switching Protocols',
113
        self::PROCESSING => 'Processing',
114
        self::OK => 'OK',
115
        self::CREATED => 'Created',
116
        self::ACCEPTED => 'Accepted',
117
        self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
118
        self::NO_CONTENT => 'No Content',
119
        self::RESET_CONTENT => 'Reset Content',
120
        self::PARTIAL_CONTENT => 'Partial Content',
121
        self::MULTI_STATUS => 'Multi-Status',
122
        self::ALREADY_REPORTED => 'Already Reported',
123
        self::IM_USED => 'IM Used',
124
        self::MULTIPLE_CHOICES => 'Multiple Choices',
125
        self::MOVED_PERMANENTLY => 'Moved Permanently',
126
        self::FOUND => 'Found',
127
        self::SEE_OTHER => 'See Other',
128
        self::NOT_MODIFIED => 'Not Modified',
129
        self::USE_PROXY => 'Use Proxy',
130
        self::SWITCH_PROXY => 'Switch Proxy',
131
        self::TEMPORARY_REDIRECT => 'Temporary Redirect',
132
        self::PERMANENT_REDIRECT => 'Permanent Redirect',
133
        self::BAD_REQUEST => 'Bad Request',
134
        self::UNAUTHORIZED => 'Unauthorized',
135
        self::PAYMENT_REQUIRED => 'Payment Required',
136
        self::FORBIDDEN => 'Forbidden',
137
        self::NOT_FOUND => 'Not Found',
138
        self::METHOD_NOT_ALLOWED => 'Method Not Allowed',
139
        self::NOT_ACCEPTABLE => 'Not Acceptable',
140
        self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
141
        self::REQUEST_TIMEOUT => 'Request Timeout',
142
        self::CONFLICT => 'Conflict',
143
        self::GONE => 'Gone',
144
        self::LENGTH_REQUIRED => 'Length Required',
145
        self::PRECONDITION_FAILED => 'Precondition Failed',
146
        self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
147
        self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
148
        self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
149
        self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
150
        self::EXPECTATION_FAILED => 'Expectation Failed',
151
        self::IM_A_TEAPOT => 'I\'m a teapot',
152
        self::AUTHENTICATION_TIMEOUT => 'Authentication Timeout',
153
        self::METHOD_FAILURE => 'Method Failure',
154
        self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity',
155
        self::LOCKED => 'Locked',
156
        self::FAILED_DEPENDENCY => 'Failed Dependency',
157
        self::UPGRADE_REQUIRED => 'Upgrade Required',
158
        self::PRECONDITION_REQUIRED => 'Precondition Required',
159
        self::TOO_MANY_REQUESTS => 'Too Many Requests',
160
        self::REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large',
161
        self::LOGIN_TIMEOUT => 'Login Timeout',
162
        self::NO_RESPONSE => 'No Response',
163
        self::RETRY_WITH => 'Retry With',
164
        self::BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS => 'Blocked by Windows Parental Controls',
165
        self::UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
166
        self::REQUEST_HEADER_TOO_LARGE => 'Request Header Too Large',
167
        self::CERT_ERROR => 'Cert Error',
168
        self::NO_CERT => 'No Cert',
169
        self::HTTP_TO_HTTPS => 'HTTP to HTTPS',
170
        self::TOKEN_EXPIREDINVALID => 'Token expired/invalid',
171
        self::CLIENT_CLOSED_REQUEST => 'Client Closed Request',
172
        self::INTERNAL_SERVER_ERROR => 'Internal Server Error',
173
        self::NOT_IMPLEMENTED => 'Not Implemented',
174
        self::BAD_GATEWAY => 'Bad Gateway',
175
        self::SERVICE_UNAVAILABLE => 'Service Unavailable',
176
        self::GATEWAY_TIMEOUT => 'Gateway Timeout',
177
        self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
178
        self::VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
179
        self::INSUFFICIENT_STORAGE => 'Insufficient Storage',
180
        self::LOOP_DETECTED => 'Loop Detected',
181
        self::BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded',
182
        self::NOT_EXTENDED => 'Not Extended',
183
        self::NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required',
184
        self::NETWORK_READ_TIMEOUT_ERROR => 'Network read timeout error',
185
        self::NETWORK_CONNECT_TIMEOUT_ERROR => 'Network connect timeout error',
186
    ];
187
    
188
    /**
189
     * Get the HTTP status message for the specified HTTP status code
190
     * getMessage(200) -> 'OK'
191
     */
192 1
    public static function getMessage(int $code): ?string
193
    {
194 1
        return self::$messages[$code] ?? null;
195
    }
196
    
197
    /**
198
     * Get the HTTP code from the specified HTTP status message
199
     * getCode('OK') -> 200
200
     */
201 1
    public static function getCode(string $message): ?int
202
    {
203 1
        return array_flip(self::$messages)[$message] ?? null;
204
    }
205
    
206
    /**
207
     * Get the HTTP code from the specified HTTP status message
208
     * getStatus(200) -> '200 OK'
209
     */
210 1
    public static function getStatus(int $code): ?string
211
    {
212 1
        return trim($code . ' ' . self::getMessage($code));
213
    }
214
}
215