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
Push — master ( 19f587...7aa64a )
by Omar El
03:11
created

Response   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 336
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 336
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A flushBuffer() 0 4 1
B sendHeaders() 0 23 4
A sendContent() 0 4 1
A setContent() 0 4 1
A type() 0 10 2
A stop() 0 3 1
A readFile() 0 4 1
A writeCSV() 0 15 2
A setStatusCode() 0 7 2
A getMimeType() 0 8 3
A clearBuffer() 0 7 2
B download() 0 28 3
A csv() 0 15 1
1
<?php
2
3
/**
4
 * Response class.
5
 *
6
 * handles the response text, status and headers of a HTTP response.
7
 *
8
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
9
 * @author     Omar El Gabry <[email protected]>
10
 */
11
12
class Response {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
14
    /**
15
     * @var array
16
     */
17
    public $headers;
18
19
    /**
20
     * @var string
21
     */
22
    private $content;
23
24
    /**
25
     * @var string
26
     */
27
    private $version;
28
29
    /**
30
     * @var int
31
     */
32
    private $statusCode;
33
34
    /**
35
     * @var string
36
     */
37
    private $statusText;
38
39
    /**
40
     * @var string
41
     */
42
    private $charset;
43
44
    /**
45
     * @var string
46
     */
47
    private $file = null;
48
49
    /**
50
     * @var array
51
     */
52
    private $csv = null;
53
54
    /**
55
     * Holds HTTP response statuses
56
     *
57
     * @var array
58
     */
59
    private $statusTexts = [
60
        200 => 'OK',
61
        302 => 'Found',
62
        400 => 'Bad Request',
63
        401 => 'Unauthorized',
64
        403 => 'Forbidden',
65
        404 => 'Not Found',
66
        500 => 'Internal Server Error'
67
    ];
68
69
    /**
70
     * Holds type key to mime type mappings for known mime types.
71
     *
72
     * @var array
73
     */
74
    private $mimeTypes = [
75
        'csv'  => ['text/csv', 'application/vnd.ms-excel'],
76
        'doc'  => 'application/msword',
77
        'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
78
        'pdf'  => 'application/pdf',
79
        'zip'  => 'application/zip',
80
        'ppt'  => 'application/vnd.ms-powerpoint'
81
    ];
82
83
    /**
84
     * Constructor.
85
     *
86
     * @param string $content The response content
87
     * @param int    $status  The response status code
88
     * @param array  $headers An array of response headers
89
     *
90
     */
91
    public function __construct($content = '', $status = 200, $headers = array()){
92
93
        $this->content = $content;
94
        $this->statusCode = $status;
95
        $this->headers = $headers;
96
        $this->statusText = $this->statusTexts[$status];
97
        $this->version = '1.0';
98
        $this->charset = 'UTF-8';
99
    }
100
101
    /**
102
     * Sends HTTP headers and content.
103
     *
104
     */
105
    public function send(){
106
107
        $this->sendHeaders();
108
109
        if ($this->file) {
110
            $this->readFile();
111
        } else if ($this->csv) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->csv 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...
112
            $this->writeCSV();
113
        } else {
114
            $this->sendContent();
115
        }
116
117
        if (function_exists('fastcgi_finish_request')) {
118
            fastcgi_finish_request();
119
        } elseif ('cli' !== PHP_SAPI) {
120
            $this->flushBuffer();
121
        }
122
123
        return $this;
124
    }
125
126
    /**
127
     * Flushes output buffers.
128
     *
129
     */
130
    private function flushBuffer(){
131
        // ob_flush();
132
        flush();
133
    }
134
135
    /**
136
     * Sends HTTP headers.
137
     *
138
     * @return Response
139
     */
140
    private function sendHeaders(){
141
142
        // check headers have already been sent by the developer
143
        if (headers_sent()) {
144
            return $this;
145
        }
146
147
        // status
148
        header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);
149
150
        // Content-Type
151
        // if Content-Type is already exists in headers, then don't send it
152
        if(!array_key_exists('Content-Type', $this->headers)){
153
            header('Content-Type: ' . 'text/html; charset=' . $this->charset);
154
        }
155
156
        // headers
