Passed
Push — Accessing-and-setting-the-site... ( c85d15...7ed64c )
by Stone
03:19
created

Request::getUri()   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
        $requestMethod = $_SERVER['REQUEST_METHOD'];
24
        if ($requestMethod === 'GET') {
25
            return $_GET[$key] ?? null;
26
        }
27
        if ($requestMethod === 'POST') {
28
            return $_POST[$key] ?? null;
29
        }
30
        throw new \Exception("Unknown Request Method");
31
    }
32
33
    public function getUri(){
34
        return $_SERVER['REQUEST_URI'];
35
    }
36
37
    /**
38
     * checks if the request is a XML HTTP REQUEST
39
     * @return bool
40
     */
41
    public function isXmlRequest()
42
    {
43
        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
44
            return true;
45
        }
46
        return false;
47
    }
48
49
    /**
50
     * gets the referer of the request
51
     * @return string|null
52
     */
53
    public function getReferer()
54
    {
55
        return $_SERVER['HTTP_REFERER'] ?? null;
56
    }
57
58
    /**
59
     * constructs the base url of the site
60
     * @return string
61
     */
62
    public function getBaseUrl(): string
63
    {
64
        $host = $_SERVER['HTTP_HOST'];
65
        $https = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
66
        return $https . '://' . $host . '/';
67
    }
68
69
    /**
70
     * Gettint the headers
71
     * @return array
72
     */
73
    public function getHeaders(): array
74
    {
75
            return apache_request_headers() ?: [];
76
    }
77
78
}