| Total Complexity | 56 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GeneralSettingsController 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 GeneralSettingsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class GeneralSettingsController extends BaseController |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Builds the general settings form. |
||
| 38 | * |
||
| 39 | * This action is responsible for preparing the data required to populate the general settings form. |
||
| 40 | * It retrieves the audio and video codecs from the database, sorts them by priority, and assigns them to the view. |
||
| 41 | * It also retrieves all PBX settings and creates an instance of the GeneralSettingsEditForm. |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function modifyAction(): void |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Retrieves a list of simple passwords from the given data. |
||
| 70 | * |
||
| 71 | * This function checks if the SSHPassword and WebAdminPassword in the data array are simple passwords. |
||
| 72 | * It also checks if the CloudInstanceId matches any of these passwords. |
||
| 73 | * If a simple password or a matching CloudInstanceId is found, the corresponding password key is added to the list. |
||
| 74 | * |
||
| 75 | * @param array $data The data array containing the passwords and CloudInstanceId. |
||
| 76 | * @return array The list of password keys that failed the simple password check. |
||
| 77 | */ |
||
| 78 | private function getSimplePasswords(array $data): array |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Saves the general settings form data. |
||
| 105 | * |
||
| 106 | */ |
||
| 107 | public function saveAction(): void |
||
| 108 | { |
||
| 109 | if (!$this->request->isPost()) { |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | $postData = self::sanitizeData($this->request->getPost(), $this->filter); |
||
| 113 | |||
| 114 | // No need to sanitize these fields |
||
| 115 | $postData[PbxSettings::WEB_ADMIN_PASSWORD] = $this->request->getPost(PbxSettings::WEB_ADMIN_PASSWORD); |
||
| 116 | $postData[PbxSettings::SSH_PASSWORD] = $this->request->getPost(PbxSettings::SSH_PASSWORD); |
||
| 117 | |||
| 118 | $passwordCheckFail = $this->getSimplePasswords($postData); |
||
| 119 | if (!empty($passwordCheckFail)) { |
||
| 120 | foreach ($passwordCheckFail as $settingsKey) { |
||
| 121 | $this->flash->error($this->translation->_('gs_SetPasswordError', ['password' => $postData[$settingsKey]])); |
||
| 122 | } |
||
| 123 | $this->view->success = false; |
||
| 124 | $this->view->passwordCheckFail = $passwordCheckFail; |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->db->begin(); |
||
| 129 | |||
| 130 | list($result, $messages) = $this->updatePBXSettings($postData); |
||
| 131 | if (!$result) { |
||
| 132 | $this->view->success = false; |
||
| 133 | $this->view->messages = $messages; |
||
| 134 | $this->db->rollback(); |
||
| 135 | return; |
||
| 136 | } |
||
| 137 | |||
| 138 | list($result, $messages) = $this->updateCodecs($postData['codecs']); |
||
| 139 | if (!$result) { |
||
| 140 | $this->view->success = false; |
||
| 141 | $this->view->messages = $messages; |
||
| 142 | $this->db->rollback(); |
||
| 143 | return; |
||
| 144 | } |
||
| 145 | |||
| 146 | list($result, $messages) = $this->createParkingExtensions( |
||
| 147 | $postData[PbxSettings::PBX_CALL_PARKING_START_SLOT], |
||
| 148 | $postData[PbxSettings::PBX_CALL_PARKING_END_SLOT], |
||
| 149 | $postData[PbxSettings::PBX_CALL_PARKING_EXT], |
||
| 150 | ); |
||
| 151 | |||
| 152 | if (!$result) { |
||
| 153 | $this->view->success = false; |
||
| 154 | $this->view->messages = $messages; |
||
| 155 | $this->db->rollback(); |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
||
| 160 | $this->view->success = true; |
||
| 161 | $this->db->commit(); |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Create or update parking extensions by ensuring only necessary slots are modified. |
||
| 167 | * This method first fetches the existing parking slots and determines which slots |
||
| 168 | * need to be created or deleted based on the desired range and reserved slot. |
||
| 169 | * It aims to minimize database operations by only deleting slots that are no longer needed |
||
| 170 | * and creating new slots that do not exist yet, preserving all others. |
||
| 171 | * |
||
| 172 | * @param int $startSlot The starting number of the parking slot range. |
||
| 173 | * @param int $endSlot The ending number of the parking slot range. |
||
| 174 | * @param int $reservedSlot The number of the reserved slot to be included outside the range. |
||
| 175 | * |
||
| 176 | * @return array Returns an array with two elements: |
||
| 177 | * - bool: true if the operation was successful without any errors, false otherwise. |
||
| 178 | * - array: an array of messages, primarily errors encountered during operations. |
||
| 179 | */ |
||
| 180 | private function createParkingExtensions(int $startSlot, int $endSlot, int $reservedSlot): array |
||
| 181 | { |
||
| 182 | $messages = []; |
||
| 183 | |||
| 184 | // Retrieve all current parking slots. |
||
| 185 | $currentSlots = Extensions::findByType(Extensions::TYPE_PARKING); |
||
| 186 | |||
| 187 | // Create an array of desired numbers. |
||
| 188 | $desiredNumbers = range($startSlot, $endSlot); |
||
| 189 | $desiredNumbers[] = $reservedSlot; |
||
| 190 | |||
| 191 | // Determine slots to delete. |
||
| 192 | $currentNumbers = []; |
||
| 193 | foreach ($currentSlots as $slot) { |
||
| 194 | if (!in_array($slot->number, $desiredNumbers)) { |
||
| 195 | if (!$slot->delete()) { |
||
| 196 | $messages['error'][] = $slot->getMessages(); |
||
| 197 | } |
||
| 198 | } else { |
||
| 199 | $currentNumbers[] = $slot->number; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | // Determine slots to create. |
||
| 204 | $numbersToCreate = array_diff($desiredNumbers, $currentNumbers); |
||
| 205 | foreach ($numbersToCreate as $number) { |
||
| 206 | $record = new Extensions(); |
||
| 207 | $record->type = Extensions::TYPE_PARKING; |
||
| 208 | $record->number = $number; |
||
| 209 | $record->show_in_phonebook = '0'; |
||
| 210 | if (!$record->create()) { |
||
| 211 | $messages['error'][] = $record->getMessages(); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | // Determine the overall result. |
||
| 216 | $result = count($messages['error'] ?? []) === 0; |
||
| 217 | return [$result, $messages]; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Update codecs based on the provided data. |
||
| 222 | * |
||
| 223 | * @param string $codecsData The JSON-encoded data for codecs. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | private function updateCodecs(string $codecsData): array |
||
| 228 | { |
||
| 229 | $messages = []; |
||
| 230 | $codecs = json_decode($codecsData, true); |
||
| 231 | foreach ($codecs as $codec) { |
||
| 232 | $record = Codecs::findFirstById($codec['codecId']); |
||
| 233 | $newPriority = $codec['priority']; |
||
| 234 | $newStatus = $codec['disabled'] === true ? '1' : '0'; |
||
| 235 | if (intval($record->priority) !== intval($newPriority) || $record->disabled !== $newStatus) { |
||
| 236 | $record->priority = $newPriority; |
||
| 237 | $record->disabled = $newStatus; |
||
| 238 | if (!$record->update()) { |
||
| 239 | $messages['error'][] = $record->getMessages(); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | $result = count($messages) === 0; |
||
| 244 | return [$result, $messages]; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Update PBX settings based on the provided data. |
||
| 249 | * |
||
| 250 | * @param array $data The data containing PBX settings. |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | private function updatePBXSettings(array $data): array |
||
| 332 | } |
||
| 333 | } |
||
| 334 |