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 ( ed4f1e...cf9ac2 )
by Freek
01:07
created

Dns::getDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Dns;
4
5
use Exception;
6
use Symfony\Component\Process\Process;
7
use Spatie\Dns\Exceptions\InvalidArgument;
8
9
class Dns
10
{
11
    protected $domain = '';
12
13
    protected $recordTypes = [
14
        'A',
15
        'AAAA',
16
        'NS',
17
        'SOA',
18
        'MX',
19
        'TXT',
20
        'DNSKEY',
21
    ];
22
23
    public function __construct(string $domain)
24
    {
25
        if (empty($domain)) {
26
            throw InvalidArgument::domainIsMissing();
27
        }
28
29
        $this->domain = $this->sanitizeDomainName($domain);
30
    }
31
32
    public function getDomain(): string
33
    {
34
        return $this->domain;
35
    }
36
37
    public function getRecords(...$types): string
38
    {
39
        $types = $this->determineTypes($types);
40
41
        $types = count($types)
42
            ? $types
43
            : $this->recordTypes;
44
45
        $dnsRecords = array_map([$this, 'getRecordsOfType'], $types);
46
47
        return implode('', array_filter($dnsRecords));
48
    }
49
50
    protected function determineTypes(array $types): array
51
    {
52
        $types = is_array($types[0] ?? null)
53
            ? $types[0]
54
            : $types;
55
56
        $types = array_map('strtoupper', $types);
57
58
        foreach ($types as $type) {
59
            if (! in_array($type, $this->recordTypes)) {
60
                throw InvalidArgument::filterIsNotAValidRecordType($type, $this->recordTypes);
61
            }
62
        }
63
64
        return $types;
65
    }
66
67
    protected function sanitizeDomainName(string $domain): string
68
    {
69
        $domain = str_replace(['http://', 'https://'], '', $domain);
70
71
        $domain = strtok($domain, '/');
72
73
        return strtolower($domain);
74
    }
75
76
    protected function getRecordsOfType(string $type): string
77
    {
78
        $command = 'dig +nocmd '.escapeshellarg($this->domain)." {$type} +multiline +noall +answer";
79
80
        $process = new Process($command);
81
82
        $process->run();
83
84
        if (! $process->isSuccessful()) {
85
            throw new Exception('Dns records could not be fetched');
86
        }
87
88
        return $process->getOutput();
89
    }
90
}
91