Passed
Branch 2.0 (72e182)
by Philippe
02:18
created

Root   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A fromString() 0 4 1
B get() 0 9 6
A valid() 0 4 1
A secure() 0 7 1
A host() 0 4 1
A scheme() 0 4 1
B parse() 0 22 6
A __toString() 0 4 1
A parseHost() 0 10 4
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 22
    private function __construct(string $host)
20
    {
21 22
        $this->parse($host);
22
23 21
        if(false !== filter_var($this->get(), FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED))
24
        {
25 21
            $this->valid = true;
26
        }
27 21
    }
28
29 22
    public static function fromString(string $host)
30
    {
31 22
        return new self($host);
32
    }
33
34 21
    public function get()
35
    {
36 21
        $scheme = (!is_null($this->scheme)) ? $this->scheme.'://' : ($this->schemeless ? '//' : $this->defaultScheme.'://');
37 21
        $port = (!is_null($this->port)) ? ':'.$this->port : null;
38
39 21
        if($scheme == 'http://' && $this->secure) $scheme = 'https://';
40
41 21
        return $scheme.$this->host.$port;
42
    }
43
44 1
    public function valid(): bool
45
    {
46 1
        return $this->valid;
47
    }
48
49 9
    public function secure(): Root
50
    {
51 9
        $this->secure = true;
52 9
        $this->scheme = 'https';
53
54 9
        return $this;
55
    }
56
57 9
    public function host(): string
58
    {
59 9
        return $this->host;
60
    }
61
62 9
    public function scheme(): ?string
63
    {
64 9
        return $this->scheme;
65
    }
66
67 22
    private function parse(string $host)
68
    {
69
        // Sanitize url input a bit to remove double slashes, but do not remove first slashes
70 22
        if($host == '//') $host = '/';
71
72 22
        $parsed = parse_url($host);
73
74 22
        if (false === $parsed) {
75 1
            throw new InvalidUrl('Failed to parse url. Invalid url ['.$host.'] passed as parameter.');
76
        }
77
78
        // If a schemeless url is passed, parse_url will ignore this and strip the first tags
79
        // so we keep a reminder to explicitly reassemble the 'anonymous scheme' manually
80 21
        $this->schemeless = !isset($parsed['scheme']) && (0 === strpos($host, '//') && isset($parsed['host']));
81
82 21
        $this->scheme = $parsed['scheme'] ?? null;
83 21
        if($this->scheme == 'https') $this->secure();
84
85 21
        $this->host = $this->parseHost($parsed);
86 21
        $this->port = $parsed['port'] ?? null;
87
88 21
    }
89
90 1
    public function __toString(): string
91
    {
92 1
        return $this->get();
93
    }
94
95 21
    private function parseHost(array $parsed): string
96
    {
97 21
        if(isset($parsed['host'])) return $parsed['host'];
98
99 19
        if(!isset($parsed['path'])) return null;
100
101 19
        return (0 < strpos($parsed['path'],'/'))
102 1
                    ? substr($parsed['path'],0,strpos($parsed['path'],'/'))
103 19
                    : $parsed['path'];
104
    }
105
}