Completed
Branch phpstan (be7aa3)
by Sam
01:34
created

ZoneBuilder::fillOutMx()   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\MX;
16
use Badcow\DNS\Rdata\AAAA;
17
use Badcow\DNS\Rdata\RdataInterface;
18
use Badcow\DNS\Rdata\SOA;
19
use Badcow\DNS\Ip\Toolbox;
20
21
class ZoneBuilder
22
{
23
    /**
24
     * @param Zone $zone
25
     *
26
     * @return string
27
     */
28 3
    public static function build(Zone $zone): string
29
    {
30 3
        $master = '$ORIGIN '.$zone->getName().PHP_EOL;
31 3
        if (null !== $zone->getDefaultTtl()) {
32 3
            $master .= '$TTL '.$zone->getDefaultTtl().PHP_EOL;
33
        }
34
35 3
        foreach ($zone as $rr) {
36 3
            if (null !== $rr->getRdata()) {
37 3
                $master .= preg_replace('/\s+/', ' ', trim(sprintf('%s %s %s %s %s',
38 3
                    $rr->getName(),
39 3
                    $rr->getTtl(),
40 3
                    $rr->getClass(),
41 3
                    $rr->getType(),
42 3
                    $rr->getRdata()->output()
43
                )));
44
            }
45
46 3
            if (null !== $rr->getComment()) {
47 1
                $master .= '; '.$rr->getComment();
48
            }
49
50 3
            $master .= PHP_EOL;
51
        }
52
53 3
        return $master;
54
    }
55
56
    /**
57
     * Fills out all of the data of each resource record. The function will add the parent domain to all non-qualified
58
     * sub-domains, replace '@' with the zone name, ensure the class and TTL are on each record.
59
     *
60
     * @param Zone $zone
61
     */
62 1
    public static function fillOutZone(Zone $zone)
63
    {
64 1
        $class = $zone->getClass();
65
66 1
        foreach ($zone as &$rr) {
67 1
            $rr->setName(self::fullyQualify($rr->getName(), $zone->getName()));
68 1
            $rr->setTtl($rr->getTtl() ?? $zone->getDefaultTtl());
69 1
            $rr->setClass($class);
70 1
            $rdata = $rr->getRdata();
71 1
            static::fillOutRdata($rdata, $zone);
0 ignored issues
show
Bug introduced by
Since fillOutRdata() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of fillOutRdata() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
72
        }
73 1
    }
74
75
    /**
76
     * Add the parent domain to the sub-domain if the sub-domain if it is not fully qualified.
77
     *
78
     * @param string|null $subDomain
79
     * @param string      $parent
80
     *
81
     * @return string
82
     */
83 1
    private static function fullyQualify(?string $subDomain, string $parent): string
84
    {
85 1
        if ('@' === $subDomain || null === $subDomain) {
86 1
            return $parent;
87
        }
88
89 1
        if ('.' !== substr($subDomain, -1, 1)) {
90 1
            return $subDomain.'.'.$parent;
91
        }
92
93 1
        return $subDomain;
94
    }
95
96
    /**
97
     * @param RdataInterface $rdata
98
     * @param Zone           $zone
99
     */
100 1
    private static function fillOutRdata(RdataInterface $rdata, Zone $zone): void
101
    {
102
        $mappings = [
103 1
            SOA::class => 'static::fillOutSoa',
104
            Cname::class => 'static::fillOutCname',
105
            MX::class => 'static::fillOutMx',
106
            AAAA::class => 'static::fillOutAaaa',
107
        ];
108
109 1
        foreach ($mappings as $class => $callable) {
110 1
            if (!is_callable($callable)) {
111
                throw new \InvalidArgumentException(sprintf('The argument "%s" is not callable.', $callable));
112
            }
113
114 1
            if ($rdata instanceof $class) {
115 1
                call_user_func($callable, $rdata, $zone);
116
            }
117
        }
118 1
    }
119
120
    /**
121
     * @param SOA  $rdata
122
     * @param Zone $zone
123
     */
124 1
    private static function fillOutSoa(SOA $rdata, Zone $zone): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
125
    {
126 1
        $rdata->setMname(self::fullyQualify($rdata->getMname(), $zone->getName()));
127 1
        $rdata->setRname(self::fullyQualify($rdata->getRname(), $zone->getName()));
128 1
    }
129
130
    /**
131
     * @param CNAME $rdata
132
     * @param Zone  $zone
133
     */
134 1
    private static function fillOutCname(Cname $rdata, Zone $zone): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
135
    {
136 1
        $rdata->setTarget(self::fullyQualify($rdata->getTarget(), $zone->getName()));
137 1
    }
138
139
    /**
140
     * @param MX   $rdata
141
     * @param Zone $zone
142
     */
143 1
    private static function fillOutMx(MX $rdata, Zone $zone): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
144
    {
145 1
        $rdata->setExchange(self::fullyQualify($rdata->getExchange(), $zone->getName()));
146 1
    }
147
148
    /**
149
     * @param AAAA $rdata
150
     * @param Zone $zone
151
     */
152 1
    private static function fillOutAaaa(AAAA $rdata, Zone $zone): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
153
    {
154 1
        $rdata->setAddress(Toolbox::expandIpv6($rdata->getAddress()));
155 1
    }
156
}
157