Passed
Pull Request — develop (#1)
by nguereza
02:57
created

HttpStatus::isSuccessful()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant PHP
7
 * Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 * Copyright (c) 2011 - 2017 rehyved.com
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file HttpStatus.php
35
 *
36
 *  The Http Status class
37
 *
38
 * An enumeration of possible HTTP status codes.
39
 * This class provides convenience methods to check the type
40
 * of status and retrieve the reason phrase for the status.
41
 *
42
 *  @package    Platine\Framework\Http\Client
43
 *  @author Platine Developers team
44
 *  @copyright  Copyright (c) 2020
45
 *  @license    http://opensource.org/licenses/MIT  MIT License
46
 *  @link   https://www.platine-php.com
47
 *  @version 1.0.0
48
 *  @filesource
49
 */
50
51
declare(strict_types=1);
52
53
namespace Platine\Framework\Http\Client;
54
55
use InvalidArgumentException;
56
57
/**
58
 * @class HttpStatus
59
 * @package Platine\Framework\Http\Client
60
 */
61
class HttpStatus
62
{
63
    // Informational 1xx:
64
    public const INFORMATIONAL = 100;
65
    public const CONTINUE = 100;
66
    public const SWITCHING_PROTOCOLS = 101;
67
68
    // Successful 2xx:
69
    public const SUCCESSFUL = 200;
70
    public const OK = 200;
71
    public const CREATED = 201;
72
    public const ACCEPTED = 202;
73
    public const NON_AUTHORITATIVE_INFORMATION = 203;
74
    public const NO_CONTENT = 204;
75
    public const RESET_CONTENT = 205;
76
    public const PARTIAL_CONTENT = 206;
77
78
    // Redirection 3xx:
79
    public const REDIRECTION = 300;
80
    public const MULTIPLE_CHOICES = 300;
81
    public const MOVED_PERMANENTLY = 301;
82
    public const FOUND = 302;
83
    public const SEE_OTHER = 303;
84
    public const NOT_MODIFIED = 304;
85
    public const USE_PROXY = 305;
86
    // Code 306 was used in a previous HTTP specification but no longer used but kept reserved.
87
    public const TEMPORARY_REDIRECT = 307;
88
89
    // Client Error 4xx:
90
    public const CLIENT_ERROR = 400;
91
    public const BAD_REQUEST = 400;
92
    public const UNAUTHORIZED = 401;
93
    public const PAYMENT_REQUIRED = 402;
94
    public const FORBIDDEN = 403;
95
    public const NOT_FOUND = 404;
96
    public const METHOD_NOT_ALLOWED = 405;
97
    public const NOT_ACCEPTABLE = 406;
98
    public const PROXY_AUTHENTICATION_REQUIRED = 407;
99
    public const REQUEST_TIMEOUT = 408;
100
    public const CONFLICT = 409;
101
    public const GONE = 410;
102
    public const LENGTH_REQUIRED = 411;
103
    public const PRECONDITION_FAILED = 412;
104
    public const REQUEST_ENTITY_TOO_LARGE = 413;
105
    public const REQUEST_URI_TOO_LONG = 414;
106
    public const UNSUPPORTED_MEDIA_TYPE = 415;
107
    public const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
108
    public const EXPECTATION_FAILED = 417;
109
110
    // Server Error 5xx:
111
    public const SERVER_ERROR = 500;
112
    public const INTERNAL_SERVER_ERROR = 500;
113
    public const NOT_IMPLEMENTED = 501;
114
    public const BAD_GATEWAY = 502;
115
    public const SERVICE_UNAVAILABLE = 503;
116
    public const GATEWAY_TIMEOUT = 504;
117
    public const HTTP_VERSION_NOT_SUPPORTED = 505;
118
    public const SERVER_ERROR_END = 600;
119
120
    public const REASON_PHRASES = [
121
        self::CONTINUE => 'Continue',
122
        self::SWITCHING_PROTOCOLS => 'Switching Protocols',
123
124
        self::OK => 'OK',
125
        self::CREATED => 'Created',
126
        self::ACCEPTED => 'Accepted',
127
        self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
128
        self::NO_CONTENT => 'No Content',
129
        self::RESET_CONTENT => 'Reset Content',
130
        self::PARTIAL_CONTENT => 'Partial Content',
131
132
        self::MULTIPLE_CHOICES => 'Multiple Choices',
133
        self::MOVED_PERMANENTLY => 'Moved Permanently',
134
        self::FOUND => 'Found',
135
        self::SEE_OTHER => 'See Other',
136
        self::NOT_MODIFIED => 'Not Modified',
137
        self::USE_PROXY => 'Use Proxy',
138
        self::TEMPORARY_REDIRECT => 'Temporary Redirect',
139
140
        self::BAD_REQUEST => 'Bad Request',
141
        self::UNAUTHORIZED => 'Unauthorized',
142
        self::PAYMENT_REQUIRED => 'Payment Required',
143
        self::FORBIDDEN => 'Forbidden',
144
        self::NOT_FOUND => 'Not Found',
145
        self::METHOD_NOT_ALLOWED => 'Method Not Allowed',
146
        self::NOT_ACCEPTABLE => 'Not Acceptable',
147
        self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
148
        self::REQUEST_TIMEOUT => 'Request Time-out',
149
        self::CONFLICT => 'Conflict',
150
        self::GONE => 'Gone',
151
        self::LENGTH_REQUIRED => 'Length Required',
152
        self::PRECONDITION_FAILED => 'Precondition Failed',
153
        self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
154
        self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
155
        self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
156
        self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
157
        self::EXPECTATION_FAILED => 'Expectation Failed',
158
159
        self::INTERNAL_SERVER_ERROR => 'Internal Server Error',
160
        self::NOT_IMPLEMENTED => 'Not Implemented',
161
        self::BAD_GATEWAY => 'Bad Gateway',
162
        self::SERVICE_UNAVAILABLE => 'Service Unavailable',
163
        self::GATEWAY_TIMEOUT => 'Gateway Timeout',
164
        self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported'
165
    ];
166
167
    /**
168
     * Checks if the provided status code falls in the Informational range of HTTP statuses
169
     * @param int $statusCode the status code to check
170
     * @return bool TRUE if it falls into the Informational range, FALSE if not
171
     */
172
    public static function isInformational(int $statusCode): bool
173
    {
174
        return ($statusCode >= self::INFORMATIONAL) && ($statusCode < self::SUCCESSFUL);
175
    }
176
177
    /**
178
     * Checks if the provided status code falls in the Successful range of HTTP statuses
179
     * @param int $statusCode the status code to check
180
     * @return bool TRUE if it falls into the Successful range, FALSE if not
181
     */
182
    public static function isSuccessful(int $statusCode): bool
183
    {
184
        return ($statusCode >= self::SUCCESSFUL) && ($statusCode < self::REDIRECTION);
185
    }
186
187
    /**
188
     * Checks if the provided status code falls in the Redirection range of HTTP statuses
189
     * @param int $statusCode the status code to check
190
     * @return bool TRUE if it falls into the Redirection range, FALSE if not
191
     */
192
    public static function isRedirection(int $statusCode): bool
193
    {
194
        return ($statusCode >= self::REDIRECTION) && ($statusCode < self::CLIENT_ERROR);
195
    }
196
197
    /**
198
     * Checks if the provided status code falls in the Client error range of HTTP statuses
199
     * @param int $statusCode the status code to check
200
     * @return bool TRUE if it falls into the Client error range, FALSE if not
201
     */
202
    public static function isClientError(int $statusCode): bool
203
    {
204
        return ($statusCode >= self::CLIENT_ERROR) && ($statusCode < self::SERVER_ERROR);
205
    }
206
207
    /**
208
     * Checks if the provided status code falls in the Server error range of HTTP statuses
209
     * @param int $statusCode the status code to check
210
     * @return bool TRUE if it falls into the Server error range, FALSE if not
211
     */
212
    public static function isServerError(int $statusCode): bool
213
    {
214
        return ($statusCode >= self::SERVER_ERROR) && ($statusCode < self::SERVER_ERROR_END);
215
    }
216
217
    /**
218
     * Checks if the provided status code falls in the Client or Server error range of HTTP statuses
219
     * @param int $statusCode the status code to check
220
     * @return bool TRUE if it falls into the Client or Server error range, FALSE if not
221
     */
222
    public static function isError(int $statusCode): bool
223
    {
224
        return ($statusCode >= self::CLIENT_ERROR) && ($statusCode < self::SERVER_ERROR_END);
225
    }
226
227
    /**
228
     * Retrieve the reason phrase for the provided HTTP status code
229
     * @param int $statusCode the status code for which to retrieve the reason phrase
230
     * @return string the reason phrase
231
     * @throws InvalidArgumentException if the provided value is not a valid HTTP status code
232
     */
233
    public static function getReasonPhrase(int $statusCode): string
234
    {
235
        if (array_key_exists($statusCode, self::REASON_PHRASES)) {
236
            return self::REASON_PHRASES[$statusCode];
237
        }
238
        throw new InvalidArgumentException('Invalid status code');
239
    }
240
}
241