Dig   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A toDNSRecord() 0 40 1
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Mappers;
4
5
use RemotelyLiving\PHPDNS\Entities\CAAData;
6
use RemotelyLiving\PHPDNS\Entities\CNAMEData;
7
use RemotelyLiving\PHPDNS\Entities\DNSRecord;
8
use RemotelyLiving\PHPDNS\Entities\Hostname;
9
use RemotelyLiving\PHPDNS\Entities\Interfaces\DNSRecordInterface;
10
use RemotelyLiving\PHPDNS\Entities\MXData;
11
use RemotelyLiving\PHPDNS\Entities\NSData;
12
use RemotelyLiving\PHPDNS\Entities\SOAData;
13
use RemotelyLiving\PHPDNS\Entities\SRVData;
14
use RemotelyLiving\PHPDNS\Entities\TXTData;
15
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
16
17
final class Dig extends MapperAbstract
18
{
19
    public function toDNSRecord(): DNSRecordInterface
20
    {
21
        $baseRecord = DNSRecord::createFromPrimitives(
22
            $this->fields['type'],
23
            $this->fields['host'],
24
            $this->fields['ttl'],
25
            $this->fields['ip'] ?? $this->fields['ipv6'] ?? null,
26
            $this->fields['class'],
27
        );
28
29
        return match ((string) $this->fields['type']) {
30
            'A', 'AAAA' => $baseRecord,
31
            'CAA' => $baseRecord
32
               ->setData(new CAAData($this->fields['flags'], $this->fields['tag'], $this->fields['value'])),
33
            'CNAME' => $baseRecord
34
               ->setData(new CNAMEData(Hostname::createFromString($this->fields['target']))),
35
            'MX' => $baseRecord
36
               ->setData(new MXData(Hostname::createFromString($this->fields['target']), $this->fields['pri'])),
37
            'NS' => $baseRecord
38
               ->setData(new NSData(Hostname::createFromString($this->fields['target']))),
39
            'SOA' => $baseRecord
40
               ->setData(new SOAData(
41
                   Hostname::createFromString($this->fields['mname']),
42
                   Hostname::createFromString($this->fields['rname']),
43
                   $this->fields['serial'],
44
                   $this->fields['refresh'],
45
                   $this->fields['retry'],
46
                   $this->fields['expire'],
47
                   $this->fields['minimum_ttl'],
48
               )),
49
            'SRV' => $baseRecord
50
               ->setData(new SRVData(
51
                   $this->fields['pri'],
52
                   $this->fields['weight'],
53
                   $this->fields['port'],
54
                   Hostname::createFromString($this->fields['target'])
55
               )),
56
            'TXT' => $baseRecord
57
               ->setData(new TXTData($this->fields['txt'])),
58
            default => throw new InvalidArgumentException($this->fields['type'] . ' is not supported by dig')
59
        };
60
    }
61
}
62