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

ChallengeHTTP   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
dl 0
loc 45
ccs 0
cts 24
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
     * @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
}