Completed
Push — 23 ( 2cbb94...e876bd )
by Harald
11:26 queued 05:21
created

Http::_doUrlRequest()   F

Complexity

Conditions 14
Paths 960

Size

Total Lines 70
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 960
nop 3
dl 0
loc 70
rs 2.8571
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Braintree;
3
4
/**
5
 * Braintree HTTP Client
6
 * processes Http requests using curl
7
 *
8
 * @copyright  2015 Braintree, a division of PayPal, Inc.
9
 */
10
class Http
11
{
12
    protected $_config;
13
    private $_useClientCredentials = false;
14
15
    public function __construct($config)
16
    {
17
        $this->_config = $config;
18
    }
19
20 View Code Duplication
    public function delete($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $response = $this->_doRequest('DELETE', $path);
23
        if($response['status'] === 200) {
24
            return true;
25
        } else {
26
            Util::throwStatusCodeException($response['status']);
27
        }
28
    }
29
30 View Code Duplication
    public function get($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $response = $this->_doRequest('GET', $path);
33
        if ($response['status'] === 200) {
34
            return Xml::buildArrayFromXml($response['body']);
35
        } else {
36
            Util::throwStatusCodeException($response['status']);
37
        }
38
    }
39
40 View Code Duplication
    public function post($path, $params = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $response = $this->_doRequest('POST', $path, $this->_buildXml($params));
43
        $responseCode = $response['status'];
44
        if($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
45
            return Xml::buildArrayFromXml($response['body']);
46
        } else {
47
            Util::throwStatusCodeException($responseCode);
48
        }
49
    }
50
51 View Code Duplication
    public function put($path, $params = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $response = $this->_doRequest('PUT', $path, $this->_buildXml($params));
54
        $responseCode = $response['status'];
55
        if($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
56
            return Xml::buildArrayFromXml($response['body']);
57
        } else {
58
            Util::throwStatusCodeException($responseCode);
59
        }
60
    }
61
62
    private function _buildXml($params)
63
    {
64
        return empty($params) ? null : Xml::buildXmlFromArray($params);
65
    }
66
67
    private function _getHeaders()
68
    {
69
        return [
70
            'Accept: application/xml',
71
            'Content-Type: application/xml',
72
        ];
73
    }
74
75
    private function _getAuthorization()
76
    {
77
        if ($this->_useClientCredentials) {
78
            return [
79
                'user' => $this->_config->getClientId(),
80
                'password' => $this->_config->getClientSecret(),
81
            ];
82
        } else if ($this->_config->isAccessToken()) {
83
            return [
84
                'token' => $this->_config->getAccessToken(),
85
            ];
86
        } else {
87
            return [
88
                'user' => $this->_config->getPublicKey(),
89
                'password' => $this->_config->getPrivateKey(),
90
            ];
91
        }
92
    }
93
94
    public function useClientCredentials()
95
    {
96
        $this->_useClientCredentials = true;
97
    }
98
99
    private function _doRequest($httpVerb, $path, $requestBody = null)
100
    {
101
        return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . $path, $requestBody);
102
    }
103
104
    public function _doUrlRequest($httpVerb, $url, $requestBody = null)
105
    {
106
        $curl = curl_init();
107
        curl_setopt($curl, CURLOPT_TIMEOUT, $this->_config->timeout());
108
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpVerb);
109
        curl_setopt($curl, CURLOPT_URL, $url);
110
111
        if ($this->_config->acceptGzipEncoding()) {
112
            curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
113
        }
114
        if ($this->_config->sslVersion()) {
115
            curl_setopt($curl, CURLOPT_SSLVERSION, $this->_config->sslVersion());
116
        }
117
118
        $headers = $this->_getHeaders($curl);
0 ignored issues
show
Unused Code introduced by
The call to Http::_getHeaders() has too many arguments starting with $curl.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
        $headers[] = 'User-Agent: Braintree PHP Library ' . Version::get();
120
        $headers[] = 'X-ApiVersion: ' . Configuration::API_VERSION;
121
122
        $authorization = $this->_getAuthorization();
123
        if (isset($authorization['user'])) {
124
            curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
125
            curl_setopt($curl, CURLOPT_USERPWD, $authorization['user'] . ':' . $authorization['password']);
126
        } else if (isset($authorization['token'])) {
127
            $headers[] = 'Authorization: Bearer ' . $authorization['token'];
128
        }
129
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
130
131
        // curl_setopt($curl, CURLOPT_VERBOSE, true);
132
        if ($this->_config->sslOn()) {
133
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
134
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
135
            curl_setopt($curl, CURLOPT_CAINFO, $this->getCaFile());
136
        }
137
138
        if(!empty($requestBody)) {
139
            curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
140
        }
141
142
        if($this->_config->isUsingProxy()) {
143
            $proxyHost = $this->_config->getProxyHost();
144
            $proxyPort = $this->_config->getProxyPort();
145
            $proxyType = $this->_config->getProxyType();
146
            $proxyUser = $this->_config->getProxyUser();
147
            $proxyPwd= $this->_config->getProxyPassword();
148
            curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
149
            if(!empty($proxyType)) {
150
                curl_setopt($curl, CURLOPT_PROXYTYPE, $proxyType);
151
            }
152
            if($this->_config->isAuthenticatedProxy()) {
153
                curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPwd);
154
            }
155
        }
156
157
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
158
        $response = curl_exec($curl);
159
        $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
160
        $error_code = curl_errno($curl);
161
162
        if ($error_code == 28 && $httpStatus == 0) {
163
            throw new Exception\Timeout();
164
        }
165
166
        curl_close($curl);
167
        if ($this->_config->sslOn()) {
168
            if ($httpStatus == 0) {
169
                throw new Exception\SSLCertificate();
170
            }
171
        }
172
        return ['status' => $httpStatus, 'body' => $response];
173
    }
174
175
    private function getCaFile()
176
    {
177
        static $memo;
178
179
        if ($memo === null) {
180
            $caFile = $this->_config->caFile();
181
182
            if (substr($caFile, 0, 7) !== 'phar://') {
183
                return $caFile;
184
            }
185
186
            $extractedCaFile = sys_get_temp_dir() . '/api_braintreegateway_com.ca.crt';
187
188
            if (!file_exists($extractedCaFile) || sha1_file($extractedCaFile) != sha1_file($caFile)) {
189
                if (!copy($caFile, $extractedCaFile)) {
190
                    throw new Exception\SSLCaFileNotFound();
191
                }
192
            }
193
            $memo = $extractedCaFile;
194
        }
195
196
        return $memo;
197
    }
198
}
199
class_alias('Braintree\Http', 'Braintree_Http');
200