Passed
Push — master ( 21d408...c01c31 )
by Radu
01:12
created

RequestServerTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAcceptLanguage() 0 6 2
A getHost() 0 10 4
A getHostExtension() 0 9 2
A getServerProtocol() 0 6 2
A getReferer() 0 3 2
B getSchema() 0 16 8
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
trait RequestServerTrait
5
{
6
    public function getAcceptLanguage()
7
    {
8
        if (!isset($this->server['HTTP_ACCEPT_LANGUAGE'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property server does not exist on WebServCo\Framework\Traits\RequestServerTrait. Did you maybe forget to declare it?
Loading history...
9
            return false;
10
        }
11
        return substr($this->server['HTTP_ACCEPT_LANGUAGE'], 0, 2);
12
    }
13
14
    public function getHost()
15
    {
16
        if (!empty($this->server['HTTP_HOST'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property server does not exist on WebServCo\Framework\Traits\RequestServerTrait. Did you maybe forget to declare it?
Loading history...
17
            return $this->server['HTTP_HOST'];
18
        } elseif (!empty($this->server['SERVER_NAME'])) {
19
            return $this->server['SERVER_NAME'];
20
        } elseif (!empty($this->server['HOSTNAME'])) {
21
            return $this->server['HOSTNAME']; //CLI
22
        }
23
        return null;
24
    }
25
26
    public function getHostExtension()
27
    {
28
        $host = $this->getHost();
29
        if (empty($host)) {
30
            return false;
31
        }
32
33
        $parts = explode('.', $host);
34
        return end($parts);
35
    }
36
37
    public function getReferer()
38
    {
39
        return isset($this->server['HTTP_REFERER']) ? $this->server['HTTP_REFERER'] : null;
0 ignored issues
show
Bug Best Practice introduced by
The property server does not exist on WebServCo\Framework\Traits\RequestServerTrait. Did you maybe forget to declare it?
Loading history...
40
    }
41
42
    public function getSchema()
43
    {
44
        if (\WebServCo\Framework\Framework::isCLI()) {
45
            return null;
46
        }
47
48
        if (isset($this->server['HTTPS']) && 'off' != $this->server['HTTPS']) {
0 ignored issues
show
Bug Best Practice introduced by
The property server does not exist on WebServCo\Framework\Traits\RequestServerTrait. Did you maybe forget to declare it?
Loading history...
49
            return 'https';
50
        } elseif (isset($this->server['HTTP_X_FORWARDED_PROTO']) &&
51
        'https' == $this->server['HTTP_X_FORWARDED_PROTO']) {
52
            return 'https';
53
        } elseif (isset($this->server['HTTP_X_FORWARDED_SSL']) &&
54
        'on' == $this->server['HTTP_X_FORWARDED_SSL']) {
55
            return 'https';
56
        }
57
        return 'http';
58
    }
59
60
    public function getServerProtocol()
61
    {
62
        if (!isset($this->server['SERVER_PROTOCOL'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property server does not exist on WebServCo\Framework\Traits\RequestServerTrait. Did you maybe forget to declare it?
Loading history...
63
            return false;
64
        }
65
        return $this->server['SERVER_PROTOCOL'];
66
    }
67
}
68