Passed
Push — master ( ca838e...76d3e9 )
by Radu
01:16
created

RequestProcessTrait::setPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\RequestUtils as Utils;
5
6
trait RequestProcessTrait
7
{
8
    final public function sanitize($data)
9
    {
10
        if (is_array($data)) {
11
            array_walk_recursive(
12
                $data,
13
                'WebServCo\Framework\RequestUtils::sanitizeString'
14
            );
15
            return $data;
16
        }
17
        return Utils::sanitizeString($data);
18
    }
19
    
20
    protected function init($server, $post = [])
21
    {
22
        $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...
23
        
24
        $this->setMethod();
25
        $this->setFilename();
26
        $this->setPath();
27
28
        $this->process();
29
        
30
        switch ($this->method) {
31
            case \WebServCo\Framework\Http::METHOD_GET:
32
            case \WebServCo\Framework\Http::METHOD_HEAD:
33
                break;
34
            case \WebServCo\Framework\Http::METHOD_POST:
35
                $this->processPost($post);
36
                break;
37
        }
38
        if ($this->setting('clear_globals', true)) {
0 ignored issues
show
Bug introduced by
It seems like setting() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        if ($this->/** @scrutinizer ignore-call */ setting('clear_globals', true)) {
Loading history...
39
            $this->clearGlobals();
40
        }
41
    }
42
    
43
    protected function setMethod()
44
    {
45
        if (empty($this->server['REQUEST_METHOD']) ||
46
        !in_array(
47
            $this->server['REQUEST_METHOD'],
48
            \WebServCo\Framework\Http::getMethods()
49
        )) {
50
            return false;
51
        }
52
        $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...
53
        return true;
54
    }
55
    
56
    protected function setFilename()
57
    {
58
        if (empty($this->server['SCRIPT_NAME'])) {
59
            return false;
60
        }
61
        $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...
62
        return true;
63
    }
64
    
65
    protected function setPath()
66
    {
67
        if (empty($this->server['SCRIPT_NAME'])) {
68
            return false;
69
        }
70
        
71
        $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...
72
            str_replace(
73
                $this->filename,
74
                '',
75
                $this->server['SCRIPT_NAME']
76
            ),
77
            DIRECTORY_SEPARATOR
78
        );
79
        
80
        return true;
81
    }
82
    
83
    protected function clearGlobals()
84
    {
85
        if (!empty($_GET)) {
86
            foreach ($_GET as $k => $v) {
87
                unset($_REQUEST[$k]);
88
            }
89
            $_GET = [];
90
        }
91
        if (!empty($_POST)) {
92
            $_POST = [];
93
        }
94
        return true;
95
    }
96
    
97
    protected function processPost($post = [])
98
    {
99
        $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...
100
        foreach ($post as $k => $v) {
101
            $this->data[$this->sanitize($k)] = $v;
102
        }
103
        return true;
104
    }
105
    
106
    protected function process()
107
    {
108
        if (\WebServCo\Framework\Framework::isCLI()) {
109
            return $this->processCli();
110
        }
111
        
112
        return $this->processHttp();
113
    }
114
    
115
    protected function processCli()
116
    {
117
        if (isset($this->server['argv'][1])) {
118
            $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...
119
        }
120
        if (isset($this->server['argv'][2])) {
121
            foreach ($this->server['argv'] as $k => $v) {
122
                if (in_array($k, [0,1])) {
123
                    continue;
124
                }
125
                $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...
126
            }
127
        }
128
        return true;
129
    }
130
    
131
    protected function processHttp()
132
    {
133
        $string = null;
134
        switch (true) {
135
            case isset($this->server['REQUEST_URI']):
136
                $string = $this->server['REQUEST_URI'];
137
                break;
138
            case isset($this->server['PATH_INFO']):
139
                $string = $this->server['PATH_INFO'];
140
                break;
141
            case isset($this->server['ORIG_PATH_INFO']):
142
                $string = $this->server['ORIG_PATH_INFO'];
143
                break;
144
            case !empty($this->server['QUERY_STRING']):
145
                $string = $this->server['ORIG_PATH_INFO'];
146
                break;
147
            default:
148
                break;
149
        }
150
        list ($target, $queryString) = Utils::parse(
151
            $string,
152
            $this->path,
153
            $this->filename,
154
            $this->setting('suffixes')
155
        );
156
        $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...
157
        $this->query = Utils::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...
158
        return true;
159
    }
160
}
161