RedcoreModelConfig   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 128
c 0
b 0
f 0
dl 0
loc 335
rs 8.96

7 Methods

Rating   Name   Duplication   Size   Complexity  
F getInstalledExtensions() 0 91 17
A getForm() 0 39 3
A getComponent() 0 9 1
A loadExtensionLanguage() 0 10 4
A getTable() 0 6 3
A loadFormData() 0 26 4
B save() 0 86 11

How to fix   Complexity   

Complex Class

Complex classes like RedcoreModelConfig often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RedcoreModelConfig, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package     Redcore.Backend
4
 * @subpackage  Models
5
 *
6
 * @copyright   Copyright (C) 2008 - 2021 redWEB.dk. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('_JEXEC') or die;
11
12
/**
13
 * Config Model
14
 *
15
 * @package     Redcore.Backend
16
 * @subpackage  Models
17
 * @since       1.0
18
 */
19
class RedcoreModelConfig extends RModelAdmin
20
{
21
	/**
22
	 * Method to get a form object.
23
	 *
24
	 * @param   array    $data      Data for the form.
25
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
26
	 *
27
	 * @return  mixed  A JForm object on success, false on failure
28
	 */
29
	public function getForm($data = array(), $loadData = true)
30
	{
31
		$option = JFactory::getApplication()->input->getString('component');
0 ignored issues
show
Bug introduced by
The type JFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
33
		// Add the search path for the admin component config.xml file.
34
		JForm::addFormPath(JPATH_ADMINISTRATOR . '/components/' . $option);
35
36
		// Get the form.
37
		/** @var RForm $form */
38
		$form = $this->loadForm(
39
			'com_redcore.config',
40
			'config',
41
			array('control' => 'jform', 'load_data' => $loadData),
42
			false,
43
			'/config'
44
		);
45
46
		if (empty($form))
47
		{
48
			/** @var RForm $form */
49
			$form = $this->loadForm(
50
				'com_redcore.translations',
51
				'config',
52
				array('control' => 'jform', 'load_data' => $loadData),
53
				false,
54
				'/config'
55
			);
56
		}
57
		else
58
		{
59
			$form->loadFile('translations', false, '/config');
60
		}
61
62
		if (empty($form))
63
		{
64
			return false;
65
		}
66
67
		return $form;
68
	}
69
70
	/**
71
	 * Get the associated JTable
72
	 *
73
	 * @param   string  $name    Table name
74
	 * @param   string  $prefix  Table prefix
75
	 * @param   array   $config  Configuration array
76
	 *
77
	 * @return  JTable
0 ignored issues
show
Bug introduced by
The type JTable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
	 */
79
	public function getTable($name = null, $prefix = '', $config = array())
80
	{
81
		$name = !empty($name) ? $name : 'Extension';
82
		$prefix = !empty($prefix) ? $prefix : 'JTable';
83
84
		return parent::getTable($name, $prefix, $config);
85
	}
86
87
	/**
88
	 * Get the component information.
89
	 *
90
	 * @param   string  $option  Option name
91
	 *
92
	 * @return  object
93
	 */
94
	public function getComponent($option)
95
	{
96
		$this->loadExtensionLanguage($option, $option);
97
		$this->loadExtensionLanguage($option, $option . '.sys');
98
		$component = JComponentHelper::getComponent($option);
0 ignored issues
show
Bug introduced by
The type JComponentHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
		$component->option = $option;
100
		$component->xml = RComponentHelper::getComponentManifestFile($option);
101
102
		return $component;
103
	}
104
105
	/**
106
	 * Load specific language file.
107
	 *
108
	 * @param   string  $option         Option name
109
	 * @param   string  $extensionFile  Extension File Name
110
	 *
111
	 * @return  object
112
	 */
113
	public function loadExtensionLanguage($option, $extensionFile)
114
	{
115
		// Load common and local language files.
116
		$lang = JFactory::getLanguage();
117
118
		// Load language file
119
		$lang->load($extensionFile, JPATH_BASE, null, false, false)
120
		|| $lang->load($extensionFile, JPATH_BASE . "/components/$option", null, false, false)
121
		|| $lang->load($extensionFile, JPATH_BASE, $lang->getDefault(), false, false)
122
		|| $lang->load($extensionFile, JPATH_BASE . "/components/$option", $lang->getDefault(), false, false);
123
	}
124
125
	/**
126
	 * Method to save the configuration data.
127
	 *
128
	 * @param   array  $data  An array containing all global config data.
129
	 *
130
	 * @return  bool   True on success, false on failure.
131
	 */
132
	public function save($data)
133
	{
134
		$dispatcher = RFactory::getDispatcher();
135
		$table = JTable::getInstance('Extension');
136
		$option = JFactory::getApplication()->input->getString('component');
137
		$isNew = true;
138
139
		// Save the rules.
140
		if (isset($data['params']) && isset($data['params']['rules']))
141
		{
142
			$rules = new JAccessRules($data['params']['rules']);
0 ignored issues
show
Bug introduced by
The type JAccessRules was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
143
			$asset = JTable::getInstance('asset');
144
145
			if (!$asset->loadByName($option))
146
			{
147
				$root = JTable::getInstance('asset');
148
				$root->loadByName('root.1');
149
				$asset->name = $option;
150
				$asset->title = $option;
151
				$asset->setLocation($root->id, 'last-child');
152
			}
153
154
			$asset->rules = (string) $rules;
155
156
			if (!$asset->check() || !$asset->store())
157
			{
158
				$this->setError($asset->getError());
159
160
				return false;
161
			}
162
163
			// We don't need this anymore
164
			unset($data['params']['rules']);
165
		}
166
167
		// Load the previous Data
168
		if (!$table->load($data['id']))
169
		{
170
			$this->setError($table->getError());
171
172
			return false;
173
		}
174
175
		unset($data['id']);
176
177
		// Bind the data.
178
		if (!$table->bind($data))
179
		{
180
			$this->setError($table->getError());
181
182
			return false;
183
		}
184
185
		// Check the data.
186
		if (!$table->check())
187
		{
188
			$this->setError($table->getError());
189
190
			return false;
191
		}
192
193
		// Trigger the onConfigurationBeforeSave event.
194
		$result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, $table, $isNew));