157
        foreach ($this->headers as $name => $value) {
158
            header($name .': '. $value, true, $this->statusCode);
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * Sends content for the current web response.
166
     *
167
     * @return Response
168
     */
169
    private function sendContent(){
170
        echo $this->content;
171
        return $this;
172
    }
173
174
    /**
175
     * Sets content for the current web response.
176
     * 
177
     * @param string $content The response content
178
     * @return Response
179
     */
180
    public function setContent($content = ""){
181
        $this->content = $content;
182
        return $this;
183
    }
184
185
    /**
186
     * Sets content for the current web response.
187
     * 
188
     * @param string|null $content The response content
0 ignored issues
show
Documentation introduced by
There is no parameter named $content. Did you maybe mean $contentType?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
189
     * @return Response
190
     */
191
    public function type($contentType = null){
192
193
        if($contentType == null){
194
            unset($this->headers['Content-Type']);
195
        }else{
196
            $this->header['Content-Type'] = $contentType;
0 ignored issues
show
Bug introduced by
The property header does not seem to exist. Did you mean headers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
197
        }
198
199
        return $this;
200
    }
201
202
    /**
203
    * Stop execution of the current script. .
204
    *
205
    * @param int|string $status
206
    * @return void
207
    * @see http://php.net/exit
208
    */
209
    public function stop($status = 0){
210
        exit($status);
0 ignored issues
show
Coding Style Compatibility introduced by
The method stop() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
211
    }
212
213
    /**
214
     * read file
215
     *
216
     * @return Response
217
     */
218
    private function readFile(){
219
        readfile($this->file);
220
        return $this;
221
    }
222
223
    /**
224
     * write to CSV file
225
     *
226
     * @return Response
227
     */
228
    private function writeCSV(){
229
230
        $cols = $this->csv["cols"];
231
        $rows = $this->csv["rows"];
232
233
        $out = fopen("php://output", 'w');
234
235
        fputcsv($out, $cols, ',', '"');
236
        foreach($rows as $row) {
237
            fputcsv($out, array_values($row), ',', '"');
238
        }
239
240
        fclose($out);
241
        return $this;
242
    }
243
244
    /**
245
     * Sets the response status code & it's relevant text.
246
     *
247
     * @param int $code HTTP status code
248
     * @return Response
249
     */
250
    public function setStatusCode($code){
251
252
        $this->statusCode = (int) $code;
253
        $this->statusText = isset($this->statusTexts[$code]) ? $this->statusTexts[$code] : '';
254
255
        return $this;
256
    }
257
258
    /**
259
     * Returns the mime type definition for an alias
260
     *
261
     * @param string $key
262
     * @return mixed
263
     */
264
    private function getMimeType($key){
265
266
        if (isset($this->mimeTypes[$key])) {
267
            $mime = $this->mimeTypes[$key];
268
            return  is_array($mime) ? current($mime) : $mime;
269
        }
270
        return false;
271
    }
272
273
    /**
274
     * Clean (erase) the output buffer
275
     *
276
     * @return void
277
     */
278
    public function clearBuffer(){
279
	
280
		// check if output_buffering is active
281
		if(ob_get_level() > 0){
282
			return ob_clean();
283
		}
284
    }
285
286
    /**
287
     * download a file
288
     *
289
     * @param  string $path
290
     * @param  array  $file
291
     * @param  array  $headers
292
     * @return Response
293
     */
294
    public function download($path, array $file, array $headers = []){
295
296
        $this->file = $path;
297
        $this->setStatusCode(200);
298
299
        if(empty($headers)){
300
301
            $mime = $this->getMimeType($file["extension"]);
302
            if(!$mime){
303
                $mime = "application/octet-stream";
304
            }
305
306
            $headers = [
307
                'Content-Description' => 'File Transfer',
308
                'Content-Type' => $mime,
309
                'Content-Disposition' => 'attachment; filename="'. $file["basename"].'"',
310
                'Expires' => '0',
311
                'Cache-Control' => 'must-revalidate',
312
                'Pragma' => 'public',
313
                'Content-Transfer-Encoding' => 'binary',
314
                'Content-Length' => filesize($path)
315
            ];
316
        }
317
318
        $this->headers = $headers;
319
        $this->clearBuffer();
320
        return $this;
321
    }
322
323
    /**
324
     * download CSV file
325
     * This will create csv file using $csvData
326
     *
327
     * @param  array  $csvData
328
     * @param  array  $file
329
     * @return Response
330
     * @see    core/Response/writeCSV()
331
     */
332
    public function csv(array $csvData, array $file){
333
334
        $this->csv = $csvData;
335
        $this->setStatusCode(200);
336
337
        $basename = $file["filename"] . ".csv";
338
        $headers  = [
339
            'Content-Type' => 'text/csv',
340
            'Content-Disposition' => 'attachment; filename="'. $basename.'"'
341
        ];
342
343
        $this->headers = $headers;
344
        $this->clearBuffer();
345
        return $this;
346
    }
347
} 
348