Completed
Push — master ( 5b4b97...81fa56 )
by Flo
10:14
created

Response   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 160
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCode() 0 4 1
A getCode() 0 4 1
A getMessage() 0 10 2
A setMessage() 0 4 1
A setContent() 0 5 1
A getContent() 0 5 1
B setResponseHeader() 0 20 5
A __toString() 0 4 1
1
<?php
2
/**
3
 * Class Response
4
 *
5
 * @package Faulancer\Http
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Http;
9
10
use Faulancer\Service\Config;
11
use Faulancer\ServiceLocator\ServiceLocator;
12
13
/**
14
 * Class Response
15
 */
16
class Response extends AbstractHttp
17
{
18
19
    const HTTP_STATUS_CODES = [
20
        100 => 'Continue',
21
        102 => 'Processing',
22
        200 => 'Ok',
23
        206 => 'Partial Content',
24
        301 => 'Moved Permanently',
25
        304 => 'Not Modified',
26
        400 => 'Bad Request',
27
        401 => 'Unauthorized',
28
        403 => 'Forbidden',
29
        404 => 'Not Found',
30
        405 => 'Method Not Allowed',
31
        408 => 'Request Timeout',
32
        410 => 'Gone',
33
        418 => 'I\'m a teapot',
34
        429 => 'Too Many Requests',
35
        500 => 'Internal Server Error',
36
        501 => 'Not Implemented',
37
        502 => 'Bad Gateway',
38
        503 => 'Service Unavailable',
39
        504 => 'Gateway Timed-out',
40
        505 => 'HTTP Version Not Supported',
41
        507 => 'Insufficient Storage',
42
    ];
43
44
    /**
45
     * The status code (default: 200)
46
     * @var integer
47
     */
48
    protected $code = 200;
49
50
    /**
51
     * The status message (default: Ok)
52
     *
53
     * @var string
54
     */
55
    protected $message = 'Ok';
56
57
    /**
58
     * The response body
59
     * @var string
60
     */
61
    protected $content;
62
63
    /**
64
     * Response constructor.
65
     * @param mixed $content
66
     */
67
    public function __construct($content = null)
68
    {
69
        $this->setContent($content);
70
    }
71
72
    /**
73
     * Set response code
74
     *
75
     * @param integer $code
76
     */
77
    public function setCode(int $code = 200)
78
    {
79
        $this->code = $code;
80
    }
81
82
    /**
83
     * Get response code
84
     *
85
     * @return int
86
     */
87
    public function getCode() :int
88
    {
89
        return $this->code;
90
    }
91
92
    /**
93
     * Get response message
94
     *
95
     * @return string
96
     */
97
    public function getMessage(): string
98
    {
99
        $definedMessage = self::HTTP_STATUS_CODES[$this->getCode()] ?? null;
100
101
        if ($definedMessage !== null) {
102
            return $definedMessage;
103
        }
104
105
        return $this->message;
106
    }
107
108
    /**
109
     * Set response message
110
     *
111
     * @param string $message
112
     */
113
    public function setMessage(string $message)
114
    {
115
        $this->message = $message;
116
    }
117
118
    /**
119
     * Set response body
120
     *
121
     * @param mixed $content
122
     * @return self
123
     */
124
    public function setContent($content)
125
    {
126
        $this->content = $content;
127
        return $this;
128
    }
129
130
    /**
131
     * Get response body and set headers
132
     *
133
     * @return mixed
134
     */
135
    public function getContent()
136
    {
137
        $this->setResponseHeader();
138
        return $this->content;
139
    }
140
141
    /**
142
     * @param array $headers
143
     */
144
    public function setResponseHeader(array $headers = [])
145
    {
146
        $serviceLocator = ServiceLocator::instance();
147
148
        /** @var Config $config */
149
        $config = $serviceLocator->get(Config::class);
150
151
        $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/2.0';
152
        header($protocol . ' ' . $this->getCode() . ' ' . self::HTTP_STATUS_CODES[$this->getCode()] . PHP_EOL);
153
154
        if ($config->get('HSTSSupport')) {
155
            header('Strict-Transport-Security: max-age=31536000');
156
        }
157
158
        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...
159
            foreach ($headers as $name => $value) {
160
                header($name . ': ' . $value . PHP_EOL);
161
            }
162
        }
163
    }
164
165
    /**
166
     * If object is getting outputted
167
     *
168
     * @return string
169
     */
170
    public function __toString()
171
    {
172
        return (string)$this->getContent();
173
    }
174
175
}