Completed
Pull Request — master (#3)
by Бабичев
02:07
created

Server::path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Router;
4
5
class Server
6
{
7
8
    /**
9
     * @var array
10
     */
11
    protected $server = [];
12
13
    /**
14
     * @return Server
15
     */
16 3
    public static function sharedInstance(): self
17
    {
18 3
        static $self;
19 3
        if (!$self) {
20 1
            $self = new static();
21
        }
22 3
        return $self;
23
    }
24
25
    /**
26
     * @param string $name
27
     * @param null|string $default
28
     * @return string
29
     */
30 3
    public function server(string $name, ?string $default = null): ?string
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
31
    {
32 3
        if (empty($this->server[$name])) {
33 3
            $this->server[$name] = \filter_input(INPUT_SERVER, $name) ?? $default;
34
        }
35 3
        return $this->server[$name];
36
    }
37
38
    /**
39
     * @param string $default
40
     *
41
     * @return string
42
     */
43 3
    public function method(string $default = 'GET'): string
44
    {
45 3
        return $this->server('REQUEST_METHOD', $default);
46
    }
47
48
    /**
49
     * @param string $default
50
     * @return string
51
     */
52 3
    public function protocol(string $default = 'https'): string
53
    {
54 3
        $protocol = $this->server('HTTP_CF_VISITOR'); // cloudFlare
55
56 3
        if ($protocol)
57
        {
58
            /**
59
             * { scheme: "https" }
60
             *
61
             * @var string $protocol
62
             */
63
            $protocol = json_decode($protocol, true);
64
        }
65
66 3
        return $protocol['scheme'] ??
67 3
            $this->server(
68 3
                'HTTP_X_FORWARDED_PROTO',
69 3
                $this->server('REQUEST_SCHEME', $default)
70
            );
71
    }
72
73
    /**
74
     * @return string
75
     */
76 3
    public function host(): string
77
    {
78 3
        return $this->server('HTTP_HOST', PHP_SAPI);
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function path(): string
85
    {
86
        return \parse_url($this->server('REQUEST_URI'), PHP_URL_PATH);
87
    }
88
89
}
90