| Total Complexity | 43 |
| Total Lines | 335 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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'); |
||
| 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 |
||
| 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); |
||
| 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']); |
||
| 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( |
||
| 354 | } |
||
| 355 | } |
||
| 356 |