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 ( 15dd37...dcd5d8 )
by Freek
13s
created

Dns::getSpecificNameserverPart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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
    const NAMESERVER_DEFAULT = 'default';
12
13
    protected $domain = '';
14
    protected $nameserver = '';
15
16
    protected $recordTypes = [
17
        'A',
18
        'AAAA',
19
        'NS',
20
        'SOA',
21
        'MX',
22
        'TXT',
23
        'DNSKEY',
24
    ];
25
26
    public function __construct(string $domain, string $nameserver = '')
27
    {
28
        if (empty($domain)) {
29
            throw InvalidArgument::domainIsMissing();
30
        }
31
32
        $this->nameserver = self::NAMESERVER_DEFAULT;
33
        if (!empty($nameserver)) {
34
            $this->nameserver = $nameserver;
35
        }
36
37
38
        $this->domain = $this->sanitizeDomainName($domain);
39
    }
40
41
    public function getDomain(): string
42
    {
43
        return $this->domain;
44
    }
45
46
    public function getNameserver(): string
47
    {
48
        return $this->nameserver;
49
    }
50
51
    public function getRecords(...$types): string
52
    {
53
        $types = $this->determineTypes($types);
54
55
        $types = count($types)
56
            ? $types
57
            : $this->recordTypes;
58
59
        $dnsRecords = array_map([$this, 'getRecordsOfType'], $types);
60
61
        return implode('', array_filter($dnsRecords));
62
    }
63
64
    protected function determineTypes(array $types): array
65
    {
66
        $types = is_array($types[0] ?? null)
67
            ? $types[0]
68
            : $types;
69
70
        $types = array_map('strtoupper', $types);
71
72
        foreach ($types as $type) {
73
            if (! in_array($type, $this->recordTypes)) {
74
                throw InvalidArgument::filterIsNotAValidRecordType($type, $this->recordTypes);
75
            }
76
        }
77
78
        return $types;
79
    }
80
81
    protected function sanitizeDomainName(string $domain): string
82
    {
83
        $domain = str_replace(['http://', 'https://'], '', $domain);
84
85
        $domain = strtok($domain, '/');
86
87
        return strtolower($domain);
88
    }
89
90
    protected function getRecordsOfType(string $type): string
91
    {
92
        $nameserverPart = $this->getSpecificNameserverPart();
93
94
        $command = 'dig +nocmd '.$nameserverPart.' '.escapeshellarg($this->domain)." {$type} +multiline +noall +answer";
95
96
        $process = new Process($command);
97
98
        $process->run();
99
100
        if (! $process->isSuccessful()) {
101
            throw new Exception('Dns records could not be fetched');
102
        }
103
104
        return $process->getOutput();
105
    }
106
107
    protected function getSpecificNameserverPart()
108
    {
109
        if ($this->nameserver == self::NAMESERVER_DEFAULT) {
110
            return '';
111
        }
112
113
        return '@'.escapeshellarg($this->nameserver);
114
    }
115
}
116