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