| Conditions | 8 |
| Paths | 8 |
| Total Lines | 53 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | private function pointUpload($path, $stream, $params) { |
||
| 52 | $req = new Rest($this->config); |
||
| 53 | $headers = array(); |
||
| 54 | if (is_array($params)) { |
||
| 55 | foreach($params as $key => $val) { |
||
| 56 | $headers['X-Upyun-Meta-' . $key] = $val; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | $res = $req->request('PUT', $path) |
||
| 60 | ->withHeaders(array_merge(array( |
||
| 61 | 'X-Upyun-Multi-Stage' => 'initiate', |
||
| 62 | 'X-Upyun-Multi-Type' => Psr7\mimetype_from_filename($path), |
||
| 63 | 'X-Upyun-Multi-Length' => $stream->getSize(), |
||
| 64 | ), $headers)) |
||
| 65 | ->send(); |
||
| 66 | if ($res->getStatusCode() !== 204) { |
||
| 67 | throw new \Exception('init request failed when poinit upload!'); |
||
| 68 | } |
||
| 69 | |||
| 70 | $init = Util::getHeaderParams($res->getHeaders()); |
||
| 71 | $uuid = $init['x-upyun-multi-uuid']; |
||
| 72 | $blockSize = 1024 * 1024; |
||
| 73 | $partId = 0; |
||
| 74 | do { |
||
| 75 | $fileBlock = $stream->read($blockSize); |
||
| 76 | $res = $req->request('PUT', $path) |
||
| 77 | ->withHeaders(array( |
||
| 78 | 'X-Upyun-Multi-Stage' => 'upload', |
||
| 79 | 'X-Upyun-Multi-Uuid' => $uuid, |
||
| 80 | 'X-Upyun-Part-Id' => $partId |
||
| 81 | )) |
||
| 82 | ->withFile(Psr7\stream_for($fileBlock)) |
||
| 83 | ->send(); |
||
| 84 | |||
| 85 | if ($res->getStatusCode() !== 204) { |
||
| 86 | throw new \Exception('upload request failed when poinit upload!'); |
||
| 87 | } |
||
| 88 | $data = Util::getHeaderParams($res->getHeaders()); |
||
| 89 | $partId = $data['x-upyun-next-part-id']; |
||
| 90 | } while($partId != -1); |
||
| 91 | |||
| 92 | $res = $req->request('PUT', $path) |
||
| 93 | ->withHeaders(array( |
||
| 94 | 'X-Upyun-Multi-Uuid' => $uuid, |
||
| 95 | 'X-Upyun-Multi-Stage' => 'complete' |
||
| 96 | )) |
||
| 97 | ->send(); |
||
| 98 | |||
| 99 | if ($res->getStatusCode() != 204 && $res->getStatusCode() != 201) { |
||
| 100 | throw new \Exception('end request failed when poinit upload!'); |
||
| 101 | } |
||
| 102 | return $res; |
||
| 103 | } |
||
| 104 | |||
| 116 |