Completed
Push — master ( 0d492a...3b127f )
by Flo
12:04
created

Response::getMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/**
3
 * Class Response
4
 *
5
 * @package Faulancer\Http
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Http;
9
10
/**
11
 * Class Response
12
 */
13
class Response extends AbstractHttp
14
{
15
16
    const HTTP_STATUS_CODES = [
17
        100 => 'Continue',
18
        102 => 'Processing',
19
        200 => 'Ok',
20
        206 => 'Partial Content',
21
        301 => 'Moved Permanently',
22
        304 => 'Not Modified',
23
        400 => 'Bad Request',
24
        401 => 'Unauthorized',
25
        403 => 'Forbidden',
26
        404 => 'Not Found',
27
        405 => 'Method Not Allowed',
28
        408 => 'Request Timeout',
29
        410 => 'Gone',
30
        418 => 'I\'m a teapot',
31
        429 => 'Too Many Requests',
32
        500 => 'Internal Server Error',
33
        501 => 'Not Implemented',
34
        502 => 'Bad Gateway',
35
        503 => 'Service Unavailable',
36
        504 => 'Gateway Timed-out',
37
        505 => 'HTTP Version Not Supported',
38
        507 => 'Insufficient Storage',
39
    ];
40
41
    /**
42
     * The status code (default: 200)
43
     * @var integer
44
     */
45
    protected $code = 200;
46
47
    /**
48
     * The status message (default: Ok)
49
     *
50
     * @var string
51
     */
52
    protected $message = 'Ok';
53
54
    /**
55
     * The response body
56
     * @var string
57
     */
58
    protected $content;
59
60
    /**
61
     * Response constructor.
62
     * @param mixed $content
63
     */
64
    public function __construct($content = null)
65
    {
66
        $this->setContent($content);
67
    }
68
69
    /**
70
     * Set response code
71
     *
72
     * @param integer $code
73
     */
74
    public function setCode(int $code = 200)
75
    {
76
        $this->code = $code;
77
    }
78
79
    /**
80
     * Get response code
81
     *
82
     * @return int
83
     */
84
    public function getCode() :int
85
    {
86
        return $this->code;
87
    }
88
89
    /**
90
     * Get response message
91
     *
92
     * @return string
93
     */
94
    public function getMessage(): string
95
    {
96
        $definedMessage = self::HTTP_STATUS_CODES[$this->getCode()] ?? null;
97
98
        if ($definedMessage !== null) {
99
            return $definedMessage;
100
        }
101
102
        return $this->message;
103
    }
104
105
    /**
106
     * Set response message
107
     *
108
     * @param string $message
109
     */
110
    public function setMessage(string $message): void
111
    {
112
        $this->message = $message;
113
    }
114
115
    /**
116
     * Set response body
117
     *
118
     * @param mixed $content
119
     * @return self
120
     */
121
    public function setContent($content)
122
    {
123
        $this->content = $content;
124
        return $this;
125
    }
126
127
    /**
128
     * Get response body and set headers
129
     *
130
     * @return mixed
131
     */
132
    public function getContent()
133
    {
134
        $this->setResponseHeader();
135
        return $this->content;
136
    }
137
138
    /**
139
     * @param array $headers
140
     * @codeCoverageIgnore Is covered because usage of php built-in function
141
     */
142
    public function setResponseHeader(array $headers = [])
143
    {
144
        $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/2.0';
145
        header($protocol . ' ' . $this->getCode() . ' ' . self::HTTP_STATUS_CODES[$this->getCode()] . PHP_EOL);
146
        header('Strict-Transport-Security: max-age=31536000');
147
148
        if ($headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
149
            foreach ($headers as $name => $value) {
150
                header($name . ': ' . $value . PHP_EOL);
151
            }
152
        }
153
    }
154
155
    /**
156
     * If object is getting outputted
157
     *
158
     * @return string
159
     */
160
    public function __toString()
161
    {
162
        return (string)$this->getContent();
163
    }
164
165
}