Completed
Push — master ( 0eceaa...aee3a6 )
by Ben
15:21 queued 05:47
created

Root::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale\Values;
4
5
use Thinktomorrow\Locale\Exceptions\InvalidUrl;
6
7
class Root
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
{
9
    private $valid = false;
10
11
    private $scheme;
12
    private $schemeless = false;
13
    private $defaultScheme = 'http';
14
    private $secure = false;
15
16
    private $host;
17
    private $port;
18
19 72
    private function __construct(string $host)
20
    {
21 72
        $this->parse($host);
22
23 71
        if (false !== filter_var($this->get(), FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
24 71
            $this->valid = true;
25
        }
26 71
    }
27
28 72
    public static function fromString(string $host)
29
    {
30 72
        return new self($host);
31
    }
32
33 71
    public function get()
34
    {
35 71
        $scheme = (!is_null($this->scheme)) ? $this->scheme.'://' : ($this->schemeless ? '//' : $this->defaultScheme.'://');
36 71
        $port = (!is_null($this->port)) ? ':'.$this->port : null;
37
38 71
        if ($scheme == 'http://' && $this->secure) {
39
            $scheme = 'https://';
40
        }
41
42 71
        return $scheme.$this->host.$port;
43
    }
44
45 1
    public function valid(): bool
46
    {
47 1
        return $this->valid;
48
    }
49
50 13
    public function secure(): self
51
    {
52 13
        $this->secure = true;
53 13
        $this->scheme = 'https';
54
55 13
        return $this;
56
    }
57
58 9
    public function host(): string
59
    {
60 9
        return $this->host;
61
    }
62
63 9
    public function scheme(): ?string
64
    {
65 9
        return $this->scheme;
66
    }
67
68 72
    private function parse(string $host)
69
    {
70
        // Sanitize url input a bit to remove double slashes, but do not remove first slashes
71 72
        if ($host == '//') {
72 1
            $host = '/';
73
        }
74
75 72
        $parsed = parse_url($host);
76
77 72
        if (false === $parsed) {
78 1
            throw new InvalidUrl('Failed to parse url. Invalid url ['.$host.'] passed as parameter.');
79
        }
80
81
        // If a schemeless url is passed, parse_url will ignore this and strip the first tags
82
        // so we keep a reminder to explicitly reassemble the 'anonymous scheme' manually
83 71
        $this->schemeless = !isset($parsed['scheme']) && (0 === strpos($host, '//') && isset($parsed['host']));
84
85 71
        $this->scheme = $parsed['scheme'] ?? null;
86 71
        if ($this->scheme == 'https') {
87 13
            $this->secure();
88
        }
89
90 71
        $this->host = $this->parseHost($parsed);
91 71
        $this->port = $parsed['port'] ?? null;
92 71
    }
93
94 1
    public function __toString(): string
95
    {
96 1
        return $this->get();
97
    }
98
99 71
    private function parseHost(array $parsed): string
100
    {
101 71
        if (isset($parsed['host'])) {
102 68
            return $parsed['host'];
103
        }
104
105 14
        if (!isset($parsed['path'])) {
106
            return null;
107
        }
108
109 14
        return (0 < strpos($parsed['path'], '/'))
110 1
                    ? substr($parsed['path'], 0, strpos($parsed['path'], '/'))
111 14
                    : $parsed['path'];
112
    }
113
}
114