195
196
		if (in_array(false, $result, true))
197
		{
198
			$this->setError($table->getError());
199
200
			return false;
201
		}
202
203
		// Store the data.
204
		if (!$table->store())
205
		{
206
			$this->setError($table->getError());
207
208
			return false;
209
		}
210
211
		// Clean the component cache.
212
		$this->cleanCache('_system');
213
214
		// Trigger the onConfigurationAfterSave event.
215
		$dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, $table, $isNew));
216
217
		return true;
218
	}
219
220
	/**
221
	 * Method to get the data that should be injected in the form.
222
	 *
223
	 * @return  array  The default data is an empty array.
224
	 */
225
	protected function loadFormData()
226
	{
227
		// Check the session for previously entered form data.
228
		$data = JFactory::getApplication()->getUserState(
229
			$this->context . '.data',
230
			array()
231
		);
232
233
		if (empty($data))
234
		{
235
			$option = JFactory::getApplication()->input->getString('component');
236
237
			if ($option)
238
			{
239
				$table = JTable::getInstance('Extension');
240
241
				if ($table->load(array('element' => $option, 'type' => 'component')))
242
				{
243
					$data = $this->getItem($table->extension_id);
244
245
					$data = $data->params;
246
				}
247
			}
248
		}
249
250
		return $data;
251
	}
252
253
	/**
254
	 * Gets Installed extensions
255
	 *
256
	 * @param   string   $extensionType      Extension type
257
	 * @param   array    $extensionElements  Extension element search type
258
	 * @param   array    $extensionFolder    Folder user when searching for plugin
259
	 * @param   boolean  $loadLanguage       Load language file for that extension
260
	 *
261
	 * @return  array  List of objects
262
	 */
263
	public function getInstalledExtensions(
264
		$extensionType = 'module',
265
		$extensionElements = array('%redcore%'),
266
		$extensionFolder = array('redcore'),
267
		$loadLanguage = true)
268
	{
269
		$db = $this->getDbo();
270
		$query = $db->getQuery(true)
271
			->select('e.name, e.type, e.element')
272
			->from('#__extensions AS e')
273
			->where('e.type = ' . $db->q($extensionType))
274
			->where('e.client_id = 0')
275
			->order('e.name');
276
277
		$folders = is_array($extensionFolder) ? $extensionFolder : array($extensionFolder);
0 ignored issues
show
introduced by
The condition is_array($extensionFolder) is always true.
Loading history...
278
		$isRedCore = false;
279
280
		foreach ($folders as $key => $folder)
281
		{
282
			$folders[$key] = $db->q($folder);
283
284
			if ($folder == 'redcore')
285
			{
286
				$isRedCore = true;
287
			}
288
		}
289
290
		if ($isRedCore)
291
		{
292
			$folders[] = $db->q('redpayment');
293
		}
294
295
		if ($extensionType == 'module')
296
		{
297
			$query->leftJoin('#__modules as m ON m.module = e.element')
298
				->select('m.published as enabled, m.module as moduleName')
299
				->group('e.element');
300
		}
301
		else
302
		{
303
			$query->select('e.enabled, e.folder');
304
		}
305
306
		$elements = array();
307
308
		foreach ($extensionElements as $group => $extensionElement)
309
		{
310
			if ($extensionType == 'plugin')
311
			{
312
				$elements[] = '(e.element LIKE ' . $db->q($extensionElement) . ' AND e.folder = ' . $db->quote($group) . ')';
313
			}
314
			else
315
			{
316
				$elements[] = 'e.element LIKE ' . $db->q($extensionElement);
317
			}
318
		}
319
320
		$extensionsSearch = implode(' OR ', $elements);
321
322
		if (!empty($elements) && $extensionType == 'plugin')
323
		{
324
			$extensionsSearch .= ' OR ';
325
		}
326
327
		$query->where('(' . $extensionsSearch . ($extensionType == 'plugin' ? ' e.folder IN (' . implode(',', $folders) . ')' : '') . ')');
328
		$db->setQuery($query);
329
330
		$extensions = $db->loadObjectList();
331
332
		if ($loadLanguage && !empty($extensions))
333
		{
334
			// Load common and local language files.
335
			$lang = JFactory::getLanguage();
336
337
			foreach ($extensions as $extension)
338
			{
339
				if ($extensionType == 'plugin')
340
				{
341
					$extensionName = strtolower('plg_' . $extension->folder . '_' . $extension->element);
342
					$lang->load(strtolower($extensionName), JPATH_ADMINISTRATOR, null, false, true)
343
					|| $lang->load(strtolower($extensionName), JPATH_PLUGINS . '/' . $extension->folder . '/' . $extension->element, null, false, true);
344
				}
345
				else
346
				{
347
					$lang->load($extension->moduleName, JPATH_SITE, null, false, true) ||
348
					$lang->load($extension->moduleName,  JPATH_SITE . "/modules/" . $extension->moduleName, null, false, true);
349
				}
350
			}
351
		}
352
353
		return $extensions;
354
	}
355
}
356