GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CurlHttpClient   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __destruct() 0 4 1
A get() 0 8 1
A post() 0 9 1
B exec() 0 27 3
A curlReset() 0 15 3
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Common\HttpClient;
11
12
use RuntimeException;
13
14
class CurlHttpClient implements HttpClientInterface
15
{
16
    /** @var resource */
17
    private $curlChannel;
18
19
    /** @var array */
20
    private $authInfo;
21
22
    public function __construct(array $authInfo)
23
    {
24
        if (false === $this->curlChannel = curl_init()) {
25
            throw new RuntimeException('unable to create cURL channel');
26
        }
27
        $this->authInfo = $authInfo;
28
    }
29
30
    public function __destruct()
31
    {
32
        curl_close($this->curlChannel);
33
    }
34
35
    public function get($requestUri)
36
    {
37
        return $this->exec(
38
            [
39
                CURLOPT_URL => $requestUri,
40
            ]
41
        );
42
    }
43
44
    public function post($requestUri, array $postData = [])
45
    {
46
        return $this->exec(
47
            [
48
                CURLOPT_URL => $requestUri,
49
                CURLOPT_POSTFIELDS => http_build_query($postData),
50
            ]
51
        );
52
    }
53
54
    private function exec(array $curlOptions)
55
    {
56
        // reset all cURL options
57
        $this->curlReset();
58
59
        $defaultCurlOptions = [
60
            CURLOPT_USERPWD => sprintf('%s:%s', $this->authInfo[0], $this->authInfo[1]),
61
            CURLOPT_HEADER => false,
62
            CURLOPT_RETURNTRANSFER => true,
63
            CURLOPT_FOLLOWLOCATION => false,
64
            CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
65
        ];
66
67
        if (false === curl_setopt_array($this->curlChannel, $curlOptions + $defaultCurlOptions)) {
68
            throw new RuntimeException('unable to set cURL options');
69
        }
70
71
        if (false === $responseData = curl_exec($this->curlChannel)) {
72
            $curlError = curl_error($this->curlChannel);
73
            throw new RuntimeException(sprintf('failure performing the HTTP request: "%s"', $curlError));
74
        }
75
76
        return [
77
            curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE),
78
            json_decode($responseData, true),
79
        ];
80
    }
81
82
    private function curlReset()
83
    {
84
        // requires PHP >= 5.5 for curl_reset
85
        if (function_exists('curl_reset')) {
86
            curl_reset($this->curlChannel);
87
88
            return;
89
        }
90
91
        // reset the request method to GET, that is enough to allow for
92
        // multiple requests using the same cURL channel
93
        if (false === curl_setopt($this->curlChannel, CURLOPT_HTTPGET, true)) {
94
            throw new RuntimeException('unable to set cURL options');
95
        }
96
    }
97
}
98