Completed
Push — master ( 282f78...6dce89 )
by Sam
05:20
created

XmlResolver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace yswery\DNS\Resolver;
4
5
use yswery\DNS\ClassEnum;
6
use yswery\DNS\ResourceRecord;
7
use yswery\DNS\RecordTypeEnum;
8
use yswery\DNS\UnsupportedTypeException;
9
10
class XmlResolver extends AbstractResolver
11
{
12
    private $defaultClass = ClassEnum::INTERNET;
13
14
    /**
15
     * XmlResolver constructor.
16
     *
17
     * @param array $files
18
     *
19
     * @throws UnsupportedTypeException
20
     */
21
    public function __construct(array $files)
22
    {
23
        $this->isAuthoritative = true;
24
        $this->allowRecursion = false;
25
26
        foreach ($files as $file) {
27
            $xml = new \SimpleXMLElement(file_get_contents($file));
28
            $this->addZone($this->process($xml));
29
        }
30
    }
31
32
    /**
33
     * @param object $xml
34
     *
35
     * @return ResourceRecord[]
36
     *
37
     * @throws UnsupportedTypeException
38
     */
39
    private function process($xml): array
40
    {
41
        $parent = (string) $xml->{'name'};
42
        $defaultTtl = (int) $xml->{'default-ttl'};
43
        $resourceRecords = [];
44
45
        foreach ($xml->{'resource-records'}->{'resource-record'} as $rr) {
46
            $name = (string) $rr->{'name'} ?? $parent;
47
            $class = isset($rr->{'class'}) ? ClassEnum::getClassFromName($rr->{'class'}) : $this->defaultClass;
48
            $ttl = isset($rr->{'ttl'}) ? (int) $rr->{'ttl'} : $defaultTtl;
49
50
            $resourceRecords[] = (new ResourceRecord())
51
                ->setName($this->handleName($name, $parent))
52
                ->setClass($class)
53
                ->setType($type = RecordTypeEnum::getTypeIndex($rr->{'type'}))
0 ignored issues
show
Bug introduced by
It seems like $type = yswery\DNS\Recor...tTypeIndex($rr->'type') can also be of type false; however, parameter $type of yswery\DNS\ResourceRecord::setType() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                ->setType(/** @scrutinizer ignore-type */ $type = RecordTypeEnum::getTypeIndex($rr->{'type'}))
Loading history...
54
                ->setTtl($ttl)
55
                ->setRdata($this->extractRdata($this->simpleXmlToArray($rr->{'rdata'}), $type, $parent));
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type false; however, parameter $type of yswery\DNS\Resolver\Abst...esolver::extractRdata() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                ->setRdata($this->extractRdata($this->simpleXmlToArray($rr->{'rdata'}), /** @scrutinizer ignore-type */ $type, $parent));
Loading history...
56
        }
57
58
        return $resourceRecords;
59
    }
60
61
    /**
62
     * Convert a SimpleXML object to an associative array.
63
     *
64
     * @param \SimpleXMLElement $xmlObject
65
     *
66
     * @return array
67
     */
68
    private function simpleXmlToArray(\SimpleXMLElement $xmlObject): array
69
    {
70
        $array = [];
71
        foreach ($xmlObject->children() as $node) {
72
            $array[$node->getName()] = is_array($node) ? $this->simpleXmlToArray($node) : (string) $node;
0 ignored issues
show
Bug introduced by
$node of type array is incompatible with the type SimpleXMLElement expected by parameter $xmlObject of yswery\DNS\Resolver\XmlR...ver::simpleXmlToArray(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $array[$node->getName()] = is_array($node) ? $this->simpleXmlToArray(/** @scrutinizer ignore-type */ $node) : (string) $node;
Loading history...
73
        }
74
75
        return $array;
76
    }
77
}
78