Completed
Pull Request — master (#184)
by
unknown
03:18
created

ResumeUploader::upload()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12.0654

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 12
eloc 22
c 4
b 1
f 0
nc 18
nop 0
dl 0
loc 31
ccs 24
cts 26
cp 0.9231
crap 12.0654
rs 5.1612

How to fix   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
namespace Qiniu\Storage;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Client;
6
use Qiniu\Http\Error;
7
8
/**
9
 * 断点续上传类, 该类主要实现了断点续上传中的分块上传,
10
 * 以及相应地创建块和创建文件过程.
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkblk.html
13
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkfile.html
14
 */
15
final class ResumeUploader
16
{
17
    private $upToken;
18
    private $key;
19
    private $inputStream;
20
    private $size;
21
    private $params;
22
    private $mime;
23
    private $contexts;
24
    private $host;
25
    private $currentUrl;
26
    private $config;
27
    private $filePath
28
29
    /**
30
     * 上传二进制流到七牛
31
     *
32
     * @param $upToken    上传凭证
33
     * @param $key        上传文件名
34
     * @param $inputStream 上传二进制流
35
     * @param $size       上传流的大小
36
     * @param $params     自定义变量
37
     * @param $mime       上传数据的mimeType
38
     *
39
     * @link http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
40 6
     */
41
    public function __construct(
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PUBLIC, expecting ',' or ';'
Loading history...
42
        $upToken,
43
        $key,
44
        $inputStream,
45
        $size,
46
        $params,
47
        $mime,
48
        $config,
49 6
        $filePath=null
50 6
    ) {
51 6
        $this->upToken = $upToken;
52 6
        $this->key = $key;
53 6
        $this->inputStream = $inputStream;
54 6
        $this->size = $size;
55 6
        $this->params = $params;
56 6
        $this->mime = $mime;
57 6
        $this->contexts = array();
58 6
        $this->config = $config;
59
        $this->host = $config->getUpHost();
60
        $this->filePath=basename($filePath);
61
    }
62
63 6
    /**
64
     * 上传操作
65 6
     */
66 6
    public function upload()
67 6
    {
68 6
        $uploaded = 0;
69 6
        while ($uploaded < $this->size) {
70
            $blockSize = $this->blockSize($uploaded);
71
            $data = fread($this->inputStream, $blockSize);
72 6
            if ($data === false) {
73 6
                throw new \Exception("file read failed", 1);
74 6
            }
75 6
            $crc = \Qiniu\crc32_data($data);
76 6
            $response = $this->makeBlock($data, $blockSize);
77 6
            $ret = null;
78 6
            if ($response->ok() && $response->json() != null) {
79 3
                $ret = $response->json();
80 3
            }
81 6
            if ($response->statusCode < 0) {
82 3
                $this->host = $this->config->getUpHostBackup();
83 3
            }
84 3
            if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
85
                $response = $this->makeBlock($data, $blockSize);
86 6
                $ret = $response->json();
87
            }
88
89 6
            if (! $response->ok() || !isset($ret['crc32'])|| $crc != $ret['crc32']) {
90 6
                return array(null, new Error($this->currentUrl, $response));
91 6
            }
92 6
            array_push($this->contexts, $ret['ctx']);
93
            $uploaded += $blockSize;
94
        }
95
        return $this->makeFile();
96
    }
97
98 6
    /**
99
     * 创建块
100 6
     */
101 6
    private function makeBlock($block, $blockSize)
102
    {
103
        $url = $this->host . '/mkblk/' . $blockSize;
104 6
        return $this->post($url, $block);
105
    }
106 6
107 6
    private function fileUrl()
108 6
    {
109 6
        $url = $this->host . '/mkfile/' . $this->size;
110 6
        $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
111 6
        if ($this->key != null) {
112
            $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
113
        }
114
        if($this->fname!=null){
115
            $url.='/fname/'.  \Qiniu\base64_urlSafeEncode($this->fname);
116
        }else{
117 6
            $url.='/fname/'.  \Qiniu\base64_urlSafeEncode($this->key);
118
        }
119
        if (!empty($this->params)) {
120
            foreach ($this->params as $key => $value) {
121
                $val =  \Qiniu\base64_urlSafeEncode($value);
122
                $url .= "/$key/$val";
123 6
            }
124
        }
125 6
        return $url;
126 6
    }
127 6
128 6
    /**
129
     * 创建文件
130
     */
131 6
    private function makeFile()
132
    {
133
        $url = $this->fileUrl();
134 6
        $body = implode(',', $this->contexts);
135
        $response = $this->post($url, $body);
136
        if ($response->needRetry()) {
137 6
            $response = $this->post($url, $body);
138
        }
139 6
        if (! $response->ok()) {
140 6
            return array(null, new Error($this->currentUrl, $response));
141 6
        }
142
        return array($response->json(), null);
143
    }
144 6
145
    private function post($url, $data)
146 6
    {
147 6
        $this->currentUrl = $url;
148
        $headers = array('Authorization' => 'UpToken ' . $this->upToken);
149 6
        return Client::post($url, $data, $headers);
150
    }
151
152
    private function blockSize($uploaded)
153
    {
154
        if ($this->size < $uploaded + Config::BLOCK_SIZE) {
155
            return $this->size - $uploaded;
156
        }
157
        return  Config::BLOCK_SIZE;
158
    }
159
}
160