Completed
Push — 4.0 ( a1234e...3b08f9 )
by Marco
03:21
created

HttpStatusCodes::isSuccessful()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 2
nc 2
nop 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
use \Exception;
4
5
/**
6
 * @package     Comodojo Dispatcher
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @author      Marco Castiello <[email protected]>
9
 * @license     GPL-3.0+
10
 *
11
 * LICENSE:
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 */
26
27
28
class HttpStatusCodes {
29
30
    private $codes = array(
31
        // Informational 1xx
32
        100 => 'Continue',
33
        101 => 'Switching Protocols',
34
        102 => 'Processing',
35
        // Successful 2xx
36
        200 => 'OK',
37
        201 => 'Created',
38
        202 => 'Accepted',
39
        203 => 'Non-Authoritative Information',
40
        204 => 'No Content',
41
        205 => 'Reset Content',
42
        206 => 'Partial Content',
43
        207 => 'Multi-Status', // missing
44
        208 => 'Already Reported', // missing
45
        226 => 'IM Used', // missing
46
        // Redirection 3xx
47
        300 => 'Multiple Choices',
48
        301 => 'Moved Permanently',
49
        302 => 'Found',
50
        303 => 'See Other',
51
        304 => 'Not Modified',
52
        305 => 'Use Proxy',
53
        307 => 'Temporary Redirect',
54
        308 => 'Permanent Redirect',
55
        // Client Error 4xx
56
        400 => 'Bad Request',
57
        401 => 'Unauthorized', // missing
58
        402 => 'Payment Required', // missing
59
        403 => 'Forbidden',
60
        404 => 'Not Found',
61
        405 => 'Method Not Allowed',
62
        406 => 'Not Acceptable', // missing
63
        407 => 'Proxy Authentication Required', // missing
64
        408 => 'Request Timeout', // missing
65
        409 => 'Conflict', // missing
66
        410 => 'Gone',
67
        411 => 'Length Required', // missing
68
        412 => 'Precondition Failed', // missing
69
        413 => 'Payload Too Large', // missing
70
        414 => 'URI Too Long', // missing
71
        415 => 'Unsupported Media Type', // missing
72
        416 => 'Range Not Satisfiable', // missing
73
        417 => 'Expectation Failed', // missing
74
        421 => 'Misdirected Request', // missing
75
        422 => 'Unprocessable Entity', // missing
76
        423 => 'Locked', // missing
77
        424 => 'Failed Dependency', // missing
78
        426 => 'Upgrade Required', // missing
79
        428 => 'Precondition Required', // missing
80
        429 => 'Too Many Requests', // missing
81
        431 => 'Request Header Fields Too Large', // missing
82
        451 => 'Unavailable For Legal Reasons', // missing
83
        // Server Error 5xx
84
        500 => 'Internal Server Error',
85
        501 => 'Not Implemented',
86
        502 => 'Bad Gateway',
87
        503 => 'Service Unavailable',
88
        504 => 'Gateway Timeout',
89
        505 => 'HTTP Version Not Supported',
90
        506 => 'Variant Also Negotiates (Experimental)', // missing
91
        507 => 'Insufficient Storage', // missing
92
        508 => 'Loop Detected', // missing
93
        510 => 'Not Extended', // missing
94
        511 => 'Network Authentication Required' // missing
95
    );
96
    
97
    public function exists($code) {
98
        
99
        return array_key_exists($code, $this->codes);
100
        
101
    }
102
    
103
    public function getMessage($code) {
104
        
105
        if ( $this->exists($code) ) return $this->code[$code];
0 ignored issues
show
Bug introduced by
The property code does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
106
        
107
        throw new Exception("Invalid HTTP status code $code");
108
        
109
    }
110
    
111
    public function isInformational($code) {
112
        
113
        return $code >= 100 && $code < 200;
114
        
115
    }
116
    
117
    public function isSuccessful($code) {
118
        
119
        return $code >= 200 && $code < 300;
120
        
121
    }
122
    
123
    public function isRedirection($code) {
124
        
125
        return $code >= 300 && $code < 400;
126
        
127
    }
128
    
129
    public function isClientError($code) {
130
        
131
        return $code >= 400 && $code < 500;
132
        
133
    }
134
    
135
    public function isServerError($code) {
136
        
137
        return $code >= 500 && $code < 600;
138
        
139
    }
140
141
}
142