| Total Complexity | 54 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 46 | { |
||
| 47 | // Retrieve and sort audio codecs from database |
||
| 48 | $audioCodecs = Codecs::find(['conditions' => 'type="audio"'])->toArray(); |
||
| 49 | usort($audioCodecs, [__CLASS__, 'sortArrayByPriority']); |
||
| 50 | $this->view->audioCodecs = $audioCodecs; |
||
| 51 | |||
| 52 | // Retrieve and sort video codecs from database |
||
| 53 | $videoCodecs = Codecs::find(['conditions' => 'type="video"'])->toArray(); |
||
| 54 | usort($videoCodecs, [__CLASS__, 'sortArrayByPriority']); |
||
| 55 | $this->view->videoCodecs = $videoCodecs; |
||
| 56 | |||
| 57 | // Fetch all PBX settings |
||
| 58 | $pbxSettings = PbxSettings::getAllPbxSettings(); |
||
| 59 | |||
| 60 | // Fetch and assign simple passwords for the view |
||
| 61 | $this->view->simplePasswords = $this->getSimplePasswords($pbxSettings); |
||
| 62 | |||
| 63 | // Create an instance of the GeneralSettingsEditForm and assign it to the view |
||
| 64 | $this->view->form = new GeneralSettingsEditForm(null, $pbxSettings); |
||
| 65 | $this->view->submitMode = null; |
||
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Retrieves a list of simple passwords from the given data. |
||
| 71 | * |
||
| 72 | * This function checks if the SSHPassword and WebAdminPassword in the data array are simple passwords. |
||
| 73 | * It also checks if the CloudInstanceId matches any of these passwords. |
||
| 74 | * If a simple password or a matching CloudInstanceId is found, the corresponding password key is added to the list. |
||
| 75 | * |
||
| 76 | * @param array $data The data array containing the passwords and CloudInstanceId. |
||
| 77 | * @return array The list of password keys that failed the simple password check. |
||
| 78 | */ |
||
| 79 | private function getSimplePasswords(array $data): array |
||
| 80 | { |
||
| 81 | // Initialize an array to keep track of passwords that fail the check |
||
| 82 | $passwordCheckFail = []; |
||
| 83 | |||
| 84 | $cloudInstanceId = $data['CloudInstanceId'] ?? ''; |
||
| 85 | $checkPasswordFields = [PbxSettingsConstants::SSH_PASSWORD, 'WebAdminPassword']; |
||
| 86 | |||
| 87 | // If SSH is disabled, remove the SSH_PASSWORD key |
||
| 88 | if ($data[PbxSettingsConstants::SSH_DISABLE_SSH_PASSWORD] === 'on') { |
||
| 89 | unset($checkPasswordFields[PbxSettingsConstants::SSH_PASSWORD]); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Loop through and check passwords |
||
| 93 | foreach ($checkPasswordFields as $value) { |
||
| 94 | if (!isset($data[$value]) || $data[$value] === GeneralSettingsEditForm::HIDDEN_PASSWORD) { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | if ($cloudInstanceId === $data[$value] || Util::isSimplePassword($data[$value])) { |
||
| 98 | $passwordCheckFail[] = $value; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | return $passwordCheckFail; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Saves the general settings form data. |
||
| 106 | * |
||
| 107 | */ |
||
| 108 | public function saveAction(): void |
||
| 109 | { |
||
| 110 | if (!$this->request->isPost()) { |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | $data = $this->request->getPost(); |
||
| 114 | |||
| 115 | $passwordCheckFail = $this->getSimplePasswords($data); |
||
| 116 | if (!empty($passwordCheckFail)) { |
||
| 117 | foreach ($passwordCheckFail as $settingsKey) { |
||
| 118 | $this->flash->error($this->translation->_('gs_SetPasswordError', ['password' => $data[$settingsKey]])); |
||
| 119 | } |
||
| 120 | $this->view->success = false; |
||
| 121 | $this->view->passwordCheckFail = $passwordCheckFail; |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->db->begin(); |
||
| 126 | |||
| 127 | list($result, $messages) = $this->updatePBXSettings($data); |
||
| 128 | if (!$result) { |
||
| 129 | $this->view->success = false; |
||
| 130 | $this->view->messages = $messages; |
||
| 131 | $this->db->rollback(); |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | list($result, $messages) = $this->updateCodecs($data['codecs']); |
||
| 136 | if (!$result) { |
||
| 137 | $this->view->success = false; |
||
| 138 | $this->view->messages = $messages; |
||
| 139 | $this->db->rollback(); |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | list($result, $messages) = $this->createParkingExtensions( |
||
| 144 | $data[PbxSettingsConstants::PBX_CALL_PARKING_START_SLOT], |
||
| 145 | $data[PbxSettingsConstants::PBX_CALL_PARKING_END_SLOT], |
||
| 146 | $data[PbxSettingsConstants::PBX_CALL_PARKING_EXT], |
||
| 147 | ); |
||
| 148 | |||
| 149 | if (!$result) { |
||
| 150 | $this->view->success = false; |
||
| 151 | $this->view->messages = $messages; |
||
| 152 | $this->db->rollback(); |
||
| 153 | return; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
||
| 157 | $this->view->success = true; |
||
| 158 | $this->db->commit(); |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Create parking extensions. |
||
| 165 | * |
||
| 166 | * @param int $startSlot |
||
| 167 | * @param int $endSlot |
||
| 168 | * @param int $reservedSlot |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | private function createParkingExtensions(int $startSlot, int $endSlot, int $reservedSlot): array |
||
| 173 | { |
||
| 174 | $messages = []; |
||
| 175 | // Delete all parking slots |
||
| 176 | $currentSlots = Extensions::findByType(Extensions::TYPE_PARKING); |
||
| 177 | foreach ($currentSlots as $currentSlot) { |
||
| 178 | if (!$currentSlot->delete()) { |
||
| 179 | $messages['error'][] = $currentSlot->getMessages(); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // Create an array of new numbers |
||
| 184 | $numbers = range($startSlot, $endSlot); |
||
| 185 | $numbers[] = $reservedSlot; |
||
| 186 | foreach ($numbers as $number) { |
||
| 187 | $record = new Extensions(); |
||
| 188 | $record->type = Extensions::TYPE_PARKING; |
||
| 189 | $record->number = $number; |
||
| 190 | if (!$record->create()) { |
||
| 191 | $messages['error'][] = $record->getMessages(); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | $result = count($messages) === 0; |
||
| 196 | return [$result, $messages]; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Update codecs based on the provided data. |
||
| 201 | * |
||
| 202 | * @param string $codecsData The JSON-encoded data for codecs. |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | private function updateCodecs(string $codecsData): array |
||
| 207 | { |
||
| 208 | $messages = []; |
||
| 209 | $codecs = json_decode($codecsData, true); |
||
| 210 | foreach ($codecs as $codec) { |
||
| 211 | $record = Codecs::findFirstById($codec['codecId']); |
||
| 212 | $record->priority = $codec['priority']; |
||
| 213 | $record->disabled = $codec['disabled'] === true ? '1' : '0'; |
||
| 214 | if (!$record->update()) { |
||
| 215 | $messages['error'][] = $record->getMessages(); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $result = count($messages) === 0; |
||
| 219 | return [$result, $messages]; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Update PBX settings based on the provided data. |
||
| 224 | * |
||
| 225 | * @param array $data The data containing PBX settings. |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | private function updatePBXSettings(array $data):array |
||
| 306 | } |
||
| 307 | |||
| 308 | } |