| Conditions | 5 |
| Paths | 5 |
| Total Lines | 71 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 2 | Features | 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 |
||
| 71 | public function transfer_file($file, $start = 0, $hash = "") |
||
| 72 | { |
||
| 73 | if (!$this->target_url) { |
||
| 74 | throw new Exception("Please setup a target url for upload"); |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | $fp = $this->get_storage_filesystem()->readStream($file); |
||
| 79 | |||
| 80 | fseek($fp, $start, SEEK_SET); |
||
| 81 | |||
| 82 | $binary_data = fread($fp, $this->transfer_limit); |
||
| 83 | |||
| 84 | $tmp_filename = "xcloner_upload_".substr(md5(time()), 0, 5); |
||
| 85 | |||
| 86 | $this->get_tmp_filesystem()->write($tmp_filename, $binary_data); |
||
| 87 | |||
| 88 | $tmp_file_path = $this->get_tmp_filesystem_adapter()->applyPathPrefix($tmp_filename); |
||
| 89 | |||
| 90 | $send_array = array(); |
||
| 91 | |||
| 92 | $send_array['file'] = $file; |
||
| 93 | $send_array['start'] = $start; |
||
| 94 | $send_array['xcloner_action'] = "write_file"; |
||
| 95 | $send_array['hash'] = $hash; |
||
| 96 | #$send_array['blob'] = $binary_data; |
||
| 97 | $send_array['blob'] = $this->curl_file_create($tmp_file_path, 'application/x-binary', $tmp_filename); |
||
| 98 | |||
| 99 | //$data = http_build_query($send_array); |
||
| 100 | |||
| 101 | $this->get_logger()->info(sprintf("Sending curl request to %s with %s data of file %s starting position %s using temporary file %s", |
||
| 102 | $this->target_url, $this->transfer_limit, $file, $start, $tmp_filename)); |
||
| 103 | |||
| 104 | |||
| 105 | $ch = curl_init(); |
||
| 106 | curl_setopt($ch, CURLOPT_URL, $this->target_url); |
||
| 107 | |||
| 108 | curl_setopt($ch, CURLOPT_POST, 1); |
||
| 109 | //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); |
||
| 110 | //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); |
||
| 111 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); |
||
| 112 | curl_setopt($ch, CURLOPT_TIMEOUT, 1200); |
||
| 113 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 114 | |||
| 115 | curl_setopt($ch, CURLOPT_POSTFIELDS, $send_array); |
||
| 116 | curl_setopt($ch, CURLOPT_VERBOSE, true); |
||
| 117 | |||
| 118 | $original_result = curl_exec($ch); |
||
| 119 | |||
| 120 | |||
| 121 | $this->get_tmp_filesystem()->delete($tmp_filename); |
||
| 122 | |||
| 123 | $result = json_decode($original_result); |
||
| 124 | |||
| 125 | if (!$result) { |
||
| 126 | throw new Exception("We have received no valid response from the remote host, original message: ".$original_result); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($result->status != 200) { |
||
| 130 | throw new Exception($result->response); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (ftell($fp) >= $this->get_storage_filesystem()->getSize($file)) { |
||
| 134 | $this->get_logger()->info(sprintf("Upload done for file %s to target url %s, transferred a total of %s bytes", |
||
| 135 | $file, $this->target_url, ftell($fp))); |
||
| 136 | $this->remove_tmp_filesystem(); |
||
| 137 | |||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | return ftell($fp); |
||
| 142 | } |
||
| 166 |