GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (a445f1)
by Jérémy
03:02
created

Response::sendHeaders()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.439
c 1
b 0
f 0
cc 6
eloc 13
nc 10
nop 0
1
<?php
2
3
namespace CapMousse\ReactRestify\Http;
4
5
use React\Http\Response as HttpResponse;
6
7
class Response
8
{
9
    /**
10
     * @var \React\Http\Response
11
     */
12
    private $httpResponse;
13
14
    private $server;
15
    private $version;
16
17
    /**
18
     * Status code of the response
19
     * @var int
20
     */
21
    private $status = 200;
22
23
    /**
24
     * Array of headers to send
25
     * @var array
26
     */
27
    private $headers = array();
28
29
    /**
30
     * The content-length
31
     * @var int
32
     */
33
    private $contentLength = 0;
34
35
    /**
36
     * Data to send
37
     * @var string
38
     */
39
    private $data;
40
41
    /**
42
     * Check if headers are already sent
43
     * @var bool
44
     */
45
    private $headersSent = false;
46
47
    /**
48
     * Create a new Restify/Response object
49
     *
50
     * @param \React\Http\Response $response
51
     *
52
     */
53
    public function __construct(HttpResponse $response, $server = null, $version = null)
54
    {
55
        $this->httpResponse = $response;
56
        $this->server = $server;
57
        $this->version = $version;
58
    }
59
60
    /**
61
     * Add a header to the response
62
     *
63
     * @param string $name
64
     * @param string $value
65
     *
66
     * @return Response
67
     */
68
    public function addHeader($name, $value)
69
    {
70
        $this->headers[$name] = $value;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Set the status code of the response
77
     *
78
     * @param int $code
79
     *
80
     * @return Response
81
     */
82
    public function setStatus($code)
83
    {
84
        $this->status = $code;
85
86
        return $this;
87
    }
88
89
    /**
90
     * is the response writable ?
91
     *
92
     * @return boolean
93
     */
94
    public function isWritable()
95
    {
96
        return $this->httpResponse->isWritable();
97
    }
98
99
    /**
100
     * Write a HTTP 100 (continue) header
101
     */
102
    public function writeContinue()
103
    {
104
        $this->httpResponse->writeContinue();
105
    }
106
107
    /**
108
     * Write data to the response
109
     *
110
     * @param string $data
111
     */
112
    public function write($data)
113
    {
114
        $this->contentLength += strlen($data);
115
        $this->data .= $data;
116
    }
117
118
    /**
119
     * Write json to the response
120
     *
121
     * @param mixed $data
122
     */
123
    public function writeJson($data)
124
    {
125
        $data = json_encode($data);
126
127
        $this->write($data);
128
        $this->addHeader("Content-Type", "application/json");
129
    }
130
131
    /**
132
     * End the connexion
133
     */
134
    public function end()
135
    {
136
        $this->sendHeaders();
137
        $this->httpResponse->write($this->data);
138
        $this->httpResponse->end();
139
    }
140
141
    /**
142
     * Close the connexion
143
     */
144
    public function close()
145
    {
146
        $this->sendHeaders();
147
        $this->httpResponse->write($this->data);
148
        $this->httpResponse->close();
149
    }
150
151
    /**
152
     * Send all headers to the response
153
     */
154
    public function sendHeaders()
155
    {
156
        if ($this->headersSent) {
157
            return;
158
        }
159
160
        if (!isset($this->headers["Content-Length"])) {
161
            $this->addHeader("Content-Length", $this->contentLength);
162
163
            if (null !== $this->server) {
164
                $this->addHeader("Server", $this->server);
165
            }
166
167
            if (null !== $this->version) {
168
                $this->addHeader("Server-Version", $this->version);
169
            }
170
171
            if (!isset($this->headers["Content-Type"])) {
172
                $this->addHeader("Content-Type", "text/plain");
173
            }
174
        }
175
176
        $this->httpResponse->writeHead($this->status, $this->headers);
177
        $this->headersSent = true;
178
    }
179
}
180