ChallengeHTTP   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
dl 0
loc 42
ccs 0
cts 19
cp 0
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 37 4
1
<?php
2
namespace LE_ACME2\Utilities;
3
4
use LE_ACME2\Exception;
5
6
class ChallengeHTTP {
7
8
    /**
9
     * @throws Exception\HTTPAuthorizationInvalid
10
     */
11
    public static function fetch(string $domain, string $token) : string {
12
13
        $requestURL = 'http://' . $domain . '/.well-known/acme-challenge/' . $token;
14
        $handle = curl_init();
15
        curl_setopt($handle, CURLOPT_URL, $requestURL);
16
        curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
17
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
18
19
        $response = curl_exec($handle);
20
21
        if(curl_errno($handle)) {
22
23
            throw new Exception\HTTPAuthorizationInvalid(
24
                'Error while testing HTTP challenge for "' . $domain . '"": ' .
25
                $domain . '/.well-known/acme-challenge/' . $token . PHP_EOL .
26
                '- CURL error: ' . curl_error($handle)
27
            );
28
        }
29
30
        $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
31
        if(substr($statusCode, 0, 1) != 2) {
32
            throw new Exception\HTTPAuthorizationInvalid(
33
                'Error while testing HTTP challenge for "' . $domain . '"": ' .
34
                $domain . '/.well-known/acme-challenge/' . $token .
35
                ' - unexpected status code:  ' . $statusCode
36
            );
37
        }
38
39
        if($response === false) {
40
            throw new Exception\HTTPAuthorizationInvalid(
41
                'HTTP challenge for "' . $domain . '"": ' .
42
                $domain . '/.well-known/acme-challenge/' . $token .
43
                ' tested, found invalid. CURL response: ' . var_export($response, true)
44
            );
45
        }
46
47
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
}