Completed
Branch v2 (e11fee)
by Sam
01:28
created

ZoneBuilder::fullyQualify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badcow\DNS;
13
14
use Badcow\DNS\Rdata\CNAME;
15
use Badcow\DNS\Rdata\MX;
16
use Badcow\DNS\Rdata\AAAA;
17
use Badcow\DNS\Rdata\SOA;
18
use Badcow\DNS\Ip\Toolbox;
19
20
class ZoneBuilder
21
{
22
    /**
23
     * @param Zone $zone
24
     *
25
     * @return string
26
     */
27 3
    public static function build(Zone $zone): string
28
    {
29 3
        $master = '$ORIGIN '.$zone->getName().PHP_EOL;
30 3
        if (null !== $zone->getDefaultTtl()) {
31 3
            $master .= '$TTL '.$zone->getDefaultTtl().PHP_EOL;
32
        }
33
34 3
        foreach ($zone as $rr) {
35 3
            if (null !== $rr->getRdata()) {
36 3
                $master .= preg_replace('/\s+/', ' ', trim(sprintf('%s %s %s %s %s',
37 3
                    $rr->getName(),
38 3
                    $rr->getTtl(),
39 3
                    $rr->getClass(),
40 3
                    $rr->getType(),
41 3
                    $rr->getRdata()->output()
42
                )));
43
            }
44
45 3
            if (null !== $rr->getComment()) {
46 1
                $master .= '; '.$rr->getComment();
47
            }
48
49 3
            $master .= PHP_EOL;
50
        }
51
52 3
        return $master;
53
    }
54
55
    /**
56
     * Fills out all of the data of each resource record. The function will add the parent domain to all non-qualified
57
     * sub-domains, replace '@' with the zone name, ensure the class and TTL are on each record.
58
     *
59
     * @param Zone $zone
60
     */
61
    public static function fillOutZone(Zone $zone)
62
    {
63
        $class = Classes::INTERNET;
64
        foreach ($zone as $rr) {
65
            if (null !== $class = $rr->getClass()) {
66
                break;
67
            }
68
        }
69
70
        foreach ($zone as &$rr) {
71
            $rr->setName(self::fullyQualify($rr->getName(), $zone->getName()));
72
            $rr->setTtl($rr->getTtl() ?? $zone->getDefaultTtl());
73
74
            if ($rr->getRdata() instanceof SOA) {
75
                $rr->getRdata()->setMname(self::fullyQualify($rr->getRdata()->getMname(), $zone->getName()));
76
                $rr->getRdata()->setRname(self::fullyQualify($rr->getRdata()->getRname(), $zone->getName()));
77
            }
78
79
            if ($rr->getRdata() instanceof CNAME) {
80
                $rr->getRdata()->setTarget(self::fullyQualify($rr->getRdata()->getTarget(), $zone->getName()));
81
            }
82
83
            if ($rr->getRdata() instanceof MX) {
84
                $rr->getRdata()->setExchange(self::fullyQualify($rr->getRdata()->getExchange(), $zone->getName()));
85
            }
86
87
            if ($rr->getRdata() instanceof AAAA) {
88
                $rr->getRdata()->setAddress(Toolbox::expandIpv6($rr->getRdata()->getAddress()));
89
            }
90
91
            $rr->setClass($class);
92
        }
93
    }
94
95
    /**
96
     * Add the parent domain to the sub-domain if the sub-domain if it is not fully qualified.
97
     *
98
     * @param string $subdomain
99
     * @param string $parent
100
     *
101
     * @return string
102
     */
103
    private static function fullyQualify(string $subdomain, string $parent): string
104
    {
105
        if ('@' === $subdomain) {
106
            return $parent;
107
        }
108
109
        if ('.' !== substr($subdomain, -1, 1)) {
110
            return $subdomain.'.'.$parent;
111
        }
112
113
        return $subdomain;
114
    }
115
}
116