ChallengeHTTP::fetch()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 37
ccs 0
cts 19
cp 0
rs 9.552
c 1
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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
}