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.
Completed
Push — master ( 38a77e...303f81 )
by François
02:44
created

CurlHttpClient   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 122
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setOptions() 0 23 2
A __destruct() 0 4 1
A get() 0 10 1
A post() 0 11 1
B handleRequest() 0 32 6
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace SURFnet\VPN\Common\HttpClient;
19
20
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;
21
use RuntimeException;
22
23
class CurlHttpClient implements HttpClientInterface
24
{
25
    /** @var resource */
26
    private $curlChannel;
27
28
    /** @var string */
29
    private $authUser;
30
31
    /** @var string */
32
    private $authPass;
33
34
    public function __construct($authUser, $authPass)
35
    {
36
        if (false === $this->curlChannel = curl_init()) {
37
            throw new RuntimeException('unable to initialize a cURL session');
38
        }
39
        $this->authUser = $authUser;
40
        $this->authPass = $authPass;
41
    }
42
43
    private function setOptions(array $additionalOptions)
44
    {
45
        curl_reset($this->curlChannel);
46
        $curlOptions = array_merge(
47
            [
48
                CURLOPT_FOLLOWLOCATION => false,
49
                CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,
50
                CURLOPT_SSL_VERIFYPEER => true,
51
                CURLOPT_SSL_VERIFYHOST => 2,
52
                CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
53
                CURLOPT_USERPWD => sprintf('%s:%s', $this->authUser, $this->authPass),
54
                CURLOPT_RETURNTRANSFER => true,
55
                CURLOPT_HTTPHEADER, [
56
                    'Accept: application/json',
57
                ],
58
            ],
59
            $additionalOptions
60
        );
61
62
        if (false === curl_setopt_array($this->curlChannel, $curlOptions)) {
63
            throw new RuntimeException('unable to set cURL options');
64
        }
65
    }
66
67
    public function __destruct()
68
    {
69
        curl_close($this->curlChannel);
70
    }
71
72
    /**
73
     * Send a HTTP GET request.
74
     *
75
     * @param string $requestUri the request URI
76
     *
77
     * @throws HttpClientException if the response is a 4xx error with the
78
     *                             JSON "error" field of the response body as the exception message
79
     */
80
    public function get($requestUri)
81
    {
82
        $this->setOptions(
83
            [
84
                CURLOPT_URL => $requestUri,
85
            ]
86
        );
87
88
        return $this->handleRequest();
89
    }
90
91
    /**
92
     * Send a HTTP POST request.
93
     *
94
     * @param string $requestUri the request URI
95
     * @param array  $postData   the HTTP POST fields to send
96
     *
97
     * @throws HttpClientException if the response is a 4xx error with the
98
     *                             JSON "error" field of the response body as the exception message
99
     */
100
    public function post($requestUri, array $postData)
101
    {
102
        $this->setOptions(
103
            [
104
                CURLOPT_URL => $requestUri,
105
                CURLOPT_POSTFIELDS => http_build_query($postData),
106
            ]
107
        );
108
109
        return $this->handleRequest();
110
    }
111
112
    private function handleRequest()
113
    {
114
        $curlResponse = curl_exec($this->curlChannel);
115
116
        // error in the transfer?
117
        if (false === $curlResponse) {
118
            throw new RuntimeException(
119
                sprintf(
120
                    'cURL error: %s (%s)',
121
                    curl_error($this->curlChannel)
122
                )
123
            );
124
        }
125
126
        $decodedResponseData = json_decode($curlResponse, true);
127
128
        $curlResponseCode = curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE);
129
        // OK?
130
        if ($curlResponseCode >= 200 && $curlResponseCode < 300) {
131
            return $decodedResponseData;
132
        }
133
134
        if (is_array($decodedResponseData) && array_key_exists('error', $decodedResponseData)) {
135
            throw new HttpClientException(
136
                sprintf('[%d]: %s', $curlResponseCode, $decodedResponseData['error'])
137
            );
138
        }
139
140
        throw new HttpClientException(
141
                sprintf('[%d]: unexpected error', $curlResponseCode)
142
        );
143
    }
144
}
145