Passed
Pull Request — dev (#47)
by Stone
04:17 queued 01:57
created

Request::isGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Core\Dependency;
4
5
/**
6
 * Class Request
7
 * @package Core
8
 *
9
 * we are dealing with get, post and cookies here, all Superglobals
10
 * @SuppressWarnings(PHPMD.Superglobals)
11
 */
12
class Request
13
{
14
15
    /**
16
     * gets the data from a get or a post request
17
     * @param $key
18
     * @return mixed
19
     * @throws \Exception
20
     */
21
    public function getData($key)
22
    {
23
        if ($this->isGet()) {
24
            return $_GET[$key] ?? null;
25
        }
26
        if ($this->isPost()) {
27
            return $_POST[$key] ?? null;
28
        }
29
        throw new \Exception("Unknown Request Method");
30
    }
31
32
    /**
33
     * gets the full data from a get or a post request
34
     * @return mixed
35
     * @throws \Exception
36
     */
37
    public function getDataFull()
38
    {
39
        if ($this->isGet()) {
40
            return $_GET ?? null;
41
        }
42
        if ($this->isPost()) {
43
            return $_POST ?? null;
44
        }
45
        throw new \Exception("Unknown Request Method");
46
    }
47
48
    /**
49
     * is the call a post
50
     * @return bool
51
     */
52
    public function isPost():bool{
53
        return $_SERVER['REQUEST_METHOD'] === 'POST';
54
    }
55
56
    /**
57
     * is the call a get
58
     * @return bool
59
     */
60
    public function isGet():bool{
61
        return $_SERVER['REQUEST_METHOD'] === 'GET';
62
    }
63
64
    /**
65
     * get the current uri for routing
66
     * @return mixed
67
     */
68
    public function getUri(){
69
        return $_SERVER['REQUEST_URI'];
70
    }
71
72
    /**
73
     * checks if the request is a XML HTTP REQUEST
74
     * @return bool
75
     */
76
    public function isXmlRequest()
77
    {
78
        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
79
            return true;
80
        }
81
        return false;
82
    }
83
84
    /**
85
     * gets the referer of the request
86
     * @return string|null
87
     */
88
    public function getReferer()
89
    {
90
        return $_SERVER['HTTP_REFERER'] ?? null;
91
    }
92
93
    /**
94
     * constructs the base url of the site
95
     * @return string
96
     */
97
    public function getBaseUrl(): string
98
    {
99
        $host = $_SERVER['HTTP_HOST'];
100
        $https = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
101
        return $https . '://' . $host . '/';
102
    }
103
104
    /**
105
     * Gettint the headers
106
     * @return array
107
     */
108
    public function getHeaders(): array
109
    {
110
            return apache_request_headers() ?: [];
111
    }
112
113
}