GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 4782a6...82f181 )
by Freek
01:31
created

Dns::determineTypes()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Spatie\Dns;
4
5
use Spatie\Dns\Exceptions\CouldNotFetchDns;
6
use Spatie\Dns\Exceptions\InvalidArgument;
7
use Symfony\Component\Process\Process;
8
9
class Dns
10
{
11
    protected string $domain = '';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
12
13
    protected string $nameserver = '';
14
15
    protected array $recordTypes = [
16
        'A',
17
        'AAAA',
18
        'CNAME',
19
        'NS',
20
        'SOA',
21
        'MX',
22
        'SRV',
23
        'TXT',
24
        'DNSKEY',
25
        'CAA',
26
        'NAPTR',
27
    ];
28
29
    public function __construct(string $domain, string $nameserver = '')
30
    {
31
        if (empty($domain)) {
32
            throw InvalidArgument::domainIsMissing();
33
        }
34
35
        $this->nameserver = $nameserver;
36
37
        $this->domain = $this->sanitizeDomainName($domain);
38
    }
39
40
    public function useNameserver(string $nameserver)
41
    {
42
        $this->nameserver = $nameserver;
43
44
        return $this;
45
    }
46
47
    public function getDomain(): string
48
    {
49
        return $this->domain;
50
    }
51
52
    public function getNameserver(): string
53
    {
54
        return $this->nameserver;
55
    }
56
57
58
    public function getRecords(...$types): string
59
    {
60
        $types = $this->determineTypes($types);
61
62
        $types = count($types)
63
            ? $types
64
            : $this->recordTypes;
65
66
        $dnsRecords = array_map([$this, 'getRecordsOfType'], $types);
67
68
        return implode('', array_filter($dnsRecords));
69
    }
70
71
    /**
72
     * @throws InvalidArgument
73
     */
74
    protected function determineTypes(array $types): array
75
    {
76
        $types = is_array($types[0] ?? null)
77
            ? $types[0]
78
            : $types;
79
80
        $types = array_map('strtoupper', $types);
81
82
        if ($invalidTypes = array_diff($types, $this->recordTypes)) {
83
            throw InvalidArgument::filterIsNotAValidRecordType(reset($invalidTypes), $this->recordTypes);
84
        }
85
86
        return $types;
87
    }
88
89
    protected function sanitizeDomainName(string $domain): string
90
    {
91
        $domain = str_replace(['http://', 'https://'], '', $domain);
92
93
        $domain = strtok($domain, '/');
94
95
        return strtolower($domain);
96
    }
97
98
    /**
99
     * @throws CouldNotFetchDns
100
     */
101
    protected function getRecordsOfType(string $type): string
102
    {
103
        $nameserverPart = $this->getSpecificNameserverPart();
104
105
        $command = array_filter([
106
            'dig',
107
            '+nocmd',
108
            $nameserverPart,
109
            $this->domain,
110
            $type,
111
            '+multiline',
112
            '+noall',
113
            '+answer',
114
        ]);
115
116
        $process = new Process($command);
117
118
        $process->run();
119
120
        if (! $process->isSuccessful()) {
121
            throw CouldNotFetchDns::digReturnedWithError(trim($process->getErrorOutput()));
122
        }
123
124
        return $process->getOutput();
125
    }
126
127
    protected function getSpecificNameserverPart(): ?string
128
    {
129
        if ($this->nameserver === '') {
130
            return null;
131
        }
132
133
        return '@'.$this->nameserver;
134
    }
135
}
136