Passed
Push — master ( 64deae...57bf37 )
by Radu
01:22
created

RequestServerTrait   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 32
eloc 53
dl 0
loc 113
rs 9.84
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getAcceptLanguage() 0 6 2
A getAcceptContentTypes() 0 20 4
A getContentType() 0 6 2
A getHost() 0 10 4
A getHostExtension() 0 9 2
A getReferer() 0 3 2
A getServerVariable() 0 6 2
A getRefererHost() 0 3 1
A getRemoteAddress() 0 6 3
A getServerProtocol() 0 6 2
B getSchema() 0 16 8
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\Framework;
5
6
trait RequestServerTrait
7
{
8
    public function getAcceptContentTypes()
9
    {
10
        if (!isset($this->server['HTTP_ACCEPT'])) {
11
            return false;
12
        }
13
        $acceptTypes = [];
14
        $httpAccept = strtolower(str_replace(' ', '', $this->server['HTTP_ACCEPT']));
15
        $parts = explode(',', $httpAccept);
16
17
        foreach ($parts as $item) {
18
            $q = 1; // the default quality is 1.
19
            if (strpos($item, ';q=')) { // check if there is a different quality
20
                // divide "mime/type;q=X" into two parts: "mime/type" i "X"
21
                list($item, $q) = explode(';q=', $item);
22
            }
23
            // WARNING: $q == 0 means, that mime-type isn’t supported!
24
            $acceptTypes[$q] = $item;
25
        }
26
        asort($acceptTypes);
27
        return $acceptTypes;
28
    }
29
30
    public function getAcceptLanguage()
31
    {
32
        if (!isset($this->server['HTTP_ACCEPT_LANGUAGE'])) {
33
            return false;
34
        }
35
        return substr($this->server['HTTP_ACCEPT_LANGUAGE'], 0, 2);
36
    }
37
38
    public function getContentType()
39
    {
40
        if (!isset($this->server['CONTENT_TYPE'])) {
41
            return false;
42
        }
43
        return $this->server['CONTENT_TYPE'];
44
    }
45
46
    public function getHost()
47
    {
48
        if (!empty($this->server['HTTP_HOST'])) {
49
            return $this->server['HTTP_HOST'];
50
        } elseif (!empty($this->server['SERVER_NAME'])) {
51
            return $this->server['SERVER_NAME'];
52
        } elseif (!empty($this->server['HOSTNAME'])) {
53
            return $this->server['HOSTNAME']; //CLI
54
        }
55
        return '';
56
    }
57
58
    public function getHostExtension()
59
    {
60
        $host = $this->getHost();
61
        if (empty($host)) {
62
            return false;
63
        }
64
65
        $parts = explode('.', $host);
66
        return end($parts);
67
    }
68
69
    public function getReferer()
70
    {
71
        return isset($this->server['HTTP_REFERER']) ? $this->server['HTTP_REFERER'] : null;
72
    }
73
74
    public function getRefererHost()
75
    {
76
        return parse_url($this->getReferer(), PHP_URL_HOST);
77
    }
78
79
    public function getSchema()
80
    {
81
        if (Framework::isCli()) {
82
            return null;
83
        }
84
85
        if (isset($this->server['HTTPS']) && 'off' != $this->server['HTTPS']) {
86
            return 'https';
87
        } elseif (isset($this->server['HTTP_X_FORWARDED_PROTO']) &&
88
        'https' == $this->server['HTTP_X_FORWARDED_PROTO']) {
89
            return 'https';
90
        } elseif (isset($this->server['HTTP_X_FORWARDED_SSL']) &&
91
        'on' == $this->server['HTTP_X_FORWARDED_SSL']) {
92
            return 'https';
93
        }
94
        return 'http';
95
    }
96
97
    public function getServerProtocol()
98
    {
99
        if (!isset($this->server['SERVER_PROTOCOL'])) {
100
            return false;
101
        }
102
        return $this->server['SERVER_PROTOCOL'];
103
    }
104
105
    public function getRemoteAddress()
106
    {
107
        if (Framework::isCli()) {
108
            return gethostbyname(php_uname('n'));
109
        }
110
        return isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : false;
111
    }
112
113
    public function getServerVariable($index)
114
    {
115
        if (!array_key_exists($index, $this->server)) {
116
            throw new \OutOfBoundsException('Requested key does not exist');
117
        }
118
        return $this->server[$index];
119
    }
120
}
121