Completed
Push — 4.0 ( 2ca4e1...0c3d7c )
by Marco
15:24
created

Status   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 5 1
A set() 0 13 2
A description() 0 15 3
1
<?php namespace Comodojo\Dispatcher\Response;
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
class Status {
28
29
    private $status_code = 200;
30
31
    private $http_status_codes = array(
32
        // Informational 1xx
33
        100 => "Continue",
34
        101 => "Switching Protocols",
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
        // Redirection 3xx
44
        300 => "Multiple Choices",
45
        301 => "Moved Permanently",
46
        302 => "Found",
47
        303 => "See Other",
48
        304 => "Not Modified",
49
        305 => "Use Proxy",
50
        306 => "(Unused)",
51
        307 => "Temporary Redirect",
52
        // Client Error 4xx
53
        400 => "Bad Request",
54
        401 => "Unauthorized",
55
        402 => "Payment Required",
56
        403 => "Forbidden",
57
        404 => "Not Found",
58
        405 => "Method Not Allowed",
59
        406 => "Not Acceptable",
60
        407 => "Proxy Authentication Required",
61
        408 => "Request Timeout",
62
        409 => "Conflict",
63
        410 => "Gone",
64
        411 => "Length Required",
65
        412 => "Precondition Failed",
66
        413 => "Request Entity Too Large",
67
        414 => "Request-URI Too Long",
68
        415 => "Unsupported Media Type",
69
        416 => "Requested Range Not Satisfiable",
70
        417 => "Expectation Failed",
71
        // Server Error 5xx
72
        500 => "Internal Server Error",
73
        501 => "Not Implemented",
74
        502 => "Bad Gateway",
75
        503 => "Service Unavailable",
76
        504 => "Gateway Timeout",
77
        505 => "HTTP Version Not Supported"
78
    );
79
80
    public function get() {
81
82
        return $this->status_code;
83
84
    }
85
86
    public function set($code) {
87
88
        if ( !array_key_exists($code, $this->http_status_codes) ) {
89
90
            throw new Exception("Invalid HTTP Status Code");
91
92
        }
93
94
        $this->status_code = $code;
95
96
        return $this;
97
98
    }
99
100
    public function description($code=null) {
101
102
        if ( is_null($code) || !array_key_exists($code, $this->http_status_codes) ) {
103
104
            $message = $this->http_status_codes[$this->status_code];
105
106
        } else {
107
108
            $message = $this->http_status_codes[$code];
109
110
        }
111
112
        return $message;
113
114
    }
115
116
}
117