| Conditions | 8 |
| Paths | 40 |
| Total Lines | 71 |
| Code Lines | 46 |
| 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 |
||
| 135 | public function import() |
||
| 136 | { |
||
| 137 | $ch = curl_init(); |
||
| 138 | |||
| 139 | // PHP 5.5+ introduced CURLFile which makes the '@/path/to/file' syntax deprecated. |
||
| 140 | if (class_exists('CURLFile')) { |
||
| 141 | $file = new CURLFile( |
||
| 142 | $this->fileDescriptor['path'], |
||
| 143 | $this->fileDescriptor['mimeType'], |
||
| 144 | $this->fileDescriptor['name'] |
||
| 145 | ); |
||
| 146 | } else { |
||
| 147 | $file = '@' . $this->fileDescriptor['path']; |
||
| 148 | } |
||
| 149 | |||
| 150 | curl_setopt_array($ch, [ |
||
| 151 | CURLOPT_URL => $this->getUrl(), |
||
| 152 | CURLOPT_USERPWD => sprintf('%s:%s', $this->getUsername(), $this->getPassword()), |
||
| 153 | CURLOPT_POST => 1, |
||
| 154 | CURLOPT_POSTFIELDS => ['file' => $file], |
||
| 155 | CURLOPT_CONNECTTIMEOUT => 25, |
||
| 156 | CURLOPT_TIMEOUT => 100, |
||
| 157 | ]); |
||
| 158 | |||
| 159 | $chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null; |
||
| 160 | $folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : ''; |
||
| 161 | $outname = tempnam(ASSETS_PATH, 'convert'); |
||
| 162 | $outzip = $outname . '.zip'; |
||
| 163 | $out = fopen($outzip, 'w'); |
||
| 164 | curl_setopt($ch, CURLOPT_FILE, $out); |
||
| 165 | $returnValue = curl_exec($ch); |
||
| 166 | $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 167 | curl_close($ch); |
||
| 168 | fclose($out); |
||
| 169 | chmod($outzip, 0666); |
||
| 170 | |||
| 171 | if (!$returnValue || ($status != 200)) { |
||
| 172 | return ['error' => _t( |
||
| 173 | __CLASS__ . '.SERVERUNREACHABLE', |
||
| 174 | 'Could not contact document conversion server. Please try again later ' . |
||
| 175 | 'or contact your system administrator.', |
||
| 176 | 'Document Converter process Word documents into HTML.' |
||
| 177 | )]; |
||
| 178 | } |
||
| 179 | |||
| 180 | // extract the converted document into assets |
||
| 181 | // you need php zip, e.g. apt-get install php-zip |
||
| 182 | $zip = new ZipArchive(); |
||
| 183 | |||
| 184 | if ($zip->open($outzip)) { |
||
| 185 | $zip->extractTo(ASSETS_PATH .$folderName); |
||
| 186 | $zip->close(); |
||
| 187 | } |
||
| 188 | |||
| 189 | // remove temporary files |
||
| 190 | unlink($outname); |
||
| 191 | unlink($outzip); |
||
| 192 | |||
| 193 | if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) { |
||
| 194 | return ['error' => _t( |
||
| 195 | __CLASS__ . '.PROCESSFAILED', |
||
| 196 | 'Could not process document, please double-check you uploaded a .doc or .docx format.', |
||
| 197 | 'Document Converter processes Word documents into HTML.' |
||
| 198 | )]; |
||
| 199 | } |
||
| 200 | |||
| 201 | $content = file_get_contents(ASSETS_PATH . $folderName . '/index.html'); |
||
| 202 | |||
| 203 | unlink(ASSETS_PATH . $folderName . '/index.html'); |
||
| 204 | |||
| 205 | return $content; |
||
| 206 | } |
||
| 208 |