Passed
Push — master ( 956163...dedbc7 )
by Christian
03:02
created

Hostname::toLocal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\DigitalOcean;
4
5
use Assert\Assertion;
6
use DigitalOceanV2\Entity;
7
8
final class Hostname
9
{
10
11
    const DIGITAL_OCEAN_ROOT_RECORD = '@';
12
    const LOCAL_TOP_LEVEL_DOMAIN = 'local';
13
14
    /**
15
     * @var string
16
     */
17
    private $lowerLevelDomains;
18
19
    /**
20
     * @var string
21
     */
22
    private $secondLevelDomain;
23
24
    /**
25
     * @var string
26
     */
27
    private $topLevelDomain;
28
29
    private function __construct(string $lowerLevelDomains, string $secondLevelDomain, string $topLevelDomain)
30
    {
31
        $this->lowerLevelDomains = $lowerLevelDomains;
32
        $this->secondLevelDomain = $secondLevelDomain;
33
        $this->topLevelDomain = $topLevelDomain;
34
    }
35
36
    public static function parse(string $value): self
37
    {
38
        $value = trim($value);
39
40
        Assertion::notEmpty($value, "The hostname is empty");
41
42
        $domains = explode('.', $value);
43
44
        if (count($domains) < 2) {
45
            throw new \Exception("'$value' does not have a first and second level domains");
46
        }
47
        if (count($domains) > 3) {
48
            throw new \Exception("'$value' is a domain with more than 3 levels.");
49
        }
50
51
        if (2 === count($domains)) {
52
            array_unshift($domains, self::DIGITAL_OCEAN_ROOT_RECORD);
53
        }
54
55
        return new self($domains[0], $domains[1], $domains[2]);
56
    }
57
58
    public function domainName(): string
59
    {
60
        return sprintf(
61
            "%s.%s",
62
            $this->secondLevelDomain,
63
            $this->topLevelDomain
64
        );
65
    }
66
67
    public function recordName(): string
68
    {
69
        return $this->lowerLevelDomains;
70
    }
71
72
    public function rawValue(): string
73
    {
74
        return sprintf(
75
            "%s.%s.%s",
76
            $this->lowerLevelDomains,
77
            $this->secondLevelDomain,
78
            $this->topLevelDomain
79
        );
80
    }
81
82
    public function toString(): string
83
    {
84
        if ($this->isRoot()) {
85
            return $this->domainName();
86
        } else {
87
            return $this->rawValue();
88
        }
89
    }
90
91
    public function matchDomainRecord(Entity\DomainRecord $domainRecord): bool
92
    {
93
        return $domainRecord->name === $this->recordName();
94
    }
95
96
    public function toRoot(): self
97
    {
98
        return self::parse($this->domainName());
99
    }
100
101
    public function isRoot(): bool
102
    {
103
        return self::DIGITAL_OCEAN_ROOT_RECORD === $this->lowerLevelDomains;
104
    }
105
106
    public function __toString()
107
    {
108
        return $this->toString();
109
    }
110
111
    public function isLocal()
112
    {
113
        return self::LOCAL_TOP_LEVEL_DOMAIN === $this->topLevelDomain;
114
    }
115
116
    public function toLocal(): Hostname
117
    {
118
        if ($this->isLocal()) {
119
            return $this;
120
        } else {
121
            return new self(
122
                $this->lowerLevelDomains,
123
                $this->secondLevelDomain,
124
                self::LOCAL_TOP_LEVEL_DOMAIN
125
            );
126
        }
127
    }
128
129
}