Completed
Push — master ( f8decb...0d0ae6 )
by Arnaud
15s
created

Request::isXmlRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 6
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
    /**
34
     * checks if the request is a XML HTTP REQUEST
35
     * @return bool
36
     */
37
    public function isXmlRequest()
38
    {
39
        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
40
            return true;
41
        }
42
        return false;
43
    }
44
45
    /**
46
     * gets the referer of the request
47
     * @return string|null
48
     */
49
    public function getReferer()
50
    {
51
        return $_SERVER['HTTP_REFERER'] ?? null;
52
    }
53
54
    /**
55
     * constructs the base url of the site
56
     * @return string
57
     */
58
    public function getBaseUrl(): string
59
    {
60
        $host = $_SERVER['HTTP_HOST'];
61
        $https = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
62
        return $https . '://' . $host . '/';
63
    }
64
65
    /**
66
     * Gettint the headers
67
     * @return array
68
     */
69
    public function getHeaders(): array
70
    {
71
            return apache_request_headers() ?: [];
72
    }
73
74
}