| Total Complexity | 48 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WorkerCdr 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 WorkerCdr, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class WorkerCdr extends WorkerBase |
||
| 22 | { |
||
| 23 | |||
| 24 | public const SELECT_CDR_TUBE = 'select_cdr_tube'; |
||
| 25 | public const UPDATE_CDR_TUBE = 'update_cdr_tube'; |
||
| 26 | protected int $maxProc=1; |
||
| 27 | |||
| 28 | |||
| 29 | private BeanstalkClient $client_queue; |
||
| 30 | private $internal_numbers = []; |
||
| 31 | private $no_answered_calls = []; |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Entry point |
||
| 36 | * |
||
| 37 | * @param $argv |
||
| 38 | * |
||
| 39 | */ |
||
| 40 | public function start($argv): void |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | private function initSettings() |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Обработчик результата запроса. |
||
| 86 | * |
||
| 87 | */ |
||
| 88 | private function updateCdr(): void |
||
| 89 | { |
||
| 90 | $this->initSettings(); |
||
| 91 | $result = $this->getCheckResult(); |
||
| 92 | if (count($result) < 1) { |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | $arr_update_cdr = []; |
||
| 96 | // Получаем идентификаторы активных каналов. |
||
| 97 | $channels_id = $this->getActiveIdChannels(); |
||
| 98 | foreach ($result as $row) { |
||
| 99 | if (array_key_exists($row['linkedid'], $channels_id)) { |
||
| 100 | // Цепочка вызовов еще не завершена. |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | |||
| 104 | $start = strtotime($row['start']); |
||
| 105 | $answer = strtotime($row['answer']); |
||
| 106 | $end = strtotime($row['endtime']); |
||
| 107 | $dialstatus = trim($row['dialstatus']); |
||
| 108 | |||
| 109 | $duration = max(($end - $start), 0); |
||
| 110 | $billsec = ($end !== 0 && $answer !== 0) ? ($end - $answer) : 0; |
||
| 111 | |||
| 112 | [$disposition, $row] = $this->setDisposition($billsec, $dialstatus, $row); |
||
| 113 | [$row, $billsec] = $this->checkBillsecMakeRecFile($billsec, $row); |
||
| 114 | |||
| 115 | $data = [ |
||
| 116 | 'work_completed' => 1, |
||
| 117 | 'duration' => $duration, |
||
| 118 | 'billsec' => $billsec, |
||
| 119 | 'disposition' => $disposition, |
||
| 120 | 'UNIQUEID' => $row['UNIQUEID'], |
||
| 121 | 'recordingfile' => ($disposition === 'ANSWERED') ? $row['recordingfile'] : '', |
||
| 122 | 'tmp_linked_id' => $row['linkedid'], |
||
| 123 | ]; |
||
| 124 | |||
| 125 | $arr_update_cdr[] = $data; |
||
| 126 | $this->checkNoAnswerCall(array_merge($row, $data)); |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->setStatusAndPublish($arr_update_cdr); |
||
| 130 | $this->notifyByEmail(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Функция позволяет получить активные каналы. |
||
| 135 | * Возвращает ассоциативный массив. Ключ - Linkedid, значение - массив каналов. |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | private function getActiveIdChannels(): array |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Анализируем не отвеченные вызовы. Наполняем временный массив для дальнейшей обработки. |
||
| 147 | * |
||
| 148 | * @param $row |
||
| 149 | */ |
||
| 150 | private function checkNoAnswerCall($row): void |
||
| 151 | { |
||
| 152 | if ($row['disposition'] === 'ANSWERED') { |
||
| 153 | $this->no_answered_calls[$row['linkedid']]['NOANSWER'] = false; |
||
| 154 | return; |
||
| 155 | } |
||
| 156 | if ( ! array_key_exists($row['dst_num'], $this->internal_numbers)) { |
||
| 157 | // dst_num - не является номером сотрудника. Это исходящий. |
||
| 158 | return; |
||
| 159 | } |
||
| 160 | $is_internal = false; |
||
| 161 | if ((array_key_exists($row['src_num'], $this->internal_numbers))) { |
||
| 162 | // Это внутренний вызов. |
||
| 163 | $is_internal = true; |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->no_answered_calls[$row['linkedid']][] = [ |
||
| 167 | 'from_number' => $row['src_num'], |
||
| 168 | 'to_number' => $row['dst_num'], |
||
| 169 | 'start' => $row['start'], |
||
| 170 | 'answer' => $row['answer'], |
||
| 171 | 'endtime' => $row['endtime'], |
||
| 172 | 'email' => $this->internal_numbers[$row['dst_num']]['email'], |
||
| 173 | 'language' => $this->internal_numbers[$row['dst_num']]['language'], |
||
| 174 | 'is_internal' => $is_internal, |
||
| 175 | 'duration' => $row['duration'], |
||
| 176 | ]; |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Постановка задачи в очередь на оповещение по email. |
||
| 182 | */ |
||
| 183 | private function notifyByEmail(): void |
||
| 184 | { |
||
| 185 | foreach ($this->no_answered_calls as $call) { |
||
| 186 | $this->client_queue->publish(json_encode($call), WorkerNotifyByEmail::class); |
||
| 187 | } |
||
| 188 | $this->no_answered_calls = []; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param array $arr_update_cdr |
||
| 193 | */ |
||
| 194 | private function setStatusAndPublish(array $arr_update_cdr): void{ |
||
| 195 | foreach ($arr_update_cdr as $data) { |
||
| 196 | $linkedid = $data['tmp_linked_id']; |
||
| 197 | $data['GLOBAL_STATUS'] = $data['disposition']; |
||
| 198 | if (isset($this->no_answered_calls[$linkedid]['NOANSWER']) && $this->no_answered_calls[$linkedid]['NOANSWER'] === false) { |
||
| 199 | $data['GLOBAL_STATUS'] = 'ANSWERED'; |
||
| 200 | // Это отвеченный вызов (на очередь). Удаляем из списка. |
||
| 201 | unset($this->no_answered_calls[$linkedid]); |
||
| 202 | } |
||
| 203 | unset($data['tmp_linked_id']); |
||
| 204 | $this->client_queue->publish(json_encode($data), self::UPDATE_CDR_TUBE); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param int $billsec |
||
| 210 | * @param $row |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | private function checkBillsecMakeRecFile(int $billsec, $row): array{ |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | */ |
||
| 244 | private function getCheckResult(){ |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param int $billsec |
||
| 263 | * @param string $dialstatus |
||
| 264 | * @param $row |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | private function setDisposition(int $billsec, string $dialstatus, $row): array{ |
||
| 303 | } |