Completed
Push — master ( 8c7ba7...1baeaf )
by
unknown
23:36 queued 22:16
created

UploadManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.61%

Importance

Changes 0
Metric Value
dl 0
loc 128
ccs 51
cts 61
cp 0.8361
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
B trimParams() 0 14 5
A __construct() 0 7 2
A put() 0 19 1
B putFile() 0 45 4
1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Config;
5
use Qiniu\Http\HttpClient;
6
use Qiniu\Storage\ResumeUploader;
7
use Qiniu\Storage\FormUploader;
8
9
/**
10
 * 主要涉及了资源上传接口的实现
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/
13
 */
14
final class UploadManager
15
{
16
    private $config;
17
18 12
    public function __construct(Config $config = null)
19
    {
20 12
        if ($config === null) {
21 9
            $config = new Config();
22 6
        }
23 12
        $this->config = $config;
24 12
    }
25
26
    /**
27
     * 上传二进制流到七牛
28
     *
29
     * @param $upToken    上传凭证
30
     * @param $key        上传文件名
31
     * @param $data       上传二进制流
32
     * @param $params     自定义变量,规格参考
33
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
34
     * @param $mime       上传数据的mimeType
35
     * @param $checkCrc   是否校验crc32
36
     *
37
     * @return array    包含已上传文件的信息,类似:
38
     *                                              [
39
     *                                                  "hash" => "<Hash string>",
40
     *                                                  "key" => "<Key string>"
41
     *                                              ]
42
     */
43 3
    public function put(
44
        $upToken,
45
        $key,
46
        $data,
47
        $params = null,
48
        $mime = 'application/octet-stream',
49
        $checkCrc = false
50
    ) {
51 3
        $params = self::trimParams($params);
52 3
        return FormUploader::put(
53 3
            $upToken,
54 3
            $key,
55 3
            $data,
56 3
            $this->config,
57 3
            $params,
58 3
            $mime,
59 1
            $checkCrc
0 ignored issues
show
Unused Code introduced by
The call to FormUploader::put() has too many arguments starting with $checkCrc.

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...
60 2
        );
61
    }
62
63
64
    /**
65
     * 上传文件到七牛
66
     *
67
     * @param $upToken    上传凭证
68
     * @param $key        上传文件名
69
     * @param $filePath   上传文件的路径
70
     * @param $params     自定义变量,规格参考
71
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
72
     * @param $mime       上传数据的mimeType
73
     * @param $checkCrc   是否校验crc32
74
     *
75
     * @return array    包含已上传文件的信息,类似:
76
     *                                              [
77
     *                                                  "hash" => "<Hash string>",
78
     *                                                  "key" => "<Key string>"
79
     *                                              ]
80
     */
81 9
    public function putFile(
82
        $upToken,
83
        $key,
84
        $filePath,
85
        $params = null,
86
        $mime = 'application/octet-stream',
87
        $checkCrc = false
88
    ) {
89 9
        $file = fopen($filePath, 'rb');
90 9
        if ($file === false) {
91
            throw new \Exception("file can not open", 1);
92
        }
93 9
        $params = self::trimParams($params);
94 9
        $stat = fstat($file);
95 9
        $size = $stat['size'];
96 9
        if ($size <= Config::BLOCK_SIZE) {
97 3
            $data = fread($file, $size);
98 3
            fclose($file);
99 3
            if ($data === false) {
100
                throw new \Exception("file can not read", 1);
101
            }
102 3
            return FormUploader::put(
103 3
                $upToken,
104 3
                $key,
105 3
                $data,
106 3
                $this->config,
107 3
                $params,
108 3
                $mime,
109 1
                $checkCrc
0 ignored issues
show
Unused Code introduced by
The call to FormUploader::put() has too many arguments starting with $checkCrc.

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...
110 2
            );
111
        }
112
113 6
        $up = new ResumeUploader(
114 6
            $upToken,
115 6
            $key,
116 6
            $file,
117 6
            $size,
118 6
            $params,
119 6
            $mime,
120 6
            $this->config
121 4
        );
122 3
        $ret = $up->upload();
123 3
        fclose($file);
124 3
        return $ret;
125
    }
126
127 12
    public static function trimParams($params)
128
    {
129 12
        if ($params === null) {
130 12
            return null;
131
        }
132
        $ret = array();
133
        foreach ($params as $k => $v) {
134
            $pos = strpos($k, 'x:');
135
            if ($pos === 0 && !empty($v)) {
136
                $ret[$k] = $v;
137
            }
138
        }
139
        return $ret;
140
    }
141
}
142