Conditions | 8 |
Paths | 40 |
Total Lines | 69 |
Code Lines | 44 |
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 |
||
82 | public function import() { |
||
83 | $ch = curl_init(); |
||
84 | |||
85 | // PHP 5.5+ introduced CURLFile which makes the '@/path/to/file' syntax deprecated. |
||
86 | if(class_exists('CURLFile')) { |
||
87 | $file = new CURLFile( |
||
88 | $this->fileDescriptor['path'], |
||
89 | $this->fileDescriptor['mimeType'], |
||
90 | $this->fileDescriptor['name'] |
||
91 | ); |
||
92 | } else { |
||
93 | $file = '@' . $this->fileDescriptor['path']; |
||
94 | } |
||
95 | |||
96 | curl_setopt_array($ch, array( |
||
97 | CURLOPT_URL => $this->getDocvertUrl(), |
||
98 | CURLOPT_USERPWD => sprintf('%s:%s', $this->getDocvertUsername(), $this->getDocvertPassword()), |
||
99 | CURLOPT_POST => 1, |
||
100 | CURLOPT_POSTFIELDS => array('file' => $file), |
||
101 | CURLOPT_CONNECTTIMEOUT => 25, |
||
102 | CURLOPT_TIMEOUT => 100, |
||
103 | )); |
||
104 | |||
105 | $chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null; |
||
106 | $folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : ''; |
||
107 | $outname = tempnam(ASSETS_PATH, 'convert'); |
||
108 | $outzip = $outname . '.zip'; |
||
109 | |||
110 | $out = fopen($outzip, 'w'); |
||
111 | curl_setopt($ch, CURLOPT_FILE, $out); |
||
112 | $returnValue = curl_exec($ch); |
||
113 | $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
114 | curl_close($ch); |
||
115 | fclose($out); |
||
116 | chmod($outzip, 0666); |
||
117 | |||
118 | if (!$returnValue || ($status != 200)) { |
||
119 | return array('error' => _t( |
||
120 | __CLASS__ . '.SERVERUNREACHABLE', |
||
121 | 'Could not contact document conversion server. Please try again later or contact your system administrator.', |
||
122 | 'Document Converter process Word documents into HTML.' |
||
123 | )); |
||
124 | } |
||
125 | |||
126 | // extract the converted document into assets |
||
127 | // you need php zip, i.e. port install php5-zip |
||
128 | $zip = new ZipArchive(); |
||
129 | |||
130 | if($zip->open($outzip)) { |
||
131 | $zip->extractTo(ASSETS_PATH .$folderName); |
||
132 | } |
||
133 | |||
134 | // remove temporary files |
||
135 | unlink($outname); |
||
136 | unlink($outzip); |
||
137 | |||
138 | if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) { |
||
139 | return array('error' => _t( |
||
140 | __CLASS__ . '.PROCESSFAILED', |
||
141 | 'Could not process document, please double-check you uploaded a .doc or .docx format.', |
||
142 | 'Document Converter processes Word documents into HTML.' |
||
143 | )); |
||
144 | } |
||
145 | |||
146 | $content = file_get_contents(ASSETS_PATH . $folderName . '/index.html'); |
||
147 | |||
148 | unlink(ASSETS_PATH . $folderName . '/index.html'); |
||
149 | |||
150 | return $content; |
||
151 | } |
||
154 |