Passed
Push — master ( e7e3c4...d0e289 )
by Rogier
01:26
created

Client::makeCurlRequest()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 38
rs 8.8817
c 0
b 0
f 0
cc 6
nc 8
nop 4
1
<?php
2
3
namespace Rogierw\RwAcme\Http;
4
5
class Client
6
{
7
    /** @var string */
8
    private $baseUrl;
9
10
    /** @var int */
11
    private $timeout;
12
13
    public function __construct(string $baseUrl, int $timeout = 10)
14
    {
15
        $this->baseUrl = $baseUrl;
16
        $this->timeout = $timeout;
17
    }
18
19
    public function head(string $url)
20
    {
21
        return $this->makeCurlRequest('head', $url);
22
    }
23
24
    public function get(string $url, array $headers = [], array $arguments = [])
25
    {
26
        return $this->makeCurlRequest('get', $url, $headers, $arguments);
27
    }
28
29
    public function post(string $url, array $payload = [], array $headers = [])
30
    {
31
        $headers = array_merge(['Content-Type: application/jose+json'], $headers);
32
33
        return $this->makeCurlRequest('post', $url, $headers, $payload);
34
    }
35
36
    public function makeCurlRequest(string $httpVerb, string $fullUrl, array $headers = [], array $payload = []): Response
37
    {
38
        $headers = array_merge([
39
            'Content-Type: ' . ($httpVerb === 'post') ? 'application/jose+json' : 'application/json',
40
        ], $headers);
41
42
        $curlHandle = $this->getCurlHandle($fullUrl, $headers);
43
44
        switch ($httpVerb) {
45
            case 'head':
46
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'HEAD');
0 ignored issues
show
Bug introduced by
It seems like $curlHandle can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                curl_setopt(/** @scrutinizer ignore-type */ $curlHandle, CURLOPT_CUSTOMREQUEST, 'HEAD');
Loading history...
47
                curl_setopt($curlHandle, CURLOPT_NOBODY, true);
48
                break;
49
50
            case 'get':
51
                curl_setopt($curlHandle, CURLOPT_URL, $fullUrl . '?' . http_build_query($payload));
52
                break;
53
54
            case 'post':
55
                curl_setopt($curlHandle, CURLOPT_POST, true);
56
                $this->attachRequestPayload($curlHandle, $payload);
57
                break;
58
        }
59
60
        $rawResponse = curl_exec($curlHandle);
61
        $headerSize = curl_getinfo($curlHandle, CURLINFO_HEADER_SIZE);
62
        $headers = curl_getinfo($curlHandle);
63
        $error = curl_error($curlHandle);
64
65
        $rawHeaders = mb_substr($rawResponse, 0, $headerSize);
66
        $rawBody = mb_substr($rawResponse, $headerSize);
67
        $body = $rawBody;
68
69
        if ($headers['content_type'] === 'application/json') {
70
            $body = json_decode($rawBody, true);
71
        }
72
73
        return new Response($rawHeaders, $headers, $body, $error);
74
    }
75
76
    private function attachRequestPayload(&$curlHandle, array $data)
77
    {
78
        $encoded = json_encode($data);
79
80
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $encoded);
81
    }
82
83
    private function getCurlHandle(string $fullUrl, array $headers = [])
84
    {
85
        $curlHandle = curl_init();
86
87
        curl_setopt($curlHandle, CURLOPT_URL, $fullUrl);
0 ignored issues
show
Bug introduced by
It seems like $curlHandle can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        curl_setopt(/** @scrutinizer ignore-type */ $curlHandle, CURLOPT_URL, $fullUrl);
Loading history...
88
89
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([
90
            'Accept: application/json',
91
        ], $headers));
92
93
        curl_setopt($curlHandle, CURLOPT_USERAGENT, 'rogierw/rw-acme-client');
94
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
95
        curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeout);
96
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
97
        curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
98
        curl_setopt($curlHandle, CURLOPT_ENCODING, '');
99
        curl_setopt($curlHandle, CURLOPT_HEADER, true);
100
101
        return $curlHandle;
102
    }
103
}
104