RestUtilities::getStatusCodeMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 27
rs 9.536
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
/**
5
 * KNUT7 K7F (https://marciozebedeu.com/)
6
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @link      https://github.com/knut7/framework/ for the canonical source repository
13
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
14
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
15
 * @author    Marcio Zebedeu - [email protected]
16
 * @version   1.0.2
17
 */
18
19
namespace Ballybran\Core\Http;
20
21
    class RestUtilities
22
{
23
    private static $httpVersion = 'HTTP/1.1';
24
25
    public static function processRequest()
26
    {
27
        $req_method = strtolower($_SERVER['REQUEST_METHOD']);
28
29
        $obj = new RestRequest();
30
        $data = array();
31
        switch ($req_method) {
32
            case 'get':
33
                $data = $_GET;
34
                break;
35
            case 'DELETE':
36
                break;
37
            case 'post':
38
                $data = $_POST;
39
                break;
40
            case 'delete':
41
                $data = $_POST;
42
                break;
43
            case 'put':
44
                $data = parse_str(file_get_contents('php://input'), $put_vars);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as parse_str(file_get_conte...p://input'), $put_vars) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
                break;
46
            default:
47
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
                break;
49
        }
50
        $obj->setMethod($req_method);
51
        $obj->setRequestVars($data);
52
        if (isset($data['data'])) {
53
            $obj->setData(json_decode($data['data']));
54
        }
55
        return $obj;
56
    }
57
58
    public static function sendResponse(int $status = 200, $body = '', string $content_type =
59
    'text/html')
60
    {
61
        $status_header = self::$httpVersion . $status . ' '
62
            . RestUtilities::getStatusCodeMessage($status);
63
64
65
        $ob = ini_get('output_buffering');
66
67
        // Abort the method if headers have already been sent, except when output buffering has been enabled
68
        if (headers_sent() && false === (bool)$ob || 'off' == strtolower($ob)) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (headers_sent() && false...off' == strtolower($ob), Probably Intended Meaning: headers_sent() && (false...ff' == strtolower($ob))
Loading history...
69
            return new self();
70
        }
71
72
        header($status_header);
73
        header('Access-Control-allow-Origin:*');
74
        header('Access-Control-allow-Methods:POST, GET, DELETE, PUT');
75
        header('Content-type: ' . $content_type);
76
        header('Content-length: ' . strlen($body));
77
        if ($body != '') {
78
            return $body;
79
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Unused Code introduced by
ExitNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
80
        }
81
        $msg = '';
82
        switch ($status_header) {
83
            case 401:
84
                $msg = 'You must be authorized to view this page.';
85
                break;
86
            case 404:
87
                $msg = 'The requested URL was not found.';
88
                break;
89
            case 500:
90
                $msg = 'The server encountered an error processing your request.';
91
                break;
92
            case 501:
93
                $msg = 'The requested method is not implemented.';
94
95
                break;
96
        }
97
        $body = "<!DOCTYPE html>
98
                <html lang='en'>
99
                <head>
100
                       <meta charset='utf-8'>
101
                        <title>" . $status . ' ' .
102
            self::getStatusCodeMessage($status) . "</title>
103
                        </head>
104
                        <body>
105
                        <h1>" . self::getStatusCodeMessage($status) . "</h1>
106
                        <p>" . $msg . "</p>
107
                        </body></html>";
108
        return $body;
109
110
    }
111
112
    private static function getStatusCodeMessage($status)
113
    {
114
        $codes = Array(
115
            200 => 'OK',
116
            201 => 'Created',
117
            202 => 'Accepted',
118
            204 => 'No Content',
119
            301 => 'Moved Permanently',
120
            302 => 'Found',
121
            303 => 'See Other',
122
            304 => 'Not Modified',
123
            305 => 'Use Proxy',
124
            306 => '(Unused)',
125
            307 => 'Temporary Redirect',
126
            400 => 'Bad Request',
127
            401 => 'Unauthorized',
128
            402 => 'Payment Required',
129
            403 => 'Forbidden',
130
            404 => 'Not Found',
131
            500 => 'Internal Server Error',
132
            501 => 'Not Implemented',
133
            502 => 'Bad Gateway',
134
            503 => 'Service Unavailable',
135
            504 => 'Gateway Timeout',
136
            505 => 'HTTP Version Not Supported'
137
        );
138
        return (isset($codes[$status])) ? $codes[$status] : '';
139
    }
140
141
}