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 16

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 119
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 send() 0 13 2
A curlInit() 0 6 2
A curlReset() 0 9 2
B exec() 0 52 7
1
<?php
2
3
/**
4
 * Copyright (c) 2016, 2017 François Kooman <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace fkooman\OAuth\Client\Http;
26
27
use fkooman\OAuth\Client\Http\Exception\CurlException;
28
29
class CurlHttpClient implements HttpClientInterface
30
{
31
    /** @var resource */
32
    private $curlChannel;
33
34
    /** @var bool */
35
    private $allowHttp = false;
36
37
    /**
38
     * @param array $configData
39
     */
40
    public function __construct(array $configData = [])
41
    {
42
        if (array_key_exists('allowHttp', $configData)) {
43
            $this->allowHttp = (bool) $configData['allowHttp'];
44
        }
45
        $this->curlInit();
46
    }
47
48
    public function __destruct()
49
    {
50
        curl_close($this->curlChannel);
51
    }
52
53
    /**
54
     * @param Request $request
55
     *
56
     * @return Response
57
     */
58
    public function send(Request $request)
59
    {
60
        $curlOptions = [
61
            CURLOPT_CUSTOMREQUEST => $request->getMethod(),
62
            CURLOPT_URL => $request->getUri(),
63
        ];
64
65
        if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) {
66
            $curlOptions[CURLOPT_POSTFIELDS] = $request->getBody();
67
        }
68
69
        return $this->exec($curlOptions, $request->getHeaders());
70
    }
71
72
    private function curlInit()
73
    {
74
        if (false === $this->curlChannel = curl_init()) {
75
            throw new CurlException('unable to create cURL channel');
76
        }
77
    }
78
79
    private function curlReset()
80
    {
81
        if (function_exists('curl_reset')) {
82
            curl_reset($this->curlChannel);
83
        } else {
84
            curl_close($this->curlChannel);
85
            $this->curlInit();
86
        }
87
    }
88
89
    /**
90
     * @param array $curlOptions
91
     * @param array $requestHeaders
92
     *
93
     * @return Response
94
     */
95
    private function exec(array $curlOptions, array $requestHeaders)
96
    {
97
        $headerList = [];
98
99
        $this->curlReset();
100
101
        $defaultCurlOptions = [
102
            CURLOPT_HEADER => false,
103
            CURLOPT_CONNECTTIMEOUT => 5,
104
            CURLOPT_TIMEOUT => 10,
105
            CURLOPT_RETURNTRANSFER => true,
106
            CURLOPT_HTTPHEADER => [],
107
            CURLOPT_FOLLOWLOCATION => false,
108
            CURLOPT_PROTOCOLS => $this->allowHttp ? CURLPROTO_HTTPS | CURLPROTO_HTTP : CURLPROTO_HTTPS,
109
            CURLOPT_HEADERFUNCTION => function ($curlChannel, $headerData) use (&$headerList) {
110
                if (false !== strpos($headerData, ':')) {
111
                    list($key, $value) = explode(':', $headerData, 2);
112
                    $headerList[trim($key)] = trim($value);
113
                }
114
115
                return strlen($headerData);
116
            },
117
        ];
118
119
        if (0 !== count($requestHeaders)) {
120
            $curlRequestHeaders = [];
121
            foreach ($requestHeaders as $k => $v) {
122
                $curlRequestHeaders[] = sprintf('%s: %s', $k, $v);
123
            }
124
            $defaultCurlOptions[CURLOPT_HTTPHEADER] = $curlRequestHeaders;
125
        }
126
127
        if (false === curl_setopt_array($this->curlChannel, $curlOptions + $defaultCurlOptions)) {
128
            throw new CurlException('unable to set cURL options');
129
        }
130
131
        if (false === $responseData = curl_exec($this->curlChannel)) {
132
            throw new CurlException(
133
                sprintf(
134
                    '[%d] %s',
135
                    curl_errno($this->curlChannel),
136
                    curl_error($this->curlChannel)
137
                )
138
            );
139
        }
140
141
        return new Response(
142
            curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE),
143
            $responseData,
144
            $headerList
145
        );
146
    }
147
}
148