| Conditions | 18 |
| Paths | 62 |
| Total Lines | 136 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 28 | public function downloadResponseData() |
||
| 29 | { |
||
| 30 | // Check for request forgeries. |
||
| 31 | JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); |
||
| 32 | |||
| 33 | // Get items to remove from the request. |
||
| 34 | $cid = JFactory::getApplication()->input->get('cid', array(), 'array'); |
||
| 35 | |||
| 36 | if (!is_array($cid) || count($cid) < 1) |
||
| 37 | { |
||
| 38 | JLog::add(JText::_($this->text_prefix . '_NO_ITEM_SELECTED'), JLog::WARNING, 'jerror'); |
||
| 39 | } |
||
| 40 | else |
||
| 41 | { |
||
| 42 | // Make sure the item ids are integers |
||
| 43 | jimport('joomla.utilities.arrayhelper'); |
||
| 44 | ArrayHelper::toInteger($cid); |
||
| 45 | |||
| 46 | if (count($cid) > 1 && !extension_loaded('zlib')) |
||
| 47 | { |
||
| 48 | $cid = array($cid[0]); |
||
| 49 | } |
||
| 50 | |||
| 51 | $contentType = 'text/plain'; |
||
| 52 | $content = ''; |
||
| 53 | |||
| 54 | // Download single PHP file |
||
| 55 | if (count($cid) == 1) |
||
| 56 | { |
||
| 57 | /** @var RedcoreTableWebservice $table */ |
||
| 58 | $table = RTable::getAdminInstance('Webservice_History_Log', array(), 'com_redcore'); |
||
| 59 | $fileName = ''; |
||
| 60 | |||
| 61 | if ($table && $table->load($cid[0])) |
||
| 62 | { |
||
| 63 | $path = JPATH_ROOT . '/' . $table->file_name; |
||
|
|
|||
| 64 | |||
| 65 | if (is_file($path)) |
||
| 66 | { |
||
| 67 | $fileName = substr(basename($path), 0, -3) . 'txt'; |
||
| 68 | $content = file_get_contents($path); |
||
| 69 | $content = substr($content, 33); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | // Download package of PHP files in a ZIP |
||
| 74 | else |
||
| 75 | { |
||
| 76 | /** @var RedcoreTableWebservice_History_Log $table */ |
||
| 77 | $table = RTable::getAdminInstance('Webservice_History_Log', array(), 'com_redcore'); |
||
| 78 | $app = JFactory::getApplication(); |
||
| 79 | $contentType = 'application/zip'; |
||
| 80 | $fileName = 'webservice_history_logs_' . (date('Y_m_d_H_i_s')) . '.zip'; |
||
| 81 | |||
| 82 | $files = array(); |
||
| 83 | |||
| 84 | foreach ($cid as $id) |
||
| 85 | { |
||
| 86 | $table->reset(); |
||
| 87 | |||
| 88 | if ($table->load($id)) |
||
| 89 | { |
||
| 90 | $path = JPATH_ROOT . '/' . $table->file_name; |
||
| 91 | |||
| 92 | if (is_file($path)) |
||
| 93 | { |
||
| 94 | $content = file_get_contents($path); |
||
| 95 | $content = substr($content, 33); |
||
| 96 | $files[] = array( |
||
| 97 | 'name' => substr(basename($path), 0, -3) . 'txt', |
||
| 98 | 'data' => $content, |
||
| 99 | 'time' => time() |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $uniqueFile = uniqid('webservice_history_log_files_'); |
||
| 106 | $zipFile = $app->get('tmp_path') . '/' . $uniqueFile . '.zip'; |
||
| 107 | |||
| 108 | // Run the packager |
||
| 109 | jimport('joomla.filesystem.folder'); |
||
| 110 | jimport('joomla.filesystem.file'); |
||
| 111 | $delete = JFolder::files($app->get('tmp_path') . '/', $uniqueFile, false, true); |
||
| 112 | |||
| 113 | if (!empty($delete)) |
||
| 114 | { |
||
| 115 | if (!JFile::delete($delete)) |
||
| 116 | { |
||
| 117 | // JFile::delete throws an error |
||
| 118 | $this->setError(JText::_('COM_REDCORE_WEBSERVICES_ERR_ZIP_DELETE_FAILURE')); |
||
| 119 | $this->setRedirect($this->getRedirectToListRoute()); |
||
| 120 | |||
| 121 | return false; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | $packager = JArchive::getAdapter('zip'); |
||
| 126 | |||
| 127 | if (!$packager) |
||
| 128 | { |
||
| 129 | $this->setError(JText::_('COM_REDCORE_WEBSERVICES_ERR_ZIP_ADAPTER_FAILURE')); |
||
| 130 | $this->setRedirect($this->getRedirectToListRoute()); |
||
| 131 | |||
| 132 | return false; |
||
| 133 | } |
||
| 134 | elseif (!$packager->create($zipFile, $files)) |
||
| 135 | { |
||
| 136 | $this->setError(JText::_('COM_REDCORE_WEBSERVICES_ERR_ZIP_CREATE_FAILURE')); |
||
| 137 | $this->setRedirect($this->getRedirectToListRoute()); |
||
| 138 | |||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | $content = file_get_contents($zipFile); |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($content) |
||
| 146 | { |
||
| 147 | // Send the headers |
||
| 148 | header("Pragma: public"); |
||
| 149 | header("Expires: 0"); |
||
| 150 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
||
| 151 | header("Cache-Control: private", false); |
||
| 152 | header("Content-type: " . $contentType . "; charset=UTF-8"); |
||
| 153 | header("Content-Disposition: attachment; filename=\"" . $fileName . "\";"); |
||
| 154 | |||
| 155 | // Send the file |
||
| 156 | echo $content; |
||
| 157 | |||
| 158 | JFactory::getApplication()->close(); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | // Set redirect |
||
| 163 | $this->setRedirect($this->getRedirectToListRoute()); |
||
| 164 | } |
||
| 166 |