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 ( 015291...4b7930 )
by François
04:19
created

CurlHttpClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 115
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A __destruct() 0 4 1
A setHttpsOnly() 0 4 1
A get() 0 9 1
A post() 0 10 1
C exec() 0 46 7
A curlReset() 0 21 3
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()
32
    {
33
        if (false === $this->curlChannel = curl_init()) {
34
            throw new RuntimeException('unable to create cURL channel');
35
        }
36
    }
37
38
    public function __destruct()
39
    {
40
        curl_close($this->curlChannel);
41
    }
42
43
    public function setHttpsOnly($httpsOnly)
44
    {
45
        $this->httpsOnly = (bool) $httpsOnly;
46
    }
47
48
    public function get($requestUri, array $requestHeaders = [])
49
    {
50
        return $this->exec(
51
            [
52
                CURLOPT_URL => $requestUri,
53
            ],
54
            $requestHeaders
55
        );
56
    }
57
58
    public function post($requestUri, array $postData = [], array $requestHeaders = [])
59
    {
60
        return $this->exec(
61
            [
62
                CURLOPT_URL => $requestUri,
63
                CURLOPT_POSTFIELDS => http_build_query($postData),
64
            ],
65
            $requestHeaders
66
        );
67
    }
68
69
    private function exec(array $curlOptions, array $requestHeaders)
70
    {
71
        // reset all cURL options
72
        $this->curlReset();
73
74
        $headerList = [];
75
76
        $defaultCurlOptions = [
77
            CURLOPT_HEADER => false,
78
            CURLOPT_RETURNTRANSFER => true,
79
            CURLOPT_FOLLOWLOCATION => false,
80
            CURLOPT_PROTOCOLS => $this->httpsOnly ? CURLPROTO_HTTPS : CURLPROTO_HTTPS | CURLPROTO_HTTP,
81
            CURLOPT_HEADERFUNCTION => function ($curlChannel, $headerData) use (&$headerList) {
82
                // XXX is this secure? mb_strlen?
83
                if (false !== strpos($headerData, ':')) {
84
                    list($key, $value) = explode(':', $headerData, 2);
85
                    $headerList[trim($key)] = trim($value);
86
                }
87
88
                return strlen($headerData);
89
            },
90
        ];
91
92
        if (0 !== count($requestHeaders)) {
93
            $curlRequestHeaders = [];
94
            foreach ($requestHeaders as $k => $v) {
95
                $curlRequestHeaders[] = sprintf('%s: %s', $k, $v);
96
            }
97
            $defaultCurlOptions[CURLOPT_HTTPHEADER] = $curlRequestHeaders;
98
        }
99
100
        if (false === curl_setopt_array($this->curlChannel, $curlOptions + $defaultCurlOptions)) {
101
            throw new RuntimeException('unable to set cURL options');
102
        }
103
104
        if (false === $responseData = curl_exec($this->curlChannel)) {
105
            $curlError = curl_error($this->curlChannel);
106
            throw new RuntimeException(sprintf('failure performing the HTTP request: "%s"', $curlError));
107
        }
108
109
        return new Response(
110
            curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE),
111
            $responseData,
112
            $headerList
113
        );
114
    }
115
116
    private function curlReset()
117
    {
118
        // requires PHP >= 5.5 for curl_reset
119
        if (function_exists('curl_reset')) {
120
            curl_reset($this->curlChannel);
121
122
            return;
123
        }
124
125
        // reset the request method to GET, that is enough to allow for
126
        // multiple requests using the same cURL channel
127
        if (false === curl_setopt_array(
128
            $this->curlChannel,
129
            [
130
                CURLOPT_HTTPGET => true,
131
                CURLOPT_HTTPHEADER => [],
132
            ]
133
        )) {
134
            throw new RuntimeException('unable to set cURL options');
135
        }
136
    }
137
}
138