FormUploader::put()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.1999

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 28
c 1
b 0
f 0
nc 32
nop 8
dl 0
loc 53
ccs 12
cts 19
cp 0.6316
crap 11.1999
rs 8.4444

How to fix   Long Method    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Qiniu\Storage;
4
5
use Qiniu\Config;
6
use Qiniu\Http\Error;
7
use Qiniu\Http\Client;
8
use Qiniu\Http\RequestOptions;
9
10
final class FormUploader
11
{
12
13
    /**
14
     * 上传二进制流到七牛, 内部使用
15
     *
16
     * @param string $upToken 上传凭证
17
     * @param string $key 上传文件名
18
     * @param string $data 上传二进制流
19
     * @param Config $config 上传配置
20
     * @param string $params 自定义变量,规格参考
21
     *                    {@link https://developer.qiniu.com/kodo/manual/1235/vars#xvar}
22
     * @param string $mime 上传数据的mimeType
23
     * @param string $fname
24
     * @param RequestOptions $reqOpt
25
     *
26
     * @return array    包含已上传文件的信息,类似:
27 9
     *                                              [
28
     *                                                  "hash" => "<Hash string>",
29
     *                                                  "key" => "<Key string>"
30
     *                                              ]
31
     */
32
    public static function put(
33
        $upToken,
34
        $key,
35
        $data,
36 9
        $config,
37 9
        $params,
38
        $mime,
39 9
        $fname,
40
        $reqOpt = null
41
    ) {
42
        if ($reqOpt == null) {
43 9
            $reqOpt = new RequestOptions();
44
        }
45 9
        $fields = array('token' => $upToken);
46
        if ($key === null) {
0 ignored issues
show
introduced by
The condition $key === null is always false.
Loading history...
47
        } else {
48
            $fields['key'] = $key;
49
        }
50
51 9
        //enable crc32 check by default
52 9
        $fields['crc32'] = \Qiniu\crc32_data($data);
53
54
        if ($params) {
55
            foreach ($params as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $params of type string is not traversable.
Loading history...
56 9
                $fields[$k] = $v;
57
            }
58 9
        }
59 9
60
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
61
        if ($err != null) {
62 9
            return array(null, $err);
63
        }
64
65
        list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket, $reqOpt);
66
        if ($err != null) {
67
            return array(null, $err);
68
        }
69
70
71
        $response = Client::multipartPost(
72
            $upHost,
73
            $fields,
74
            'file',
75
            $fname,
76
            $data,
77
            $mime,
78
            array(),
79
            $reqOpt
80
        );
81
        if (!$response->ok()) {
82 3
            return array(null, new Error($upHost, $response));
83
        }
84
        return array($response->json(), null);
85
    }
86
87
    /**
88
     * 上传文件到七牛,内部使用
89
     *
90
     * @param string $upToken 上传凭证
91
     * @param string $key 上传文件名
92 3
     * @param string $filePath 上传文件的路径
93 3
     * @param Config $config 上传配置
94 3
     * @param string $params 自定义变量,规格参考
95 3
     *                    https://developer.qiniu.com/kodo/manual/1235/vars#xvar
96
     * @param string $mime 上传数据的mimeType
97 3
     *
98
     * @return array    包含已上传文件的信息,类似:
99 3
     *                                              [
100
     *                                                  "hash" => "<Hash string>",
101
     *                                                  "key" => "<Key string>"
102
     *                                              ]
103
     */
104 3
    public static function putFile(
105 3
        $upToken,
106
        $key,
107 3
        $filePath,
108 3
        $config,
109
        $params,
110
        $mime,
111
        $reqOpt = null
112 3
    ) {
113
        if ($reqOpt == null) {
114 3
            $reqOpt = new RequestOptions();
115 3
        }
116
117
        $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime));
118 3
        if ($key !== null) {
0 ignored issues
show
introduced by
The condition $key !== null is always true.
Loading history...
119
            $fields['key'] = $key;
120
        }
121 3
122
        $fields['crc32'] = \Qiniu\crc32_file($filePath);
123
124
        if ($params) {
125 3
            foreach ($params as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $params of type string is not traversable.
Loading history...
126 2
                $fields[$k] = $v;
127
            }
128
        }
129
        $fields['key'] = $key;
130 1
        $headers = array('Content-Type' => 'multipart/form-data');
131 1
132 1
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
133 1
        if ($err != null) {
134
            return array(null, $err);
135 1
        }
136
137
        list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket, $reqOpt);
138
        if ($err != null) {
139
            return array(null, $err);
140
        }
141
142
        $response = Client::post($upHost, $fields, $headers, $reqOpt);
143
        if (!$response->ok()) {
144
            return array(null, new Error($upHost, $response));
145
        }
146
        return array($response->json(), null);
147
    }
148
149
    private static function createFile($filename, $mime)
150
    {
151
        // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
152
        // See: https://wiki.php.net/rfc/curl-file-upload
153
        if (function_exists('curl_file_create')) {
154
            return curl_file_create($filename, $mime);
155
        }
156
157
        // Use the old style if using an older version of PHP
158
        $value = "@{$filename}";
159
        if (!empty($mime)) {
160
            $value .= ';type=' . $mime;
161
        }
162
163
        return $value;
164
    }
165
}
166