Passed
Pull Request — master (#372)
by
unknown
21:21
created

Client::sendRequest()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 45
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 7

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 7
eloc 36
nc 16
nop 1
dl 0
loc 45
ccs 41
cts 41
cp 1
crap 7
rs 8.4106
c 5
b 1
f 0
1
<?php
2
namespace Qiniu\Http;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Request;
6
use Qiniu\Http\Response;
7
8
final class Client
9
{
10 69
    public static function get($url, array $headers = array())
11
    {
12 69
        $request = new Request('GET', $url, $headers);
13 69
        return self::sendRequest($request);
14
    }
15
16
    public static function delete($url, array $headers = array())
17
    {
18
        $request = new Request('DELETE', $url, $headers);
19
        return self::sendRequest($request);
20
    }
21
22 57
    public static function post($url, $body, array $headers = array())
23
    {
24 57
        $request = new Request('POST', $url, $headers, $body);
25 57
        return self::sendRequest($request);
26
    }
27
28
    public static function PUT($url, $body, array $headers = array())
29
    {
30
        $request = new Request('PUT', $url, $headers, $body);
31
        return self::sendRequest($request);
32
    }
33
34 9
    public static function multipartPost(
35
        $url,
36
        $fields,
37
        $name,
38
        $fileName,
39
        $fileBody,
40
        $mimeType = null,
41
        array $headers = array()
42
    ) {
43 9
        $data = array();
44 9
        $mimeBoundary = md5(microtime());
45
46 9
        foreach ($fields as $key => $val) {
47 9
            array_push($data, '--' . $mimeBoundary);
48 9
            array_push($data, "Content-Disposition: form-data; name=\"$key\"");
49 9
            array_push($data, '');
50 9
            array_push($data, $val);
51 9
        }
52
53 9
        array_push($data, '--' . $mimeBoundary);
54 9
        $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
55 9
        $finalFileName = self::escapeQuotes($fileName);
56 9
        array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
57 9
        array_push($data, "Content-Type: $finalMimeType");
58 9
        array_push($data, '');
59 9
        array_push($data, $fileBody);
60
61 9
        array_push($data, '--' . $mimeBoundary . '--');
62 9
        array_push($data, '');
63
64 9
        $body = implode("\r\n", $data);
65
        // var_dump($data);exit;
66 9
        $contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
67 9
        $headers['Content-Type'] = $contentType;
68 9
        $request = new Request('POST', $url, $headers, $body);
69 9
        return self::sendRequest($request);
70
    }
71
72 102
    private static function userAgent()
73
    {
74 102
        $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
75
76 102
        $systemInfo = php_uname("s");
77 102
        $machineInfo = php_uname("m");
78
79 102
        $envInfo = "($systemInfo/$machineInfo)";
80
81 102
        $phpVer = phpversion();
82
83 102
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
84 102
        return $ua;
85
    }
86
87 102
    public static function sendRequest($request)
88
    {
89 102
        $t1 = microtime(true);
90 102
        $ch = curl_init();
91
        $options = array(
92 102
            CURLOPT_USERAGENT => self::userAgent(),
93 102
            CURLOPT_RETURNTRANSFER => true,
94 102
            CURLOPT_SSL_VERIFYPEER => false,
95 102
            CURLOPT_SSL_VERIFYHOST => false,
96 102
            CURLOPT_HEADER => true,
97 102
            CURLOPT_NOBODY => false,
98 102
            CURLOPT_CUSTOMREQUEST => $request->method,
99 102
            CURLOPT_URL => $request->url,
100 102
        );
101
        // Handle open_basedir & safe mode
102 102
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
103 102
            $options[CURLOPT_FOLLOWLOCATION] = true;
104 102
        }
105 102
        if (!empty($request->headers)) {
106 69
            $headers = array();
107 69
            foreach ($request->headers as $key => $val) {
108 69
                array_push($headers, "$key: $val");
109 69
            }
110 69
            $options[CURLOPT_HTTPHEADER] = $headers;
111 69
        }
112 102
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
113 102
        if (!empty($request->body)) {
114 39
            $options[CURLOPT_POSTFIELDS] = $request->body;
115 39
        }
116 102
        curl_setopt_array($ch, $options);
117 102
        $result = curl_exec($ch);
118 102
        $t2 = microtime(true);
119 102
        $duration = round($t2 - $t1, 3);
120 102
        $ret = curl_errno($ch);
121 102
        if ($ret !== 0) {
122 3
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
123 3
            curl_close($ch);
124 3
            return $r;
125
        }
126 102
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
127 102
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
128 102
        $headers = Header::parseRawText(substr($result, 0, $header_size));
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        $headers = Header::parseRawText(substr(/** @scrutinizer ignore-type */ $result, 0, $header_size));
Loading history...
129 102
        $body = substr($result, $header_size);
130 102
        curl_close($ch);
131 102
        return new Response($code, $duration, $headers, $body, null);
132
    }
133
134 102
    private static function escapeQuotes($str)
135
    {
136 102
        $find = array("\\", "\"");
137 102
        $replace = array("\\\\", "\\\"");
138 102
        return str_replace($find, $replace, $str);
139 102
    }
140 102
141 102
    private static function ucwordsHyphen($str)
0 ignored issues
show
Unused Code introduced by
The method ucwordsHyphen() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
142 102
    {
143 102
        return ucwords(strtolower($str), '-');
144 102
    }
145
}
146