Passed
Branch master (fc5382)
by Fabian
03:13
created

ChallengeHTTP::fetch()   A

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 24
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
     * @param string $domain
10
     * @param string $token
11
     * @return string
12
     * @throws Exception\HTTPAuthorizationInvalid
13
     */
14
    public static function fetch(string $domain, string $token) : string {
15
16
        $requestURL = 'http://' . $domain . '/.well-known/acme-challenge/' . $token;
17
        $handle = curl_init();
18
        curl_setopt($handle, CURLOPT_URL, $requestURL);
19
        curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
20
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
21
22
        $response = curl_exec($handle);
23
24
        if(curl_errno($handle)) {
25
26
            throw new Exception\HTTPAuthorizationInvalid(
27
                'Error while testing HTTP challenge for "' . $domain . '"": ' .
28
                $domain . '/.well-known/acme-challenge/' . $token . PHP_EOL .
29
                '- CURL error: ' . curl_error($handle)
30
            );
31
        }
32
33
        $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
34
        if(substr($statusCode, 0, 1) != 2) {
35
            throw new Exception\HTTPAuthorizationInvalid(
36
                'Error while testing HTTP challenge for "' . $domain . '"": ' .
37
                $domain . '/.well-known/acme-challenge/' . $token .
38
                ' - unexpected status code:  ' . $statusCode
39
            );
40
        }
41
42
        if($response === false) {
43
            throw new Exception\HTTPAuthorizationInvalid(
44
                'HTTP challenge for "' . $domain . '"": ' .
45
                $domain . '/.well-known/acme-challenge/' . $token .
46
                ' tested, found invalid. CURL response: ' . var_export($response, true)
47
            );
48
        }
49
50
        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...
51
    }
52
}