GooglePublicDNS::verify()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 20
rs 9.6111
cc 5
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the LetsEncrypt ACME client.
7
 *
8
 * @author    Ivanov Aleksandr <[email protected]>
9
 * @copyright 2019-2020
10
 * @license   https://github.com/misantron/letsencrypt-client/blob/master/LICENSE MIT License
11
 */
12
13
namespace LetsEncrypt\Http;
14
15
/**
16
 * Class GooglePublicDNS.
17
 *
18
 * @see https://developers.google.com/speed/public-dns/docs/doh/json
19
 */
20
final class GooglePublicDNS implements DnsCheckerInterface
21
{
22
    use ConnectorAwareTrait;
23
24
    private const BASE_URI = 'https://dns.google.com/resolve';
25
26
    private const STATUS_OK = 0;
27
    private const DNS_TYPE_TXT = 16;
28
29
    public function verify(string $domain, string $dnsDigest): bool
30
    {
31
        $query = [
32
            'type' => 'TXT',
33
            'name' => '_acme-challenge.' . $domain,
34
        ];
35
36
        $data = $this->connector
37
            ->get(self::BASE_URI . '?' . http_build_query($query))
38
            ->getDecodedContent();
39
40
        if ($data['Status'] === self::STATUS_OK) {
41
            foreach ($data['Answer'] ?? [] as $answer) {
42
                if ($answer['type'] === self::DNS_TYPE_TXT && $answer['data'] === '"' . $dnsDigest . '"') {
43
                    return true;
44
                }
45
            }
46
        }
47
48
        return false;
49
    }
50
}
51