Passed
Push — master ( ffa7be...fb5c02 )
by Radu
01:19
created

RequestProcessTrait::processPost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\RequestUtils;
5
6
trait RequestProcessTrait
7
{
8
    abstract public function setting($key, $defaultValue = false);
9
10
    public function sanitize($data)
11
    {
12
        if (is_array($data)) {
13
            array_walk_recursive(
14
                $data,
15
                '\WebServCo\Framework\RequestUtils::sanitizeString'
16
            );
17
            return $data;
18
        }
19
        return RequestUtils::sanitizeString($data);
20
    }
21
22
    protected function init($server, $post = [])
23
    {
24
        $this->server = $this->sanitize($server);
0 ignored issues
show
Bug Best Practice introduced by
The property server does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
26
        $this->setBody();
27
        $this->setMethod();
28
        $this->setFilename();
29
        $this->setPath();
30
31
        $this->process();
32
33
        switch ($this->method) {
34
            case \WebServCo\Framework\Http::METHOD_GET:
35
            case \WebServCo\Framework\Http::METHOD_HEAD:
36
                break;
37
            case \WebServCo\Framework\Http::METHOD_POST:
38
                $this->processPost($post);
39
                break;
40
        }
41
        if ($this->setting('clear_globals', true)) {
42
            $this->clearGlobals();
43
        }
44
    }
45
46
    protected function setBody()
47
    {
48
        $this->body = file_get_contents('php://input');
0 ignored issues
show
Bug Best Practice introduced by
The property body does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
    }
50
51
    protected function setMethod()
52
    {
53
        if (empty($this->server['REQUEST_METHOD']) ||
54
        !in_array(
55
            $this->server['REQUEST_METHOD'],
56
            \WebServCo\Framework\Http::getMethods()
57
        )) {
58
            return false;
59
        }
60
        $this->method = $this->server['REQUEST_METHOD'];
0 ignored issues
show
Bug Best Practice introduced by
The property method does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
        return true;
62
    }
63
64
    protected function setFilename()
65
    {
66
        if (empty($this->server['SCRIPT_NAME'])) {
67
            return false;
68
        }
69
        $this->filename = basename($this->server['SCRIPT_NAME']);
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        return true;
71
    }
72
73
    protected function setPath()
74
    {
75
        if (empty($this->server['SCRIPT_NAME'])) {
76
            return false;
77
        }
78
79
        $this->path = rtrim(
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
            str_replace(
81
                $this->filename,
82
                '',
83
                $this->server['SCRIPT_NAME']
84
            ),
85
            DIRECTORY_SEPARATOR
86
        );
87
88
        return true;
89
    }
90
91
    protected function clearGlobals()
92
    {
93
        if (!empty($_GET)) {
94
            foreach ($_GET as $k => $v) {
95
                unset($_REQUEST[$k]);
96
            }
97
            $_GET = [];
98
        }
99
        if (!empty($_POST)) {
100
            $_POST = [];
101
        }
102
        return true;
103
    }
104
105
    protected function processPost($post = [])
106
    {
107
        $this->data = [];
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
108
        foreach ($post as $k => $v) {
109
            $this->data[$this->sanitize($k)] = $v;
110
        }
111
        return true;
112
    }
113
114
    protected function process()
115
    {
116
        if (\WebServCo\Framework\Framework::isCLI()) {
117
            return $this->processCli();
118
        }
119
120
        return $this->processHttp();
121
    }
122
123
    protected function processCli()
124
    {
125
        if (isset($this->server['argv'][1])) {
126
            $this->target = $this->server['argv'][1];
0 ignored issues
show
Bug Best Practice introduced by
The property target does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
127
        }
128
        if (isset($this->server['argv'][2])) {
129
            foreach ($this->server['argv'] as $k => $v) {
130
                if (in_array($k, [0,1])) {
131
                    continue;
132
                }
133
                $this->args[] = $this->sanitize($v);
0 ignored issues
show
Bug Best Practice introduced by
The property args does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
            }
135
        }
136
        return true;
137
    }
138
139
    protected function processHttp()
140
    {
141
        $string = null;
142
        switch (true) {
143
            case isset($this->server['REQUEST_URI']):
144
                $string = $this->server['REQUEST_URI'];
145
                break;
146
            case isset($this->server['PATH_INFO']):
147
                $string = $this->server['PATH_INFO'];
148
                break;
149
            case isset($this->server['ORIG_PATH_INFO']):
150
                $string = $this->server['ORIG_PATH_INFO'];
151
                break;
152
            case !empty($this->server['QUERY_STRING']):
153
                $string = $this->server['ORIG_PATH_INFO'];
154
                break;
155
            default:
156
                break;
157
        }
158
        list ($target, $queryString, $suffix) = RequestUtils::parse(
159
            $string,
160
            $this->path,
161
            $this->filename,
162
            $this->setting('suffixes')
163
        );
164
        $this->target = $this->sanitize(urldecode($target));
0 ignored issues
show
Bug Best Practice introduced by
The property target does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
165
        $this->query = RequestUtils::format($this->sanitize($queryString));
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
166
        $this->suffix = $suffix;
0 ignored issues
show
Bug Best Practice introduced by
The property suffix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
167
        return true;
168
    }
169
}
170