LocalSystem   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHostnameByAddress() 0 5 1
A doQuery() 0 14 2
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Resolvers;
4
5
use RemotelyLiving\PHPDNS\Entities\DNSRecordCollection;
6
use RemotelyLiving\PHPDNS\Entities\DNSRecordType;
7
use RemotelyLiving\PHPDNS\Entities\Hostname;
8
use RemotelyLiving\PHPDNS\Entities\IPAddress;
9
use RemotelyLiving\PHPDNS\Mappers\LocalSystem as LocalMapper;
10
use RemotelyLiving\PHPDNS\Resolvers\Interfaces\ReverseDNSQuery;
11
use RemotelyLiving\PHPDNS\Services\Interfaces\LocalSystemDNS;
12
use RemotelyLiving\PHPDNS\Services\LocalSystemDNS as LocalDNSService;
13
14
final class LocalSystem extends ResolverAbstract implements ReverseDNSQuery
15
{
16
    private LocalSystemDNS $systemDNS;
17
18
    private LocalMapper $mapper;
19
20
    public function __construct(LocalSystemDNS $systemDNS = null, LocalMapper $mapper = null)
21
    {
22
        $this->systemDNS = $systemDNS ?? new LocalDNSService();
23
        $this->mapper = $mapper ?? new LocalMapper();
24
    }
25
26
    public function getHostnameByAddress(string $IPAddress): Hostname
27
    {
28
        $result = $this->systemDNS->getHostnameByAddress((string)(new IPAddress($IPAddress)));
29
30
        return Hostname::createFromString($result);
31
    }
32
33
    protected function doQuery(Hostname $hostname, DNSRecordType $recordType): DNSRecordCollection
34
    {
35
        $results = $this->systemDNS->getRecord(
36
            $hostname->getHostnameWithoutTrailingDot(), // dns_get_record doesn't like trailing dot as much!
37
            $this->mapper->getTypeCodeFromType($recordType)
38
        );
39
40
        $collection = new DNSRecordCollection();
41
42
        foreach ($results as $result) {
43
            $collection[] = $this->mapper->mapFields($result)->toDNSRecord();
44
        }
45
46
        return $collection;
47
    }
48
}
49