| Conditions | 6 |
| Paths | 7 |
| Total Lines | 66 |
| Code Lines | 34 |
| 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 | function downloadMessageAsZipFile() |
||
| 52 | { |
||
| 53 | if($this->store) { |
||
| 54 | // Generate random ZIP file name at default temporary path of PHP |
||
| 55 | $randomZipName = tempnam(sys_get_temp_dir(), 'zip'); |
||
| 56 | |||
| 57 | // Create an open zip archive. |
||
| 58 | $zip = new ZipArchive(); |
||
| 59 | $result = $zip->open($randomZipName, ZipArchive::CREATE); |
||
| 60 | |||
| 61 | if ($result === TRUE) { |
||
| 62 | for($index = 0, $count = count($this->entryIds); $index < $count; $index++) { |
||
| 63 | |||
| 64 | $this->message = mapi_msgstore_openentry($this->store, hex2bin($this->entryIds[$index])); |
||
| 65 | |||
| 66 | // get message properties. |
||
| 67 | $messageProps = mapi_getprops($this->message, array(PR_SUBJECT, PR_EC_IMAP_EMAIL, PR_MESSAGE_CLASS)); |
||
| 68 | |||
| 69 | $stream = $this->getEmlStream($messageProps); |
||
| 70 | $stat = mapi_stream_stat($stream); |
||
| 71 | |||
| 72 | // Get the stream |
||
| 73 | $datastring = ''; |
||
| 74 | for($i = 0; $i < $stat['cb']; $i += BLOCK_SIZE) { |
||
| 75 | $datastring .= mapi_stream_read($stream, BLOCK_SIZE); |
||
| 76 | // Need to discard the buffer contents to prevent memory |
||
| 77 | // exhaustion. |
||
| 78 | ob_flush(); |
||
| 79 | } |
||
| 80 | |||
| 81 | $filename = (!empty($messageProps[PR_SUBJECT])) ? $messageProps[PR_SUBJECT] : _('Untitled'); |
||
| 82 | $filename .= '.eml'; |
||
| 83 | |||
| 84 | $filename = $this->handleDuplicateFileNames($filename); |
||
| 85 | // Remove slashes to prevent unwanted directories to be created in the zip file. |
||
| 86 | $filename = str_replace('\\', '_', $filename); |
||
| 87 | $filename = str_replace('/', '_', $filename); |
||
| 88 | |||
| 89 | // Add file into zip by stream |
||
| 90 | $zip->addFromString($filename, $datastring); |
||
| 91 | } |
||
| 92 | } else { |
||
| 93 | $zip->close(); |
||
| 94 | // Remove the zip file to avoid unnecessary disk-space consumption |
||
| 95 | unlink($randomZipName); |
||
| 96 | |||
| 97 | // Throw exception if ZIP is not created successfully |
||
| 98 | throw new ZarafaException(_("ZIP is not created successfully")); |
||
| 99 | } |
||
| 100 | |||
| 101 | $zip->close(); |
||
| 102 | |||
| 103 | // Set the headers |
||
| 104 | header('Pragma: public'); |
||
| 105 | header('Expires: 0'); // set expiration time |
||
| 106 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
||
| 107 | header('Content-Disposition: attachment; filename="' . addslashes(browserDependingHTTPHeaderEncode(_("Messages").date(" d-m-Y").".zip")) . '"'); |
||
| 108 | header('Content-Transfer-Encoding: binary'); |
||
| 109 | header('Content-Type: application/zip'); |
||
| 110 | header('Content-Length: ' . filesize($randomZipName)); |
||
| 111 | |||
| 112 | // Send the actual response as ZIP file |
||
| 113 | readfile($randomZipName); |
||
| 114 | |||
| 115 | // Remove the zip file to avoid unnecessary disk-space consumption |
||
| 116 | unlink($randomZipName); |
||
| 117 | } |
||
| 178 |