Response   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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
A 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
     * @param int   $code
67
     */
68
    public function __construct($content = null, int $code = 200)
69
    {
70
        $this->setContent($content);
71
        $this->setCode($code);
72
    }
73
74
    /**
75
     * Set response code
76
     *
77
     * @param integer $code
78
     */
79
    public function setCode(int $code = 200)
80
    {
81
        $this->code = $code;
82
    }
83
84
    /**
85
     * Get response code
86
     *
87
     * @return int
88
     */
89
    public function getCode() :int
90
    {
91
        return $this->code;
92
    }
93
94
    /**
95
     * Get response message
96
     *
97
     * @return string
98
     */
99
    public function getMessage(): string
100
    {
101
        $definedMessage = self::HTTP_STATUS_CODES[$this->getCode()] ?? null;
102
103
        if ($definedMessage !== null) {
104
            return $definedMessage;
105
        }
106
107
        return $this->message;
108
    }
109
110
    /**
111
     * Set response message
112
     *
113
     * @param string $message
114
     */
115
    public function setMessage(string $message)
116
    {
117
        $this->message = $message;
118
    }
119
120
    /**
121
     * Set response body
122
     *
123
     * @param mixed $content
124
     * @return self
125
     */
126
    public function setContent($content)
127
    {
128
        $this->content = $content;
129
        return $this;
130
    }
131
132
    /**
133
     * Get response body and set headers
134
     *
135
     * @return mixed
136
     */
137
    public function getContent()
138
    {
139
        $this->setResponseHeader();
140
        return $this->content;
141
    }
142
143
    /**
144
     * @param array $headers
145
     */
146
    public function setResponseHeader(array $headers = [])
147
    {
148
        $serviceLocator = ServiceLocator::instance();
149
150
        /** @var Config $config */
151
        $config = $serviceLocator->get(Config::class);
152
153
        $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/2.0';
154
        header($protocol . ' ' . $this->getCode() . ' ' . self::HTTP_STATUS_CODES[$this->getCode()] . PHP_EOL);
155
156
        if ($config->get('HSTSSupport')) {
157
            header('Strict-Transport-Security: max-age=31536000');
158
        }
159
160
        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...
161
            foreach ($headers as $name => $value) {
162
                header($name . ': ' . $value . PHP_EOL);
163
            }
164
        }
165
    }
166
167
    /**
168
     * If object is getting outputted
169
     *
170
     * @return string
171
     */
172
    public function __toString()
173
    {
174
        return (string)$this->getContent();
175
    }
176
177
}