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 ( 487882...513f7c )
by François
13:06
created

CurlHttpClient::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 *  Copyright (C) 2017 François Kooman <[email protected]>.
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
19
namespace fkooman\OAuth\Client\Http;
20
21
use RuntimeException;
22
23
class CurlHttpClient implements HttpClientInterface
24
{
25
    /** @var resource */
26
    private $curlChannel;
27
28
    /** @var bool */
29
    private $httpsOnly = true;
30
31
    public function __construct(array $configData = [])
32
    {
33
        if (false === $this->curlChannel = curl_init()) {
34
            throw new RuntimeException('unable to create cURL channel');
35
        }
36
        if (array_key_exists('httpsOnly', $configData)) {
37
            $this->httpsOnly = (bool) $configData['httpsOnly'];
38
        }
39
    }
40
41
    public function __destruct()
42
    {
43
        curl_close($this->curlChannel);
44
    }
45
46
    public function get($requestUri, array $requestHeaders = [])
47
    {
48
        return $this->exec(
49
            [
50
                CURLOPT_URL => $requestUri,
51
            ],
52
            $requestHeaders
53
        );
54
    }
55
56
    public function post($requestUri, array $postData = [], array $requestHeaders = [])
57
    {
58
        return $this->exec(
59
            [
60
                CURLOPT_URL => $requestUri,
61
                CURLOPT_POSTFIELDS => http_build_query($postData),
62
            ],
63
            $requestHeaders
64
        );
65
    }
66
67
    private function exec(array $curlOptions, array $requestHeaders)
68
    {
69
        // reset all cURL options
70
        $this->curlReset();
71
72
        $headerList = [];
73
74
        $defaultCurlOptions = [
75
            CURLOPT_HEADER => false,
76
            CURLOPT_RETURNTRANSFER => true,
77
            CURLOPT_FOLLOWLOCATION => false,
78
            CURLOPT_PROTOCOLS => $this->httpsOnly ? CURLPROTO_HTTPS : CURLPROTO_HTTPS | CURLPROTO_HTTP,
79
            CURLOPT_HEADERFUNCTION => function ($curlChannel, $headerData) use (&$headerList) {
80
                // XXX is this secure? mb_strlen?
81
                if (false !== strpos($headerData, ':')) {
82
                    list($key, $value) = explode(':', $headerData, 2);
83
                    $headerList[trim($key)] = trim($value);
84
                }
85
86
                return strlen($headerData);
87
            },
88
        ];
89
90
        if (0 !== count($requestHeaders)) {
91
            $curlRequestHeaders = [];
92
            foreach ($requestHeaders as $k => $v) {
93
                $curlRequestHeaders[] = sprintf('%s: %s', $k, $v);
94
            }
95
            $defaultCurlOptions[CURLOPT_HTTPHEADER] = $curlRequestHeaders;
96
        }
97
98
        if (false === curl_setopt_array($this->curlChannel, $curlOptions + $defaultCurlOptions)) {
99
            throw new RuntimeException('unable to set cURL options');
100
        }
101
102
        if (false === $responseData = curl_exec($this->curlChannel)) {
103
            $curlError = curl_error($this->curlChannel);
104
            throw new RuntimeException(sprintf('failure performing the HTTP request: "%s"', $curlError));
105
        }
106
107
        return new Response(
108
            curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE),
109
            $responseData,
110
            $headerList
111
        );
112
    }
113
114
    private function curlReset()
115
    {
116
        // requires PHP >= 5.5 for curl_reset
117
        if (function_exists('curl_reset')) {
118
            curl_reset($this->curlChannel);
119
120
            return;
121
        }
122
123
        // reset the request method to GET, that is enough to allow for
124
        // multiple requests using the same cURL channel
125
        if (false === curl_setopt_array(
126
            $this->curlChannel,
127
            [
128
                CURLOPT_HTTPGET => true,
129
                CURLOPT_HTTPHEADER => [],
130
            ]
131
        )) {
132
            throw new RuntimeException('unable to set cURL options');
133
        }
134
    }
135
}
136