Completed
Branch master (897f70)
by Sam
03:16 queued 01:43
created

ZoneBuilder::fillOutAaaa()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\DNAME;
16
use Badcow\DNS\Rdata\MX;
17
use Badcow\DNS\Rdata\AAAA;
18
use Badcow\DNS\Rdata\NS;
19
use Badcow\DNS\Rdata\PTR;
20
use Badcow\DNS\Rdata\RdataInterface;
21
use Badcow\DNS\Rdata\SOA;
22
use Badcow\DNS\Ip\Toolbox;
23
24
class ZoneBuilder
25
{
26
    /**
27
     * @param Zone $zone
28
     *
29
     * @return string
30
     */
31 3
    public static function build(Zone $zone): string
32
    {
33 3
        $master = '$ORIGIN '.$zone->getName().PHP_EOL;
34 3
        if (null !== $zone->getDefaultTtl()) {
35 3
            $master .= '$TTL '.$zone->getDefaultTtl().PHP_EOL;
36
        }
37
38 3
        foreach ($zone as $rr) {
39 3
            if (null !== $rr->getRdata()) {
40 3
                $master .= preg_replace('/\s+/', ' ', trim(sprintf('%s %s %s %s %s',
41 3
                    $rr->getName(),
42 3
                    $rr->getTtl(),
43 3
                    $rr->getClass(),
44 3
                    $rr->getType(),
45 3
                    $rr->getRdata()->output()
46
                )));
47
            }
48
49 3
            if (null !== $rr->getComment()) {
50 1
                $master .= '; '.$rr->getComment();
51
            }
52
53 3
            $master .= PHP_EOL;
54
        }
55
56 3
        return $master;
57
    }
58
59
    /**
60
     * Fills out all of the data of each resource record. The function will add the parent domain to all non-qualified
61
     * sub-domains, replace '@' with the zone name, ensure the class and TTL are on each record.
62
     *
63
     * @param Zone $zone
64
     */
65 1
    public static function fillOutZone(Zone $zone)
66
    {
67 1
        $class = $zone->getClass();
68
69 1
        foreach ($zone as &$rr) {
70 1
            $rr->setName(self::fullyQualify($rr->getName(), $zone->getName()));
71 1
            $rr->setTtl($rr->getTtl() ?? $zone->getDefaultTtl());
72 1
            $rr->setClass($class);
73 1
            $rdata = $rr->getRdata();
74 1
            static::fillOutRdata($rdata, $zone);
75
        }
76 1
    }
77
78
    /**
79
     * Add the parent domain to the sub-domain if the sub-domain if it is not fully qualified.
80
     *
81
     * @param string|null $subDomain
82
     * @param string      $parent
83
     *
84
     * @return string
85
     */
86 1
    protected static function fullyQualify(?string $subDomain, string $parent): string
87
    {
88 1
        if ('@' === $subDomain || null === $subDomain) {
89 1
            return $parent;
90
        }
91
92 1
        if ('.' !== substr($subDomain, -1, 1)) {
93 1
            return $subDomain.'.'.$parent;
94
        }
95
96 1
        return $subDomain;
97
    }
98
99
    /**
100
     * @param RdataInterface $rdata
101
     * @param Zone           $zone
102
     */
103 1
    protected static function fillOutRdata(RdataInterface $rdata, Zone $zone): void
104
    {
105
        $mappings = [
106 1
            SOA::TYPE => 'static::fillOutSoa',
107
            CNAME::TYPE => 'static::fillOutCname',
108
            DNAME::TYPE => 'static::fillOutCname',
109
            NS::TYPE => 'static::fillOutCname',
110
            PTR::TYPE => 'static::fillOutCname',
111
            MX::TYPE => 'static::fillOutMx',
112
            AAAA::TYPE => 'static::fillOutAaaa',
113
        ];
114
115 1
        if (!array_key_exists($rdata->getType(), $mappings)) {
116 1
            return;
117
        }
118
119
        /** @var callable $callable */
120 1
        $callable = $mappings[$rdata->getType()];
121 1
        call_user_func($callable, $rdata, $zone);
122 1
    }
123
124
    /**
125
     * @param SOA  $rdata
126
     * @param Zone $zone
127
     */
128 1
    protected static function fillOutSoa(SOA $rdata, Zone $zone): void
129
    {
130 1
        $rdata->setMname(self::fullyQualify($rdata->getMname(), $zone->getName()));
131 1
        $rdata->setRname(self::fullyQualify($rdata->getRname(), $zone->getName()));
132 1
    }
133
134
    /**
135
     * @param CNAME $rdata
136
     * @param Zone  $zone
137
     */
138 1
    protected static function fillOutCname(Cname $rdata, Zone $zone): void
139
    {
140 1
        $rdata->setTarget(self::fullyQualify($rdata->getTarget(), $zone->getName()));
141 1
    }
142
143
    /**
144
     * @param MX   $rdata
145
     * @param Zone $zone
146
     */
147 1
    protected static function fillOutMx(MX $rdata, Zone $zone): void
148
    {
149 1
        $rdata->setExchange(self::fullyQualify($rdata->getExchange(), $zone->getName()));
150 1
    }
151
152
    /**
153
     * @param AAAA $rdata
154
     * @param Zone $zone
155
     */
156 1
    protected static function fillOutAaaa(AAAA $rdata, Zone $zone): void
0 ignored issues
show
Unused Code introduced by
The parameter $zone is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158 1
        $rdata->setAddress(Toolbox::expandIpv6($rdata->getAddress()));
159 1
    }
160
}
161