Completed
Push — master ( 810079...4da4eb )
by Bai
10s
created

ResumeUploader::upload()   C

Complexity

Conditions 13
Paths 20

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 19.9276

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 20
nop 0
dl 0
loc 35
ccs 19
cts 29
cp 0.6552
crap 19.9276
rs 5.1234
c 0
b 0
f 0

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
28
    /**
29
     * 上传二进制流到七牛
30
     *
31
     * @param $upToken    上传凭证
32
     * @param $key        上传文件名
33
     * @param $inputStream 上传二进制流
34
     * @param $size       上传流的大小
35
     * @param $params     自定义变量
36
     * @param $mime       上传数据的mimeType
37
     *
38
     * @link http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
39
     */
40 6
    public function __construct(
41
        $upToken,
42
        $key,
43
        $inputStream,
44
        $size,
45
        $params,
46
        $mime,
47
        $config
48
    ) {
49 6
        $this->upToken = $upToken;
50 6
        $this->key = $key;
51 6
        $this->inputStream = $inputStream;
52 6
        $this->size = $size;
53 6
        $this->params = $params;
54 6
        $this->mime = $mime;
55 6
        $this->contexts = array();
56 6
        $this->config = $config;
57
58 6
        list($upHost, $err) = $config->zone->getUpHostByToken($upToken);
59 6
        if ($err != null) {
60
            throw new \Exception($err, 1);
61
        }
62 6
        $this->host = $upHost;
63 6
    }
64
65
    /**
66
     * 上传操作
67
     */
68 6
    public function upload()
69
    {
70 6
        $uploaded = 0;
71 6
        while ($uploaded < $this->size) {
72 6
            $blockSize = $this->blockSize($uploaded);
73 6
            $data = fread($this->inputStream, $blockSize);
74 6
            if ($data === false) {
75
                throw new \Exception("file read failed", 1);
76
            }
77 6
            $crc = \Qiniu\crc32_data($data);
78 6
            $response = $this->makeBlock($data, $blockSize);
79 6
            $ret = null;
80 6
            if ($response->ok() && $response->json() != null) {
81 6
                $ret = $response->json();
82 4
            }
83 6
            if ($response->statusCode < 0) {
84
                list($bakHost, $err) = $this->config->zone->getBackupUpHostByToken($this->upToken);
85
                if ($err != null) {
86
                    return array(null, $err);
87
                }
88
                $this->host = $bakHost;
89
            }
90 6
            if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
91
                $response = $this->makeBlock($data, $blockSize);
92
                $ret = $response->json();
93
            }
94
95 6
            if (! $response->ok() || !isset($ret['crc32'])|| $crc != $ret['crc32']) {
96
                return array(null, new Error($this->currentUrl, $response));
97
            }
98 6
            array_push($this->contexts, $ret['ctx']);
99 6
            $uploaded += $blockSize;
100 4
        }
101 6
        return $this->makeFile();
102
    }
103
104
    /**
105
     * 创建块
106
     */
107 6
    private function makeBlock($block, $blockSize)
108
    {
109 6
        $url = $this->host . '/mkblk/' . $blockSize;
110 6
        return $this->post($url, $block);
111
    }
112
113 6
    private function fileUrl()
114
    {
115 6
        $url = $this->host . '/mkfile/' . $this->size;
116 6
        $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
117 6
        if ($this->key != null) {
118 6
            $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
119 4
        }
120 6
        if (!empty($this->params)) {
121
            foreach ($this->params as $key => $value) {
122
                $val =  \Qiniu\base64_urlSafeEncode($value);
123
                $url .= "/$key/$val";
124
            }
125
        }
126 6
        return $url;
127
    }
128
129
    /**
130
     * 创建文件
131
     */
132 6
    private function makeFile()
133
    {
134 6
        $url = $this->fileUrl();
135 6
        $body = implode(',', $this->contexts);
136 6
        $response = $this->post($url, $body);
137 6
        if ($response->needRetry()) {
138
            $response = $this->post($url, $body);
139
        }
140 6
        if (! $response->ok()) {
141
            return array(null, new Error($this->currentUrl, $response));
142
        }
143 6
        return array($response->json(), null);
144
    }
145
146 6
    private function post($url, $data)
147
    {
148 6
        $this->currentUrl = $url;
149 6
        $headers = array('Authorization' => 'UpToken ' . $this->upToken);
150 6
        return Client::post($url, $data, $headers);
151
    }
152
153 6
    private function blockSize($uploaded)
154
    {
155 6
        if ($this->size < $uploaded + Config::BLOCK_SIZE) {
156 6
            return $this->size - $uploaded;
157
        }
158 6
        return  Config::BLOCK_SIZE;
159
    }
160
}
161