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

LocalChallengeTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 6
eloc 15
c 4
b 1
f 2
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A http() 0 19 3
A dns() 0 9 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