Completed
Push — master ( ad7ee3...c08a34 )
by Rogier
29s queued 12s
created

LocalChallengeTest::dns()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 9
rs 10
cc 3
nc 2
nop 3
1
<?php
2
3
namespace Rogierw\RwAcme\Support;
4
5
use Rogierw\RwAcme\Exceptions\DomainValidationException;
6
use Rogierw\RwAcme\Http\Client;
7
8
class LocalChallengeTest
9
{
10
    public static function http(string $domain, string $token, string $keyAuthorization): void
11
    {
12
        $httpClient = new Client(10, 1);
13
14
        $response = $httpClient->get($domain . '/.well-known/acme-challenge/' . $token);
15
16
        $body = $response->getBody();
17
18
        if (is_array($body)) {
19
            $body = json_encode($body);
20
        }
21
22
        if (trim($body) === $keyAuthorization) {
23
            return;
24
        }
25
26
        throw DomainValidationException::localHttpChallengeTestFailed(
27
            $domain,
28
            $response->getHttpResponseCode() ?? 'unknown'
29
        );
30
    }
31
32
    public static function dns(string $domain, string $name, string $value): void
33
    {
34
        $response = @dns_get_record(sprintf('%s.%s', $name, $domain), DNS_TXT);
35
36
        if (!empty($response[0]['txt']) && $response[0]['txt'] === $value) {
37
            return;
38
        }
39
40
        throw DomainValidationException::localDnsChallengeTestFailed($domain);
41
    }
42
}
43