Passed
Pull Request — master (#5)
by Christian
02:09
created

LocalSystem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeCodeFromType() 0 3 1
B toDNSRecord() 0 46 8
1
<?php
2
namespace RemotelyLiving\PHPDNS\Mappers;
3
4
use RemotelyLiving\PHPDNS\Entities\DNSRecord;
5
use RemotelyLiving\PHPDNS\Entities\DNSRecordType;
6
7
class LocalSystem extends MapperAbstract
8
{
9
    private const PHP_CODE_TYPE_MAP = [
10
        \DNS_A => DNSRecordType::TYPE_A,
11
        \DNS_CNAME => DNSRecordType::TYPE_CNAME,
12
        \DNS_HINFO => DNSRecordType::TYPE_HINFO,
13
        \DNS_CAA => DNSRecordType::TYPE_CAA,
14
        \DNS_MX => DNSRecordType::TYPE_MX,
15
        \DNS_NS => DNSRecordType::TYPE_NS,
16
        \DNS_PTR => DNSRecordType::TYPE_PTR,
17
        \DNS_SOA => DNSRecordType::TYPE_SOA,
18
        \DNS_TXT => DNSRecordType::TYPE_TXT,
19
        \DNS_AAAA => DNSRecordType::TYPE_AAAA,
20
        \DNS_SRV => DNSRecordType::TYPE_SRV,
21
        \DNS_NAPTR => DNSRecordType::TYPE_NAPTR,
22
        \DNS_A6 => DNSRecordType::TYPE_A6,
23
        \DNS_ANY => DNSRecordType::TYPE_ANY,
24
    ];
25
26
    public function toDNSRecord(): DNSRecord
27
    {
28
        $IPAddress = null;
29
        $data = null;
30
31
        if (isset($this->fields['ipv6'])) {
32
            $IPAddress = $this->fields['ipv6'];
33
        }
34
35
        if (isset($this->fields['ip'])) {
36
            $IPAddress = $this->fields['ip'];
37
        }
38
39
        if (isset($this->fields['txt'])) {
40
            $data = $this->fields['txt'];
41
        }
42
43
        if (isset($this->fields['target'])) {
44
            $data = $this->fields['target'];
45
        }
46
47
        if (isset($this->fields['target']) && isset($this->fields['pri'])) {
48
            $data = "{$this->fields['pri']} {$this->fields['target']}";
49
        }
50
51
        if (isset($this->fields['mname'])) {
52
            $template = '%s %s %s %s %s %s %s';
53
            $data = sprintf(
54
                $template,
55
                $this->fields['mname'],
56
                $this->fields['rname'],
57
                $this->fields['serial'],
58
                $this->fields['refresh'],
59
                $this->fields['retry'],
60
                $this->fields['expire'],
61
                $this->fields['minimum-ttl']
62
            );
63
        }
64
65
        return DNSRecord::createFromPrimitives(
66
            $this->fields['type'],
67
            $this->fields['host'],
68
            $this->fields['ttl'],
69
            $IPAddress,
70
            $this->fields['class'],
71
            $data
72
        );
73
    }
74
75
    public function getTypeCodeFromType(DNSRecordType $type): int
76
    {
77
        return array_flip(self::PHP_CODE_TYPE_MAP)[(string)$type] ?? \DNS_ANY;
78
    }
79
}
80