DNSQueried::getResolver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Observability\Events;
4
5
use RemotelyLiving\PHPDNS\Entities\DNSRecordCollection;
6
use RemotelyLiving\PHPDNS\Entities\DNSRecordType;
7
use RemotelyLiving\PHPDNS\Entities\Hostname;
8
use RemotelyLiving\PHPDNS\Resolvers\Interfaces\Resolver;
9
10
final class DNSQueried extends ObservableEventAbstract
11
{
12
    public const NAME = 'dns.queried';
13
14
    private DNSRecordCollection $recordCollection;
15
16
    public function __construct(
17
        private Resolver $resolver,
18
        private Hostname $hostname,
19
        private DNSRecordType $recordType,
20
        DNSRecordCollection $recordCollection = null
21
    ) {
22
        parent::__construct();
23
        $this->recordCollection = $recordCollection ?? new DNSRecordCollection();
24
    }
25
26
    public function getResolver(): Resolver
27
    {
28
        return $this->resolver;
29
    }
30
31
    public function getHostname(): Hostname
32
    {
33
        return $this->hostname;
34
    }
35
36
    public function getRecordType(): DNSRecordType
37
    {
38
        return $this->recordType;
39
    }
40
41
    public function getRecordCollection(): DNSRecordCollection
42
    {
43
        return $this->recordCollection;
44
    }
45
46
    public static function getName(): string
47
    {
48
        return self::NAME;
49
    }
50
51
    public function toArray(): array
52
    {
53
        return [
54
            'resolver' => $this->resolver->getName(),
55
            'hostname' => (string)$this->hostname,
56
            'type' => (string)$this->recordType,
57
            'records' => $this->formatCollection($this->recordCollection),
58
            'empty' => $this->recordCollection->isEmpty(),
59
        ];
60
    }
61
62
    private function formatCollection(DNSRecordCollection $recordCollection): array
63
    {
64
        $formatted = [];
65
66
        foreach ($recordCollection as $record) {
67
            if ($record !== null) {
68
                $formatted[] = $record->toArray();
69
            }
70
        }
71
72
        return $formatted;
73
    }
74
}
75