LocalSystemDNS   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 2
b 0
f 0
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getHostnameByAddress() 0 9 3
A getRecord() 0 12 2
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Services;
4
5
use RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure;
6
use RemotelyLiving\PHPDNS\Resolvers\Exceptions\ReverseLookupFailure;
7
use RemotelyLiving\PHPDNS\Services\Interfaces\LocalSystemDNS as LocalSystemDNSInterface;
8
9
use function dns_get_record;
10
use function gethostbyaddr;
11
12
final class LocalSystemDNS implements LocalSystemDNSInterface
13
{
14
    /**
15
     * @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure
16
     */
17
    public function getRecord(string $hostname, int $type): array
18
    {
19
        $results = @dns_get_record($hostname, $type);
20
21
        // this is untestable without creating a system networking failure
22
        // @codeCoverageIgnoreStart
23
        if ($results === false) {
24
            throw new QueryFailure();
25
        }
26
        // @codeCoverageIgnoreEnd
27
28
        return $results;
29
    }
30
31
    /**
32
     * @throws \RemotelyLiving\PHPDNS\Resolvers\Exceptions\ReverseLookupFailure
33
     */
34
    public function getHostnameByAddress(string $IPAddress): string
35
    {
36
        $hostname = @gethostbyaddr($IPAddress);
37
38
        if ($hostname === $IPAddress || $hostname === false) {
39
            throw new ReverseLookupFailure();
40
        }
41
42
        return $hostname;
43
    }
44
}
45