| Total Complexity | 50 |
| Total Lines | 302 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ProvidersController 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 ProvidersController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ProvidersController extends BaseController |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Получение общего списка провайдеров |
||
| 20 | */ |
||
| 21 | public function indexAction(): void |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Открытие карточки SIP провайдера и заполнение значений по умолчанию |
||
| 45 | * |
||
| 46 | * @param string $uniqid Уникальный идентификатор провайдера, если мы открываем существующего |
||
| 47 | */ |
||
| 48 | public function modifysipAction($uniqid = null): void |
||
| 49 | { |
||
| 50 | $provider = Providers::findFirstByUniqid($uniqid); |
||
| 51 | |||
| 52 | if ($provider === null) { |
||
| 53 | $uniqid = strtoupper('SIP-' . time()); |
||
| 54 | $provider = new Providers(); |
||
| 55 | $provider->type = 'SIP'; |
||
| 56 | $provider->uniqid = $uniqid; |
||
| 57 | $provider->sipuid = $uniqid; |
||
| 58 | $provider->Sip = new Sip(); |
||
|
|
|||
| 59 | $provider->Sip->uniqid = $uniqid; |
||
| 60 | $provider->Sip->type = 'friend'; |
||
| 61 | $provider->Sip->port = 5060; |
||
| 62 | $provider->Sip->disabled = '0'; |
||
| 63 | $provider->Sip->qualifyfreq = 60; |
||
| 64 | $provider->Sip->qualify = '1'; |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->view->form = new SipProviderEditForm($provider->Sip); |
||
| 68 | $this->view->represent = $provider->getRepresent(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Открытие карточки IAX провайдера и заполнение значений по умолчанию |
||
| 73 | * |
||
| 74 | * @param string $uniqid Уникальный идентификатор провайдера, если мы открываем существующего |
||
| 75 | */ |
||
| 76 | public function modifyiaxAction($uniqid = null): void |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Включение провайдера |
||
| 99 | * |
||
| 100 | * @param string $type тип провайдера SIP или IAX |
||
| 101 | * @param string $uniqid Уникальный идентификатор провайдера, если мы открываем существующего |
||
| 102 | */ |
||
| 103 | public function enableAction($type, $uniqid = null): void |
||
| 104 | { |
||
| 105 | $this->view->success = false; |
||
| 106 | switch ($type) { |
||
| 107 | case 'iax': |
||
| 108 | { |
||
| 109 | $provider = Iax::findFirstByUniqid($uniqid); |
||
| 110 | break; |
||
| 111 | } |
||
| 112 | case 'sip': |
||
| 113 | { |
||
| 114 | $provider = Sip::findFirstByUniqid($uniqid); |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | default: |
||
| 118 | $provider = null; |
||
| 119 | } |
||
| 120 | if ($provider !== null) { |
||
| 121 | $provider->disabled = '0'; |
||
| 122 | if ($provider->save() === true) { |
||
| 123 | $this->view->success = true; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Отключение провайдера |
||
| 130 | * |
||
| 131 | * @param string $type тип провайдера SIP или IAX |
||
| 132 | * @param string $uniqid Уникальный идентификатор провайдера, если мы открываем существующего |
||
| 133 | */ |
||
| 134 | public function disableAction($type, $uniqid = null): void |
||
| 135 | { |
||
| 136 | $this->view->success = false; |
||
| 137 | switch ($type) { |
||
| 138 | case 'iax': |
||
| 139 | { |
||
| 140 | $provider = Iax::findFirstByUniqid($uniqid); |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | case 'sip': |
||
| 144 | { |
||
| 145 | $provider = Sip::findFirstByUniqid($uniqid); |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | default: |
||
| 149 | $provider = null; |
||
| 150 | } |
||
| 151 | if ($provider !== null) { |
||
| 152 | $provider->disabled = '1'; |
||
| 153 | if ($provider->save() === true) { |
||
| 154 | $this->view->success = true; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Сохранение провайдера AJAX запросом из формы |
||
| 161 | * |
||
| 162 | * @param bool $type - sip или iax |
||
| 163 | * |
||
| 164 | */ |
||
| 165 | public function saveAction($type): void |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Сохранение параметров в таблицу Providers |
||
| 201 | * |
||
| 202 | * @param array $data - POST дата |
||
| 203 | * @param bool $type - sip или iax |
||
| 204 | * |
||
| 205 | * @return bool результат сохранения |
||
| 206 | */ |
||
| 207 | private function saveProvider($data, $type): bool |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Удаление провайдера |
||
| 284 | * |
||
| 285 | * @param string $uniqid Уникальный идентификатор провайдера |
||
| 286 | */ |
||
| 287 | public function deleteAction($uniqid = null): void |
||
| 317 | } |
||
| 318 | |||
| 319 | } |