Passed
Push — master ( 9b8457...f05bc0 )
by Alexander
01:49
created

Response   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 3 1
A code() 0 3 1
A contentType() 0 7 2
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Abstract class Response
7
 *
8
 * @package alkemann\h2l
9
 */
10
abstract class Response
11
{
12
    public static $code_to_message = [
13
        // Informational 1xx
14
        100 => 'Continue',
15
        101 => 'Switching Protocols',
16
        // Successful 2xx
17
        200 => 'OK',
18
        201 => 'Created',
19
        202 => 'Accepted',
20
        203 => 'Non-Authoritative Information',
21
        204 => 'No Content',
22
        205 => 'Reset Content',
23
        206 => 'Partial Content',
24
        // Redirection 3xx
25
        300 => 'Multiple Choices',
26
        301 => 'Moved Permanently',
27
        302 => 'Found',
28
        303 => 'See Other',
29
        304 => 'Not Modified',
30
        305 => 'Use Proxy',
31
        307 => 'Temporary Redirect',
32
        // Client Error 4xx
33
        400 => 'Bad Request',
34
        401 => 'Unauthorized',
35
        402 => 'Payment Required',
36
        403 => 'Forbidden',
37
        404 => 'Not Found',
38
        405 => 'Method Not Allowed',
39
        406 => 'Not Acceptable',
40
        407 => 'Proxy Authentication Required',
41
        408 => 'Request Timeout',
42
        409 => 'Conflict',
43
        410 => 'Gone',
44
        411 => 'Length Required',
45
        412 => 'Precondition Failed',
46
        413 => 'Request Entity Too Large',
47
        414 => 'Request-URI Too Long',
48
        415 => 'Unsupported Media Type',
49
        416 => 'Requested Range Not Satisfiable',
50
        417 => 'Expectation Failed',
51
        // Server Error 5xx
52
        500 => 'Internal Server Error',
53
        501 => 'Not Implemented',
54
        502 => 'Bad Gateway',
55
        503 => 'Service Unavailable',
56
        504 => 'Gateway Timeout',
57
        505 => 'HTTP Version Not Supported',
58
    ];
59
60
    protected $type = 'html';
61
    protected $code = 200;
62
63
    protected $validTypes = ['html', 'json', 'xml'];
64
    protected $contentTypes = [
65
        'html' => 'text/html',
66
        'json' => 'application/json',
67
        'xml' => 'application/xml'
68
    ];
69
70
    public function type(): string
71
    {
72
        return $this->type;
73
    }
74
75
    public function code(): int
76
    {
77
        return $this->code;
78
    }
79
80
    public function contentType(): string
81
    {
82
        $format = $this->type;
83
        if (in_array($format, $this->validTypes)) {
84
            return $this->contentTypes[$format];
85
        }
86
        return "text/html";
87
    }
88
89
    abstract public function render(): string;
90
}
91