Passed
Pull Request — develop (#922)
by Tito
07:09
created

downloadResponseData()   D

Complexity

Conditions 18
Paths 62

Size

Total Lines 136
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 136
rs 4.8666
cc 18
nc 62
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * @package     Redcore.Backend
4
 * @subpackage  Controllers
5
 *
6
 * @copyright   Copyright (C) 2008 - 2016 redCOMPONENT.com. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('_JEXEC') or die;
11
12
use Joomla\Utilities\ArrayHelper;
13
14
/**
15
 * Webservice History Logs Controller
16
 *
17
 * @package     Redcore.Backend
18
 * @subpackage  Controllers
19
 * @since       1.0
20
 */
21
class RedcoreControllerWebservice_History_Logs extends RControllerAdmin
22
{
23
	/**
24
	 * Download Response Data.
25
	 *
26
	 * @return  mixed  True if successful, false otherwise.
27
	 */
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;
0 ignored issues
show
Bug introduced by
The property file_name does not seem to exist on RedcoreTableWebservice.
Loading history...
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'));
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

118
						/** @scrutinizer ignore-deprecated */ $this->setError(JText::_('COM_REDCORE_WEBSERVICES_ERR_ZIP_DELETE_FAILURE'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
						$this->setRedirect($this->getRedirectToListRoute());
120
121
						return false;
122
					}
123
				}
124
125
				$packager = JArchive::getAdapter('zip');
126
127
				if (!$packager)
0 ignored issues
show
introduced by
$packager is of type JArchiveExtractable, thus it always evaluated to true.
Loading history...
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))
0 ignored issues
show
Bug introduced by
The method create() does not exist on JArchiveExtractable. It seems like you code against a sub-type of JArchiveExtractable such as JArchiveZip. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
				elseif (!$packager->/** @scrutinizer ignore-call */ create($zipFile, $files))
Loading history...
135
				{
136
					$this->setError(JText::_('COM_REDCORE_WEBSERVICES_ERR_ZIP_CREATE_FAILURE'));
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

136
					/** @scrutinizer ignore-deprecated */ $this->setError(JText::_('COM_REDCORE_WEBSERVICES_ERR_ZIP_CREATE_FAILURE'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
	}
165
}
166