Dig   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupportedQueryType() 0 13 4
A __construct() 0 12 2
A doQuery() 0 15 4
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\Factories\SpatieDNS;
0 ignored issues
show
Bug introduced by
The type RemotelyLiving\PHPDNS\Factories\SpatieDNS was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use RemotelyLiving\PHPDNS\Mappers\Dig as DigMapper;
10
use RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure;
11
use Spatie\Dns\Dns;
12
use Spatie\Dns\Records\Record;
13
use Throwable;
14
15
use function array_slice;
16
use function explode;
17
use function implode;
18
use function preg_replace;
19
use function trim;
20
21
final class Dig extends ResolverAbstract
22
{
23
    public const SUPPORTED_QUERY_TYPES = [
24
        DNSRecordType::TYPE_A,
25
        DNSRecordType::TYPE_AAAA,
26
        DNSRecordType::TYPE_CNAME,
27
        DNSRecordType::TYPE_NS,
28
        DNSRecordType::TYPE_SOA,
29
        DNSRecordType::TYPE_MX,
30
        DNSRecordType::TYPE_SRV,
31
        DNSRecordType::TYPE_TXT,
32
        DNSRecordType::TYPE_CAA,
33
    ];
34
35
    private Dns $dig;
36
37
    private DigMapper $mapper;
38
39
    public function __construct(
40
        Dns $dig = null,
41
        DigMapper $mapper = null,
42
        Hostname $nameserver = null
43
    ) {
44
        $this->dig = $dig ?? new Dns();
45
46
        if ($nameserver !== null) {
47
            $this->dig = $this->dig->useNameserver((string) $nameserver);
48
        }
49
50
        $this->mapper = $mapper ?? new DigMapper();
51
    }
52
53
    protected function doQuery(Hostname $hostname, DNSRecordType $recordType): DNSRecordCollection
54
    {
55
        if (!self::isSupportedQueryType($recordType)) {
56
            return new DNSRecordCollection();
57
        }
58
59
        try {
60
            $response = ($recordType->equals(DNSRecordType::createANY()))
61
                ? $this->dig->getRecords((string) $hostname, self::SUPPORTED_QUERY_TYPES)
62
                : $this->dig->getRecords((string) $hostname, (string) $recordType);
63
        } catch (Throwable $e) {
64
            throw new QueryFailure($e->getMessage(), 0, $e);
65
        }
66
67
        return $this->mapResults($this->mapper, array_map(fn(Record $record): array => $record->toArray(), $response));
68
    }
69
70
    private static function isSupportedQueryType(DNSRecordType $type): bool
71
    {
72
        if ($type->isA(DNSRecordType::TYPE_ANY)) {
73
            return true;
74
        }
75
76
        foreach (self::SUPPORTED_QUERY_TYPES as $queryType) {
77
            if ($type->isA($queryType)) {
78
                return true;
79
            }
80
        }
81
82
        return false;
83
    }
84
}
85