Passed
Push — master ( 65b336...1323cd )
by Бабичев
44s
created

Server::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Bavix\Router;
4
5
class Server
6
{
7
8
    /**
9
     * @var array
10
     */
11
    protected $input = [];
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 get(string $name, ?string $default = null): ?string
31
    {
32 3
        if (empty($this->input[$name])) {
33 3
            $this->input[$name] = \filter_input(INPUT_SERVER, $name, \FILTER_DEFAULT, [
34
                'options' => [
35 3
                    'default' => $default
36
                ]
37
            ]);
38
        }
39 3
        return $this->input[$name];
40
    }
41
42
    /**
43
     * @param string $default
44
     *
45
     * @return string
46
     */
47 3
    public function method(string $default = 'GET'): string
48
    {
49 3
        return $this->get('REQUEST_METHOD', $default);
50
    }
51
52
    /**
53
     * @param string $default
54
     * @return string
55
     */
56 3
    public function protocol(string $default = 'https'): string
57
    {
58 3
        $protocol = $this->get('HTTP_CF_VISITOR'); // cloudFlare
59
60 3
        if ($protocol)
61
        {
62
            /**
63
             * { scheme: "https" }
64
             *
65
             * @var string $protocol
66
             */
67
            $protocol = \json_decode($protocol, true);
68
        }
69
70 3
        return $protocol['scheme'] ??
71 3
            $this->get(
72 3
                'HTTP_X_FORWARDED_PROTO',
73 3
                $this->get('REQUEST_SCHEME', $default)
74
            );
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function host(): string
81
    {
82 3
        return $this->get('HTTP_HOST', PHP_SAPI);
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function path(): string
89
    {
90
        return \parse_url($this->get('REQUEST_URI'), PHP_URL_PATH);
91
    }
92
93
    /**
94
     * @param string $path
95
     * @param null|string $host
96
     * @param null|string $protocol
97
     * @return string
98
     */
99 5
    public static function url(string $path, ?string $host = null, ?string $protocol = null): string
100
    {
101 5
        $scheme = $protocol ?? static::sharedInstance()->protocol();
102 5
        $host = $host ?? static::sharedInstance()->host();
103 5
        return $scheme . '://' . $host . $path;
104
    }
105
106
}
107