| Total Complexity | 60 |
| Total Lines | 372 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IncomingContexts 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 IncomingContexts, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class IncomingContexts extends CoreConfigClass |
||
| 13 | { |
||
| 14 | public $provider; |
||
| 15 | public $login; |
||
| 16 | public string $uniqId; |
||
| 17 | |||
| 18 | private array $dialplan = []; |
||
| 19 | private array $rout_data_dial = []; |
||
| 20 | private array $confExtensions = []; |
||
| 21 | private array $routes = []; |
||
| 22 | private string $lang; |
||
| 23 | private bool $need_def_rout; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Генератор входящих контекстов. |
||
| 27 | * |
||
| 28 | * @param string | array $provider |
||
| 29 | * @param string | array $login |
||
| 30 | * @param string $uniqId |
||
| 31 | * |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | public static function generate($provider, $login = '', $uniqId = ''): string |
||
| 35 | { |
||
| 36 | $generator = new self(); |
||
| 37 | $generator->provider = $provider; |
||
| 38 | $generator->login = $login; |
||
| 39 | $generator->uniqId = $uniqId; |
||
| 40 | $generator->getSettings(); |
||
| 41 | |||
| 42 | return $generator->makeDialplan(); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getSettings(): void |
||
| 46 | { |
||
| 47 | $this->confExtensions = ConferenceConf::getConferenceExtensions(); |
||
| 48 | $this->routes = $this->getRoutes(); |
||
| 49 | $this->lang = str_replace('_', '-', $this->generalSettings['PBXLanguage']); |
||
| 50 | $this->need_def_rout = $this->checkNeedDefRout(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Возвращает массив входящих маршрутов для провайдера. |
||
| 55 | * |
||
| 56 | * @return array|string[] |
||
| 57 | */ |
||
| 58 | private function getRoutes(): array |
||
| 59 | { |
||
| 60 | if ('none' === $this->provider) { |
||
| 61 | // Звонки по sip uri. |
||
| 62 | $filter = ['provider IS NULL AND priority<>9999', 'order' => 'provider,priority,extension',]; |
||
| 63 | } elseif (is_array($this->provider)) { |
||
| 64 | $filter = [ |
||
| 65 | 'provider IN ({provider:array})', |
||
| 66 | 'bind' => ['provider' => array_keys($this->provider),], |
||
| 67 | 'order' => 'provider,priority,extension', |
||
| 68 | ]; |
||
| 69 | } else { |
||
| 70 | // Звонки через провайдера. |
||
| 71 | $filter = ["provider = '$this->provider'", 'order' => 'provider,priority,extension',]; |
||
| 72 | } |
||
| 73 | |||
| 74 | $m_data = IncomingRoutingTable::find($filter); |
||
| 75 | $data = $m_data->toArray(); |
||
| 76 | uasort($data, ExtensionsConf::class . '::sortArrayByPriority'); |
||
| 77 | |||
| 78 | return $data; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Проверка нужен ли дефолтный маршрут для провайдера. |
||
| 83 | * Наполнение таблицы маршрутизаци значением по умолчанию. |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | private function checkNeedDefRout(): bool |
||
| 88 | { |
||
| 89 | $need_def_rout = $this->needDefRout(); |
||
| 90 | if ($need_def_rout === true && 'none' !== $this->provider) { |
||
| 91 | $this->routes[] = ['number' => '', 'extension' => '', 'timeout' => '']; |
||
| 92 | } |
||
| 93 | |||
| 94 | return $need_def_rout; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Проверка нужен ли дефолтный маршрут для провайдера. |
||
| 99 | * |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | private function needDefRout(): bool |
||
| 103 | { |
||
| 104 | $need_def_rout = true; |
||
| 105 | foreach ($this->routes as $rout) { |
||
| 106 | $number = trim($rout['number']); |
||
| 107 | if ($number === 'X!' || $number === '') { |
||
| 108 | $need_def_rout = false; |
||
| 109 | break; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $need_def_rout; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function makeDialplan(): string |
||
| 117 | { |
||
| 118 | foreach ($this->routes as $rout) { |
||
| 119 | $this->generateRouteDialplan($rout); |
||
| 120 | $this->generateDialActions($rout); |
||
| 121 | } |
||
| 122 | $this->multiplyExtensionsInDialplan(); |
||
| 123 | $this->trimDialPlan(); |
||
| 124 | |||
| 125 | return $this->createSummaryDialplan(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Генерация Первой части (заголовка) входящего контекста. |
||
| 130 | * |
||
| 131 | * @param array $rout |
||
| 132 | */ |
||
| 133 | private function generateRouteDialplan(array $rout): void |
||
| 134 | { |
||
| 135 | $number = trim($rout['number']); |
||
| 136 | $rout_number = ($number === '') ? 'X!' : $number; |
||
| 137 | $rout_data = &$this->dialplan[$rout_number]; |
||
| 138 | if ( ! empty($rout_data)) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | $ext_prefix = ('none' === $this->provider) ? '' : '_'; |
||
| 142 | |||
| 143 | $rout_data .= "exten => {$ext_prefix}{$rout_number},1,NoOp(--- Incoming call ---)\n\t"; |
||
| 144 | $rout_data .= 'same => n,Set(CHANNEL(language)=' . $this->lang . ')' . "\n\t"; |
||
| 145 | $rout_data .= 'same => n,Set(CHANNEL(hangup_handler_wipe)=hangup_handler,s,1)' . "\n\t"; |
||
| 146 | $rout_data .= 'same => n,Set(__FROM_DID=${EXTEN})' . "\n\t"; |
||
| 147 | $rout_data .= 'same => n,Set(__FROM_CHAN=${CHANNEL})' . "\n\t"; |
||
| 148 | // Установка имени пира. |
||
| 149 | $rout_data .= 'same => n,ExecIf($["${CHANNEL(channeltype)}" != "Local"]?Gosub(set_from_peer,s,1))' . "\n\t"; |
||
| 150 | $rout_data .= 'same => n,ExecIf($["${CHANNEL(channeltype)}" == "Local"]?Set(__FROM_PEER=${CALLERID(num)}))' . "\n\t"; |
||
| 151 | $rout_data .= 'same => n,Gosub(add-trim-prefix-clid,${EXTEN},1)' . "\n\t"; |
||
| 152 | |||
| 153 | $rout_data .= $this->hookModulesMethod(CoreConfigClass::GENERATE_INCOMING_ROUT_BEFORE_DIAL, [$rout_number]); |
||
| 154 | // Описываем возможность прыжка в пользовательский sub контекст. |
||
| 155 | $rout_data .= " \n\t" . 'same => n,GosubIf($["${DIALPLAN_EXISTS(${CONTEXT}-custom,${EXTEN},1)}" == "1"]?${CONTEXT}-custom,${EXTEN},1)'; |
||
| 156 | |||
| 157 | if ( ! empty($rout['extension'])) { |
||
| 158 | $rout_data = rtrim($rout_data); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Формирует в Dialplan команды-действия goto dial. |
||
| 164 | * |
||
| 165 | * @param $rout |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | private function generateDialActions($rout): void |
||
| 170 | { |
||
| 171 | if (empty($rout['extension'])) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | $number = trim($rout['number']); |
||
| 175 | $rout_number = ($number === '') ? 'X!' : $number; |
||
| 176 | $this->generateDialActionsRoutNumber($rout, $rout_number); |
||
| 177 | $this->duplicateDialActionsRoutNumber($rout, $rout_number, $number); |
||
| 178 | } |
||
| 179 | |||
| 180 | private function generateDialActionsRoutNumber($rout, $rout_number): void |
||
| 198 | } |
||
| 199 | |||
| 200 | private function duplicateDialActionsRoutNumber($rout, $rout_number, $number): void |
||
| 201 | { |
||
| 202 | if ( ! is_array($this->provider)) { |
||
| 203 | return; |
||
| 204 | } |
||
| 205 | $key = $this->provider[$rout['provider']] ?? ''; |
||
| 206 | if ( ! isset($this->rout_data_dial[$key])) { |
||
| 207 | $this->rout_data_dial[$key] = ''; |
||
| 208 | } |
||
| 209 | if (empty($number)) { |
||
| 210 | $this->rout_data_dial[$key] .= $this->rout_data_dial[$rout_number]; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Добавление дополнительных exten в Dialplan |
||
| 216 | */ |
||
| 217 | private function multiplyExtensionsInDialplan(): void |
||
| 218 | { |
||
| 219 | if (is_string($this->login)) { |
||
| 220 | $this->multiplyExtensionsInDialplanStringLogin(); |
||
| 221 | } |
||
| 222 | if (is_array($this->provider)) { |
||
| 223 | foreach (array_values($this->provider) as $_login) { |
||
| 224 | $this->dialplan[$_login] = str_replace('_X!,1', "{$_login},1", $this->dialplan['X!']); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Добавляет extensions для dialplan. |
||
| 231 | */ |
||
| 232 | private function multiplyExtensionsInDialplanStringLogin(): void |
||
| 233 | { |
||
| 234 | $add_login_pattern = $this->needAddLoginExtension(); |
||
| 235 | if ($this->isMultipleRoutes($add_login_pattern)) { |
||
| 236 | $this->dialplan[$this->login] = str_replace('_X!,1', "{$this->login},1", $this->dialplan['X!']); |
||
| 237 | $this->rout_data_dial[$this->login] = $this->rout_data_dial['X!']; |
||
| 238 | } elseif ($this->defaultRouteOnly($add_login_pattern)) { |
||
| 239 | // Только маршрут "По умолчанию". |
||
| 240 | $this->dialplan[$this->login] = str_replace('_X!,1', "{$this->login},1", $this->dialplan['X!']); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Если логин не числовой, то нужно добавить такой Exten. |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | private function needAddLoginExtension(): bool |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Несколько возможных входящих маршрутов "Каскад". |
||
| 276 | * |
||
| 277 | * @param bool $add_login_pattern |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | private function isMultipleRoutes(bool $add_login_pattern): bool |
||
| 282 | { |
||
| 283 | return $add_login_pattern && array_key_exists('X!', $this->rout_data_dial) && isset($this->dialplan['X!']); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Проверка. Нужен только маршрут по умолчанию. |
||
| 288 | * |
||
| 289 | * @param bool $add_login_pattern |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | private function defaultRouteOnly(bool $add_login_pattern): bool |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Отрезает пробелы справа для dialplan. |
||
| 300 | */ |
||
| 301 | private function trimDialPlan(): void |
||
| 302 | { |
||
| 303 | foreach ($this->dialplan as $key => &$dialplan) { |
||
| 304 | if ( ! array_key_exists($key, $this->rout_data_dial)) { |
||
| 305 | continue; |
||
| 306 | } |
||
| 307 | $dialplan = rtrim($dialplan); |
||
| 308 | $dialplan .= $this->rout_data_dial[$key]; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Формирование итогового dialplan. |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function createSummaryDialplan(): string |
||
| 318 | { |
||
| 319 | /** @var IncomingRoutingTable $default_action */ |
||
| 320 | $default_action = IncomingRoutingTable::findFirst('priority = 9999'); |
||
| 321 | |||
| 322 | $uniqId = is_string($this->provider) ? $this->provider : $this->uniqId; |
||
| 323 | $conf = "\n" . "[{$uniqId}-incoming]\n"; |
||
| 324 | foreach ($this->dialplan as $dpln) { |
||
| 325 | $conf .= $dpln . "\n"; |
||
| 326 | if (null === $default_action && 'none' !== $this->provider) { |
||
| 327 | continue; |
||
| 328 | } |
||
| 329 | $conf .= $this->createSummaryDialplanDefAction($default_action, $uniqId); |
||
| 330 | } |
||
| 331 | |||
| 332 | return $conf; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Формирование действия по умолчанию в dialplan. |
||
| 337 | * |
||
| 338 | * @param IncomingRoutingTable $default_action |
||
| 339 | * @param string $uniqId |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | private function createSummaryDialplanDefAction(IncomingRoutingTable $default_action, string $uniqId): string |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Формирование Goto действия для итогового dialplan. |
||
| 359 | * |
||
| 360 | * @param string $conf |
||
| 361 | * @param IncomingRoutingTable $default_action |
||
| 362 | * @param string $uniqId |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | private function createSummaryDialplanGoto( |
||
| 384 | } |
||
| 385 | |||
| 386 | } |