Completed
Pull Request — master (#293)
by
unknown
21:47 queued 19:59
created

FormUploader::put()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11.2481

Importance

Changes 0
Metric Value
cc 6
nc 12
nop 8
dl 0
loc 39
ccs 9
cts 19
cp 0.4737
crap 11.2481
rs 8.6737
c 0
b 0
f 0

How to fix   Many Parameters   

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
namespace Qiniu\Storage;
3
4
use Qiniu\Http\Client;
5
use Qiniu\Http\Error;
6
7
final class FormUploader
8
{
9
10
    /**
11
     * 上传二进制流到七牛, 内部使用
12
     *
13
     * @param $upToken    上传凭证
14
     * @param $key        上传文件名
15
     * @param $data       上传二进制流
16
     * @param $config     上传配置
17
     * @param $params     自定义变量,规格参考
18
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
19
     * @param $mime       上传数据的mimeType
20
     *
21
     * @return array    包含已上传文件的信息,类似:
22
     *                                              [
23
     *                                                  "hash" => "<Hash string>",
24
     *                                                  "key" => "<Key string>"
25
     *                                              ]
26
     */
27 6
    public static function put(
28
        $upToken,
29
        $key,
30
        $data,
31
        $config,
32
        $params,
33
        $mime,
34
        $fname,
35
        $checkCrc
0 ignored issues
show
Unused Code introduced by
The parameter $checkCrc is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    ) {
37 6
        $fields = array('token' => $upToken);
38 6
        if ($key === null) {
39
            
40
        } else {
41 6
            $fields['key'] = $key;
42
        }
43
44
        //enable crc32 check by default
45 6
        $fields['crc32'] = \Qiniu\crc32_data($data);
46
47 6
        if ($params) {
48
            foreach ($params as $k => $v) {
49
                $fields[$k] = $v;
50
            }
51
        }
52
53 6
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
54 6
        if ($err != null) {
55
            return array(null, $err);
56
        }
57
58 6
        $upHost = $config->getUpHost($accessKey, $bucket);
59
60
        $response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
61
        if (!$response->ok()) {
62
            return array(null, new Error($upHost, $response));
63
        }
64
        return array($response->json(), null);
65
    }
66
67
    /**
68
     * 上传文件到七牛,内部使用
69
     *
70
     * @param $upToken    上传凭证
71
     * @param $key        上传文件名
72
     * @param $filePath   上传文件的路径
73
     * @param $config     上传配置
74
     * @param $params     自定义变量,规格参考
75
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
76
     * @param $mime       上传数据的mimeType
77
     *
78
     * @return array    包含已上传文件的信息,类似:
79
     *                                              [
80
     *                                                  "hash" => "<Hash string>",
81
     *                                                  "key" => "<Key string>"
82
     *                                              ]
83
     */
84 3
    public static function putFile(
85
        $upToken,
86
        $key,
87
        $filePath,
88
        $config,
89
        $params,
90
        $mime
91
    ) {
92
93
94 3
        $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime));
95 3
        if ($key !== null) {
96 3
            $fields['key'] = $key;
97 2
        }
98
99 3
        $fields['crc32'] = \Qiniu\crc32_file($filePath);
100
101 3
        if ($params) {
102
            foreach ($params as $k => $v) {
103
                $fields[$k] = $v;
104
            }
105
        }
106 3
        $fields['key'] = $key;
107 3
        $headers = array('Content-Type' => 'multipart/form-data');
108
109 3
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
110 3
        if ($err != null) {
111
            return array(null, $err);
112
        }
113
114 3
        $upHost = $config->getUpHost($accessKey, $bucket);
115
116
        $response = Client::post($upHost, $fields, $headers);
117
        if (!$response->ok()) {
118
            return array(null, new Error($upHost, $response));
119
        }
120
        return array($response->json(), null);
121
    }
122
123 3
    private static function createFile($filename, $mime)
124
    {
125
        // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
126
        // See: https://wiki.php.net/rfc/curl-file-upload
127 3
        if (function_exists('curl_file_create')) {
128 3
            return curl_file_create($filename, $mime);
129
        }
130
131
        // Use the old style if using an older version of PHP
132
        $value = "@{$filename}";
133
        if (!empty($mime)) {
134
            $value .= ';type=' . $mime;
135
        }
136
137
        return $value;
138
    }
139
}
140