Completed
Push — master ( 1d13ba...f9bf31 )
by Sam
49:13
created

Zone::expand()   F

Complexity

Conditions 12
Paths 401

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 3.6319
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 12
nc 401
nop 0
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Ip\Toolbox;
15
use Badcow\DNS\Rdata\AAAA;
16
use Badcow\DNS\Rdata\CNAME;
17
use Badcow\DNS\Rdata\MX;
18
use http\Exception\InvalidArgumentException;
19
20
class Zone implements ZoneInterface
21
{
22
    use ZoneTrait;
23 24
24
    /**
25 24
     * Zone constructor.
26 21
     * @param string $name
27 14
     * @param integer $defaultTtl
28
     * @param array $resourceRecords
29 24
     * @throws \InvalidArgumentException
30 24
     */
31 24
    public function __construct($name = null, $defaultTtl = null, array $resourceRecords = [])
32
    {
33
        if (null !== $name) {
34
            $this->setName($name);
35
        }
36
37
        $this->setDefaultTtl($defaultTtl);
38 24
        $this->setResourceRecords($resourceRecords);
39
    }
40 24
41 3
    /**
42
     * @param string $name A fully qualified zone name
43
     *
44 24
     * @throws \InvalidArgumentException
45 24
     */
46
    public function setName($name)
47
    {
48
        if (!Validator::rrName($name, true)) {
49
            throw new \InvalidArgumentException(sprintf('Zone "%s" is not a fully qualified domain name.', $name));
50
        }
51
52
        $this->name = $name;
53
    }
54
55
    /**
56
     * Fills out all of the data of each resource record.
57
     */
58
    public function expand()
59
    {
60
        $class = $this->determineClass();
61
62
        foreach ($this->resourceRecords as &$rr) {
63
            /** @var ResourceRecord $rr  */
64
65
            if ('@' === $rr->getName()) {
66
                $rr->setName($this->name);
67
            }
68
69
            if (!Validator::fqdn($rr->getName())) {
70
                $rr->setName($rr->getName() . '.' . $this->name);
71
            }
72
73
            if (null === $rr->getTtl()) {
74
                $rr->setTtl($this->getDefaultTtl());
75
            }
76
77
            if ($rr->getRdata() instanceof CNAME) {
78
                if ('@' === $rr->getRdata()->getTarget()) {
79
                    $rr->getRdata()->setTarget($this->name);
80
                }
81
82
                if (!Validator::fqdn($rr->getRdata()->getTarget())) {
83
                    $rr->getRdata()->setTarget($rr->getRdata()->getTarget() . '.' . $this->name);
84
                }
85
            }
86
87
            if ($rr->getRdata() instanceof MX) {
88
                if ('@' === $rr->getRdata()->getExchange()) {
89
                    $rr->getRdata()->setExchange($this->name);
90
                }
91
92
                if (!Validator::fqdn($rr->getRdata()->getExchange())) {
93
                    $rr->getRdata()->setExchange($rr->getRdata()->getExchange() . '.' . $this->name);
94
                }
95
            }
96
97
            if ($rr->getRdata() instanceof AAAA) {
98
                $rr->getRdata()->setAddress(Toolbox::expandIpv6($rr->getRdata()->getAddress()));
99
            }
100
101
            $rr->setClass($class);
102
        }
103
    }
104
105
    /**
106
     * Determine in which class the zone resides. Returns `IN` as the default.
107
     *
108
     * @return string
109
     */
110
    private function determineClass()
111
    {
112
        foreach ($this->resourceRecords as $rr) {
113
            if (null !== $rr->getClass()) {
114
                return $rr->getClass();
115
            }
116
        }
117
118
        return Classes::INTERNET;
119
    }
120
}
121