Passed
Push — master ( ee86b6...621ce0 )
by frey
01:01
created

Http::send()   F

Complexity

Conditions 15
Paths 2592

Size

Total Lines 52
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 22.8024

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 15
eloc 43
c 1
b 1
f 0
nc 2592
nop 1
dl 0
loc 52
ccs 31
cts 46
cp 0.6739
crap 22.8024
rs 3.024

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
3
namespace Freyo\Flysystem\QcloudCOSv3\Client;
4
5
function my_curl_reset($handler)
6
{
7
    curl_setopt($handler, CURLOPT_URL, '');
8
    curl_setopt($handler, CURLOPT_HTTPHEADER, []);
9
    curl_setopt($handler, CURLOPT_POSTFIELDS, []);
10
    curl_setopt($handler, CURLOPT_TIMEOUT, 0);
11
    curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);
12
    curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
13
}
14
class Http
15
{
16
    public static $_httpInfo = '';
17
    public static $_curlHandler;
18
19
    /**
20
     * send http request.
21
     *
22
     * @param array $rq http请求信息
23
     *                  url        : 请求的url地址
24
     *                  method     : 请求方法,'get', 'post', 'put', 'delete', 'head'
25
     *                  data       : 请求数据,如有设置,则method为post
26
     *                  header     : 需要设置的http头部
27
     *                  host       : 请求头部host
28
     *                  timeout    : 请求超时时间
29
     *                  cert       : ca文件路径
30
     *                  ssl_version: SSL版本号
31
     *
32
     * @return string http请求响应
33
     */
34 14
    public static function send($rq)
35
    {
36 14
        if (self::$_curlHandler) {
37 13
            if (function_exists('curl_reset')) {
38 13
                curl_reset(self::$_curlHandler);
39 13
            } else {
40
                my_curl_reset(self::$_curlHandler);
41
            }
42 13
        } else {
43 1
            self::$_curlHandler = curl_init();
44
        }
45 14
        curl_setopt(self::$_curlHandler, CURLOPT_URL, $rq['url']);
46 14
        switch (true) {
47 14
            case isset($rq['method']) && in_array(strtolower($rq['method']), ['get', 'post', 'put', 'delete', 'head']):
48 14
                $method = strtoupper($rq['method']);
49 14
                break;
50
            case isset($rq['data']):
51
                $method = 'POST';
52
                break;
53
            default:
54
                $method = 'GET';
55
        }
56 14
        $header = isset($rq['header']) ? $rq['header'] : [];
57 14
        $header[] = 'Method:'.$method;
58 14
        $header[] = 'User-Agent:'.Conf::getUA();
59 14
        $header[] = 'Connection: keep-alive';
60 14
        if ('POST' == $method) {
61 7
            $header[] = 'Expect: ';
62 7
        }
63
64 14
        isset($rq['host']) && $header[] = 'Host:'.$rq['host'];
65 14
        curl_setopt(self::$_curlHandler, CURLOPT_HTTPHEADER, $header);
66 14
        curl_setopt(self::$_curlHandler, CURLOPT_RETURNTRANSFER, 1);
67 14
        curl_setopt(self::$_curlHandler, CURLOPT_CUSTOMREQUEST, $method);
68 14
        isset($rq['timeout']) && curl_setopt(self::$_curlHandler, CURLOPT_TIMEOUT, $rq['timeout']);
69 14
        isset($rq['data']) && in_array($method, ['POST', 'PUT']) && curl_setopt(self::$_curlHandler, CURLOPT_POSTFIELDS, $rq['data']);
70 14
        $ssl = substr($rq['url'], 0, 8) == 'https://' ? true : false;
71 14
        if (isset($rq['cert'])) {
72
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYPEER, true);
73
            curl_setopt(self::$_curlHandler, CURLOPT_CAINFO, $rq['cert']);
74
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYHOST, 2);
75
            self::setCurlSSLVersion($rq);
76 14
        } elseif ($ssl) {
77
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYPEER, false); //true any ca
78
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYHOST, 1); //check only host
79
            self::setCurlSSLVersion($rq);
80
        }
81 14
        $ret = curl_exec(self::$_curlHandler);
82 14
        self::$_httpInfo = curl_getinfo(self::$_curlHandler);
83
84 14
        return $ret;
85
    }
86
87
    public static function info()
88
    {
89
        return self::$_httpInfo;
90
    }
91
92
    private static function setCurlSSLVersion($rq)
93
    {
94
        if (isset($rq['ssl_version'])) {
95
            curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, $rq['ssl_version']);
96
        } else {
97
            curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, 4);
98
        }
99
    }
100
}
101