| Total Complexity | 59 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CallQueuesController 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 CallQueuesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class CallQueuesController extends BaseController |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Получение списка очередей вызовово |
||
| 20 | */ |
||
| 21 | public function indexAction(): void |
||
| 22 | { |
||
| 23 | $queues = CallQueues::find(); |
||
| 24 | $this->view->queues = $queues; |
||
| 25 | } |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Карточка редактирования очереди |
||
| 30 | * |
||
| 31 | * @param string $uniqid - идентификатор редактируемой очереди |
||
| 32 | */ |
||
| 33 | public function modifyAction(string $uniqid = ''): void |
||
| 104 | } |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Сохранение очереди через AJAX запрос из формы |
||
| 109 | */ |
||
| 110 | public function saveAction(): void |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Обновление параметров внутреннего номера |
||
| 168 | * |
||
| 169 | * @param \MikoPBX\Common\Models\Extensions $extension |
||
| 170 | * @param array $data массив полей из POST запроса |
||
| 171 | * |
||
| 172 | * @return bool update result |
||
| 173 | */ |
||
| 174 | private function updateExtension(Extensions $extension, array $data): bool |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Обновление параметров очереди |
||
| 190 | * |
||
| 191 | * @param \MikoPBX\Common\Models\CallQueues $queue |
||
| 192 | * @param array $data массив полей из POST запроса |
||
| 193 | * |
||
| 194 | * @return bool update result |
||
| 195 | */ |
||
| 196 | private function updateQueue(CallQueues $queue, array $data): bool |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Обновление списка участников очереди |
||
| 277 | * |
||
| 278 | * @param array $data массив полей из POST запроса |
||
| 279 | * |
||
| 280 | * @return bool update result |
||
| 281 | */ |
||
| 282 | private function updateQueueMembers(array $data): bool |
||
| 283 | { |
||
| 284 | $realMembers = []; |
||
| 285 | // Обновим настройки у существующих членов очереди |
||
| 286 | $membersTable = json_decode($data['members']); |
||
| 287 | foreach ($membersTable as $member) { |
||
| 288 | $parameters = [ |
||
| 289 | 'conditions' => 'extension = :number: AND queue=:uniqid:', |
||
| 290 | 'bind' => [ |
||
| 291 | 'number' => $member->number, |
||
| 292 | 'uniqid' => $data['uniqid'], |
||
| 293 | ], |
||
| 294 | ]; |
||
| 295 | $queueMembers = CallQueueMembers::find($parameters); |
||
| 296 | if (is_countable($queueMembers) && count($queueMembers) > 1) { |
||
| 297 | // откуда то взались лишние. Надо их всех удалить и создать нового |
||
| 298 | if ($queueMembers->delete() === false) { |
||
| 299 | $errors = $queueMembers->getMessages(); |
||
| 300 | $this->flash->error(implode('<br>', $errors)); |
||
| 301 | |||
| 302 | return false; |
||
| 303 | } |
||
| 304 | $queueMember = new CallQueueMembers(); |
||
| 305 | } elseif (is_countable($queueMembers) && count($queueMembers) === 1) { |
||
| 306 | $queueMember = $queueMembers->getFirst(); |
||
| 307 | } else { |
||
| 308 | $queueMember = new CallQueueMembers(); |
||
| 309 | } |
||
| 310 | |||
| 311 | $queueMember->priority = $member->priority; |
||
| 312 | $queueMember->extension = $member->number; |
||
| 313 | $queueMember->queue = $data['uniqid']; |
||
| 314 | $realMembers[] = $member->number; |
||
| 315 | if ($queueMember->save() === false) { |
||
| 316 | $errors = $queueMember->getMessages(); |
||
| 317 | $this->flash->error(implode('<br>', $errors)); |
||
| 318 | |||
| 319 | return false; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | // Удалим членов очереди которх нет в списке |
||
| 324 | $parameters = [ |
||
| 325 | 'conditions' => 'extension NOT IN ({numbers:array}) AND queue=:uniqid:', |
||
| 326 | 'bind' => [ |
||
| 327 | 'numbers' => $realMembers, |
||
| 328 | 'uniqid' => $data['uniqid'], |
||
| 329 | ], |
||
| 330 | ]; |
||
| 331 | |||
| 332 | $deletedMembers = CallQueueMembers::find($parameters); |
||
| 333 | if ($deletedMembers && $deletedMembers->delete() === false) { |
||
| 334 | $errors = $deletedMembers->getMessages(); |
||
| 335 | $this->flash->error(implode('<br>', $errors)); |
||
| 336 | |||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | return true; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Удаление очереди по ее ID |
||
| 345 | * |
||
| 346 | * @param string $uniqid |
||
| 347 | */ |
||
| 348 | public function deleteAction(string $uniqid = null) |
||
| 366 | } |
||
| 367 | } |