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.

Response   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 25
c 7
b 2
f 1
lcom 1
cbo 3
dl 0
loc 197
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 4 1
A send() 0 4 1
C flush() 0 29 7
A verifyResponseCode() 0 15 4
A setFormat() 0 8 2
A getFormat() 0 3 1
A addHeader() 0 7 2
A disableBrowserCache() 0 6 1
A sendHeaders() 0 10 4
B codes() 0 45 1
1
<?php
2
3
namespace Zaphpa;
4
5
/**
6
 * Response class
7
 */
8
class Response {
9
10
    /** Ordered chunks of the output buffer **/
11
    public $chunks = array();
12
13
    public $code = 200;
14
15
    private $format;
16
    private $req;
17
    private $headers = array();
18
19
    /** Public constructor **/
20
    function __construct($request = null) {
21
        $this->req = $request;
22
    }
23
24
    /**
25
     * Add string to output buffer.
26
     */
27
    public function add($out) {
28
        $this->chunks[]  = $out;
29
        return $this;
30
    }
31
32
    /**
33
     * Flush output buffer to http client and end request
34
     *
35
     *  @param $code
36
     *      HTTP response Code. Defaults to 200
37
     *  @param $format
38
     *      Output mime type. Defaults to request format
39
     */
40
    public function send($code = null, $format = null) {
41
        $this->flush($code, $format);
42
        exit(); //prevent any further output
43
    }
44
45
    /**
46
     * Send output to client without ending the script
47
     *
48
     *  @param integer $code
49
     *      HTTP response Code. Defaults to 200
50
     *  @param string $format
51
     *      Output mime type. Defaults to request format
52
     *
53
     *  @return Response current respons object, so you can chain method calls on a response object.
54
     */
55
    public function flush($code = null, $format = null) {
56
        $this->verifyResponseCode($code);
57
58
        // If no format was set explicitly, use the request format for response.
59
        if (!empty($format)) {
60
            if (headers_sent()) {
61
                throw new Exceptions\InvalidResponseStateException("Response format already sent: {$this->format}");
62
            }
63
            $this->setFormat($format);
64
        }
65
66
        // Set default values (200 and request format) if nothing was set explicitely
67
        if (empty($this->format)) { $this->format = $this->req->format; }
68
        if (empty($this->code)) { $this->code = 200; }
69
70
        $this->sendHeaders();
71
72
        /* Call preprocessors on each BaseMiddleware impl */
73
        foreach (Router::$middleware as $m) {
74
            if ($m->shouldRun()) {
75
                $m->prerender($this->chunks);
76
            }
77
        }
78
79
        $out = implode('', $this->chunks);
80
        $this->chunks = array(); // reset
81
        echo ($out);
82
        return $this;
83
    }
84
85
    protected function verifyResponseCode($code) {
86
        if (!empty($code)) {
87
            if (headers_sent()) {
88
                throw new Exceptions\InvalidResponseStateException("Response code already sent: {$this->code}");
89
            }
90
91
            $codes = $this->codes();
92
            if (array_key_exists($code, $codes)) {
93
                //$protocol = $this->req->protocol;
94
                $this->code = $code;
95
            } else {
96
                throw new Exceptions\InvalidResponseCodeException("Invalid Response Code: $code");
97
            }
98
        }
99
    }
100
101
    /**
102
     * Set output format. Common aliases like: xml, json, html and txt are supported and
103
     * automatically converted to proper HTTP content type definitions.
104
     */
105
    public function setFormat($format) {
106
        $aliases = $this->req->common_aliases();
107
        if (array_key_exists($format, $aliases)) {
108
            $format = $aliases[$format];
109
        }
110
        $this->format = $format;
111
        return $this;
112
    }
113
114
    public function getFormat() {
115
        return $this->format;
116
    }
117
118
    /**
119
    * Add an HTTP header key/value pair
120
    *
121
    * $key string
122
    * $val string
123
    *
124
    */
125
    public function addHeader($key, $val) {
126
        if (is_array($val)) {
127
            $val = implode(", ", $val);
128
        }
129
        $this->headers[] = "{$key}: $val";
130
        return $this;
131
    }
132
133
    /**
134
     * Send headers to instruct browser not to cache this content
135
     * See http://stackoverflow.com/a/2068407
136
     */
137
    public function disableBrowserCache() {
138
        $this->headers[] = 'Cache-Control: no-cache, no-store, must-revalidate'; // HTTP 1.1.
139
        $this->headers[] = 'Pragma: no-cache'; // HTTP 1.0.
140
        $this->headers[] = 'Expires: Thu, 26 Feb 1970 20:00:00 GMT'; // Proxies.
141
        return $this;
142
    }
143
144
    /**
145
     *  Send entire collection of headers if they haven't already been sent
146
     */
147
    public function sendHeaders($noContentType = false) {
148
        if (!headers_sent()) {
149
            foreach ($this->headers as $header) {
150
                header($header);
151
            }
152
            if ($noContentType == false) {
153
                header("Content-Type: $this->format;", true, $this->code);
154
            }
155
        }
156
    }
157
158
    private function codes() {
159
        return array(
160
            '100' => 'Continue',
161
            '101' => 'Switching Protocols',
162
            '200' => 'OK',
163
            '201' => 'Created',
164
            '202' => 'Accepted',
165
            '203' => 'Non-Authoritative Information',
166
            '204' => 'No Content',
167
            '205' => 'Reset Content',
168
            '206' => 'Partial Content',
169
            '300' => 'Multiple Choices',
170
            '301' => 'Moved Permanently',
171
            '302' => 'Found',
172
            '303' => 'See Other',
173
            '304' => 'Not Modified',
174
            '305' => 'Use Proxy',
175
            '307' => 'Temporary Redirect',
176
            '400' => 'Bad Request',
177
            '401' => 'Unauthorized',
178
            '402' => 'Payment Required',
179
            '403' => 'Forbidden',
180
            '404' => 'Not Found',
181
            '405' => 'Method Not Allowed',
182
            '406' => 'Not Acceptable',
183
            '407' => 'Proxy Authentication Required',
184
            '408' => 'Request Timeout',
185
            '409' => 'Conflict',
186
            '410' => 'Gone',
187
            '411' => 'Length Required',
188
            '412' => 'Precondition Failed',
189
            '413' => 'Request Entity Too Large',
190
            '414' => 'Request-URI Too Long',
191
            '415' => 'Unsupported Media Type',
192
            '416' => 'Requested Range Not Satisfiable',
193
            '417' => 'Expectation Failed',
194
            '429' => 'Too Many Requests',
195
            '500' => 'Internal Server Error',
196
            '501' => 'Not Implemented',
197
            '502' => 'Bad Gateway',
198
            '503' => 'Service Unavailable',
199
            '504' => 'Gateway Timeout',
200
            '505' => 'HTTP Version Not Supported',
201
        );
202
    }
203
204
} // end Request