| Total Complexity | 43 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like IvrMenuController 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 IvrMenuController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class IvrMenuController extends BaseController |
||
| 16 | { |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * Builds IVR menu representation |
||
| 21 | */ |
||
| 22 | public function indexAction(): void |
||
| 23 | { |
||
| 24 | $records = IvrMenuActions::find(); |
||
| 25 | $ivrMenuActions=[]; |
||
| 26 | foreach ($records as $record) { |
||
| 27 | $ivrMenuActions[$record->ivr_menu_id][$record->id]=[ |
||
| 28 | 'digits'=>$record->digits, |
||
| 29 | 'represent'=>$record->Extensions===null?'ERROR':$record->Extensions->getRepresent() |
||
| 30 | ]; |
||
| 31 | } |
||
| 32 | |||
| 33 | $records = IvrMenu::find(); |
||
| 34 | $ivrMenuList=[]; |
||
| 35 | foreach ($records as $record) { |
||
| 36 | usort($ivrMenuActions[$record->uniqid], [__CLASS__, 'sortArrayByDigits']); |
||
| 37 | $ivrMenuList[]=[ |
||
| 38 | 'uniqid'=>$record->uniqid, |
||
| 39 | 'name'=>$record->name, |
||
| 40 | 'extension'=>$record->extension, |
||
| 41 | 'actions'=>$ivrMenuActions[$record->uniqid], |
||
| 42 | 'description'=>$record->description, |
||
| 43 | 'timeoutExtension'=>$record->TimeoutExtensions===null?'ERROR':$record->TimeoutExtensions->getRepresent() |
||
| 44 | ]; |
||
| 45 | } |
||
| 46 | $this->view->ivrmenu = $ivrMenuList; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Карточка редактирования IVR меню |
||
| 51 | * |
||
| 52 | * @param string $ivrmenuid идентификатор меню |
||
| 53 | */ |
||
| 54 | public function modifyAction($ivrmenuid = ''): void |
||
| 55 | { |
||
| 56 | $ivrmenu = IvrMenu::findFirstByUniqid($ivrmenuid); |
||
| 57 | $ivrActionsList = []; |
||
| 58 | $soundfilesList = []; |
||
| 59 | $extensionList = []; |
||
| 60 | $soundfilesList[""] = $this->translation->_("sf_SelectAudioFile"); |
||
| 61 | $extensionList[""] = $this->translation->_("ex_SelectNumber"); |
||
| 62 | $extensionListForFilter = []; |
||
| 63 | if ($ivrmenu === null) { |
||
| 64 | $ivrmenu = new IvrMenu(); |
||
| 65 | $ivrmenu->uniqid = strtoupper('IVR-' . md5($ivrmenu->id . time())); |
||
| 66 | $ivrmenu->number_of_repeat = 3; |
||
| 67 | $ivrmenu->extension |
||
| 68 | = Extensions::getNextFreeApplicationNumber(); |
||
| 69 | $ivrmenu->timeout = 7; |
||
| 70 | } else { |
||
| 71 | $extensionListForFilter[] = $ivrmenu->timeout_extension; |
||
| 72 | // Списк экстеншенов очереди |
||
| 73 | $parameters = [ |
||
| 74 | 'conditions' => 'ivr_menu_id=:menu:', |
||
| 75 | 'bind' => [ |
||
| 76 | 'menu' => $ivrmenu->uniqid, |
||
| 77 | ], |
||
| 78 | ]; |
||
| 79 | $actions = IvrMenuActions::find($parameters); |
||
| 80 | foreach ($actions as $action) { |
||
| 81 | $ivrActionsList[] = [ |
||
| 82 | 'id' => $action->id, |
||
| 83 | 'extension' => $action->extension, |
||
| 84 | 'extensionRepresent' => str_replace( |
||
| 85 | '"', |
||
| 86 | '\\"', |
||
| 87 | $action->Extensions->getRepresent() |
||
| 88 | ), |
||
| 89 | 'digits' => $action->digits, |
||
| 90 | ]; |
||
| 91 | $extensionListForFilter[] = $action->extension; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | usort($ivrActionsList, [__CLASS__, 'sortArrayByDigits']); |
||
| 95 | |||
| 96 | // Список всех эктеншенов выбранных ранее для правил адресации |
||
| 97 | if (count($extensionListForFilter) > 0) { |
||
| 98 | $parameters = [ |
||
| 99 | 'conditions' => 'number IN ({ids:array})', |
||
| 100 | 'bind' => [ |
||
| 101 | 'ids' => $extensionListForFilter, |
||
| 102 | ], |
||
| 103 | ]; |
||
| 104 | $extensions = Extensions::find($parameters); |
||
| 105 | foreach ($extensions as $record) { |
||
| 106 | $extensionList[$record->number] = $record->getRepresent(); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | // Список звуковых файлов для IVR |
||
| 111 | $soundFiles = SoundFiles::find('category="custom"'); |
||
| 112 | foreach ($soundFiles as $soundFile) { |
||
| 113 | $soundfilesList[$soundFile->id] = $soundFile->getRepresent(); |
||
| 114 | } |
||
| 115 | |||
| 116 | $form = new IvrMenuEditForm( |
||
| 117 | $ivrmenu, [ |
||
| 118 | 'extensions' => $extensionList, |
||
| 119 | 'soundfiles' => $soundfilesList, |
||
| 120 | ] |
||
| 121 | ); |
||
| 122 | $this->view->form = $form; |
||
| 123 | $this->view->ivractions = $ivrActionsList; |
||
| 124 | $this->view->represent = $ivrmenu->getRepresent(); |
||
| 125 | $this->view->extension = $ivrmenu->extension; |
||
| 126 | |||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Sorts array by digits field |
||
| 131 | * |
||
| 132 | * @param $a |
||
| 133 | * @param $b |
||
| 134 | * |
||
| 135 | * @return int|null |
||
| 136 | */ |
||
| 137 | public function sortArrayByDigits($a, $b): ?int |
||
| 138 | { |
||
| 139 | $a = (int)$a['digits']; |
||
| 140 | $b = (int)$b['digits']; |
||
| 141 | if ($a === $b) { |
||
| 142 | return 0; |
||
| 143 | } else { |
||
| 144 | return ($a < $b) ? -1 : 1; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Сохранение ivr меню |
||
| 150 | */ |
||
| 151 | public function saveAction(): void |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Обновление параметров внутреннего номера |
||
| 210 | * |
||
| 211 | * @param \MikoPBX\Common\Models\Extensions $extension |
||
| 212 | * @param array $data массив полей из POST запроса |
||
| 213 | * |
||
| 214 | * @return bool update result |
||
| 215 | */ |
||
| 216 | private function updateExtension(Extensions $extension, array $data): bool |
||
| 217 | { |
||
| 218 | $extension->number = $data['extension']; |
||
| 219 | $extension->callerid = $this->sanitizeCallerId($data['name']); |
||
| 220 | if ($extension->save() === false) { |
||
| 221 | $errors = $extension->getMessages(); |
||
| 222 | $this->flash->error(implode('<br>', $errors)); |
||
| 223 | |||
| 224 | return false; |
||
| 225 | } |
||
| 226 | |||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Обновление параметров IVR меню |
||
| 232 | * |
||
| 233 | * @param \MikoPBX\Common\Models\IvrMenu $ivrMenu |
||
| 234 | * @param array $data массив полей из POST запроса |
||
| 235 | * |
||
| 236 | * @return bool update result |
||
| 237 | */ |
||
| 238 | private function updateIVRMenu(IvrMenu $ivrMenu, array $data): bool |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Обновление действий в IVR меню |
||
| 273 | * |
||
| 274 | * @param array $data массив полей из POST запроса |
||
| 275 | * |
||
| 276 | * @return bool update result |
||
| 277 | */ |
||
| 278 | private function updateIVRMenuActions(array $data): bool |
||
| 279 | { |
||
| 280 | $existDigits = []; |
||
| 281 | |||
| 282 | // Заполним параметры IvrMenuActions |
||
| 283 | $arrActions = json_decode($data['actions']); |
||
| 284 | foreach ($arrActions as $value) { |
||
| 285 | $parameters = [ |
||
| 286 | 'conditions' => 'ivr_menu_id = :uniqid: AND digits=:digits:', |
||
| 287 | 'bind' => [ |
||
| 288 | 'digits' => $value->digits, |
||
| 289 | 'uniqid' => $data['uniqid'], |
||
| 290 | ], |
||
| 291 | ]; |
||
| 292 | $newRule = IvrMenuActions::findFirst($parameters); |
||
| 293 | if ($newRule === null) { |
||
| 294 | $newRule = new IvrMenuActions(); |
||
| 295 | $newRule->digits = $value->digits; |
||
| 296 | $newRule->ivr_menu_id = $data['uniqid']; |
||
| 297 | } |
||
| 298 | $newRule->extension = $value->extension; |
||
| 299 | if ($newRule->save() === false) { |
||
| 300 | $errors = $newRule->getMessages(); |
||
| 301 | $this->flash->warning(implode('<br>', $errors)); |
||
| 302 | $this->view->success = false; |
||
| 303 | |||
| 304 | return false; |
||
| 305 | } |
||
| 306 | $existDigits[] = $value->digits; |
||
| 307 | } |
||
| 308 | |||
| 309 | // Удалим лишние элементы меню |
||
| 310 | $parameters = [ |
||
| 311 | 'conditions' => 'digits NOT IN ({numbers:array}) AND ivr_menu_id=:uniqid:', |
||
| 312 | 'bind' => [ |
||
| 313 | 'numbers' => $existDigits, |
||
| 314 | 'uniqid' => $data['uniqid'], |
||
| 315 | ], |
||
| 316 | ]; |
||
| 317 | |||
| 318 | $deletedActions = IvrMenuActions::find($parameters); |
||
| 319 | if ($deletedActions && $deletedActions->delete() === false) { |
||
| 320 | $errors = $deletedActions->getMessages(); |
||
| 321 | $this->flash->error(implode('<br>', $errors)); |
||
| 322 | |||
| 323 | return false; |
||
| 324 | } |
||
| 325 | |||
| 326 | return true; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Удаление ivr меню |
||
| 331 | * |
||
| 332 | * @param string $uniqid |
||
| 333 | */ |
||
| 334 | public function deleteAction($uniqid = '') |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | } |