DNSQueried   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 8 1
A getHostname() 0 3 1
A getResolver() 0 3 1
A __construct() 0 8 1
A getRecordType() 0 3 1
A formatCollection() 0 11 3
A getName() 0 3 1
A getRecordCollection() 0 3 1
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