LocalSystemDNS::getHostnameByAddress()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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