| Total Complexity | 42 |
| Total Lines | 242 |
| Duplicated Lines | 0 % |
| Changes | 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 | |||
| 26 | public const UPDATE_CDR_TUBE = 'update_cdr_tube'; |
||
| 27 | |||
| 28 | |||
| 29 | private $client_queue; |
||
| 30 | private $timeout = 10; |
||
|
|
|||
| 31 | private $internal_numbers = []; |
||
| 32 | private $no_answered_calls = []; |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Entry point |
||
| 37 | * |
||
| 38 | * @param $argv |
||
| 39 | * |
||
| 40 | * @throws \Pheanstalk\Exception\DeadlineSoonException |
||
| 41 | */ |
||
| 42 | public function start($argv): void |
||
| 43 | { |
||
| 44 | $filter = [ |
||
| 45 | '(work_completed<>1 OR work_completed IS NULL) AND endtime IS NOT NULL', |
||
| 46 | 'miko_tmp_db' => true, |
||
| 47 | 'columns' => 'start,answer,src_num,dst_num,dst_chan,endtime,linkedid,recordingfile,dialstatus,UNIQUEID', |
||
| 48 | 'miko_result_in_file' => true, |
||
| 49 | ]; |
||
| 50 | |||
| 51 | |||
| 52 | $this->client_queue = new BeanstalkClient(self::SELECT_CDR_TUBE); |
||
| 53 | $this->client_queue->subscribe($this->makePingTubeName(self::class), [$this, 'pingCallBack']); |
||
| 54 | |||
| 55 | $this->initSettings(); |
||
| 56 | |||
| 57 | while (true) { |
||
| 58 | $result = $this->client_queue->request(json_encode($filter), 10); |
||
| 59 | |||
| 60 | if ($result !== false) { |
||
| 61 | $this->updateCdr(); |
||
| 62 | } |
||
| 63 | $this->client_queue->wait(5); // instead of sleep |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | private function initSettings() |
||
| 68 | { |
||
| 69 | $this->internal_numbers = []; |
||
| 70 | $this->no_answered_calls = []; |
||
| 71 | $users = Users::find(); |
||
| 72 | foreach ($users as $user) { |
||
| 73 | if (empty($user->email)) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | foreach ($user->Extensions as $exten) { |
||
| 78 | $this->internal_numbers[$exten->number] = [ |
||
| 79 | 'email' => $user->email, |
||
| 80 | 'language' => $user->language, |
||
| 81 | ]; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Обработчик результата запроса. |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | private function updateCdr(): void |
||
| 91 | { |
||
| 92 | $this->initSettings(); |
||
| 93 | $result_data = $this->client_queue->getBody(); |
||
| 94 | // Получаем результат. |
||
| 95 | $result = json_decode($result_data, true); |
||
| 96 | if (file_exists($result)) { |
||
| 97 | $file_data = json_decode(file_get_contents($result), true); |
||
| 98 | unlink($result); |
||
| 99 | $result = $file_data; |
||
| 100 | } |
||
| 101 | if ( ! is_array($result) && ! is_object($result)) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | if (count($result) < 1) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | $arr_update_cdr = []; |
||
| 108 | // Получаем идентификаторы активных каналов. |
||
| 109 | $channels_id = $this->getActiveIdChannels(); |
||
| 110 | foreach ($result as $row) { |
||
| 111 | if (array_key_exists($row['linkedid'], $channels_id)) { |
||
| 112 | // Цепочка вызовов еще не завершена. |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | if (trim($row['recordingfile']) !== '') { |
||
| 116 | // Если каналов не существует с ID, то можно удалить временные файлы. |
||
| 117 | $p_info = pathinfo($row['recordingfile']); |
||
| 118 | $fname = $p_info['dirname'] . '/' . $p_info['filename'] . '.wav'; |
||
| 119 | if (file_exists($fname)) { |
||
| 120 | @unlink($fname); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | $start = strtotime($row['start']); |
||
| 124 | $answer = strtotime($row['answer']); |
||
| 125 | $end = strtotime($row['endtime']); |
||
| 126 | $dialstatus = trim($row['dialstatus']); |
||
| 127 | |||
| 128 | $duration = max(($end - $start), 0); |
||
| 129 | $billsec = ($end != 0 && $answer != 0) ? ($end - $answer) : 0; |
||
| 130 | |||
| 131 | $disposition = 'NOANSWER'; |
||
| 132 | if ($billsec > 0) { |
||
| 133 | $disposition = 'ANSWERED'; |
||
| 134 | } elseif ('' !== $dialstatus) { |
||
| 135 | $disposition = ($dialstatus === 'ANSWERED') ? $disposition : $dialstatus; |
||
| 136 | } |
||
| 137 | |||
| 138 | if ($billsec <= 0) { |
||
| 139 | $row['answer'] = ''; |
||
| 140 | $billsec = 0; |
||
| 141 | |||
| 142 | if ( ! empty($row['recordingfile'])) { |
||
| 143 | $p_info = pathinfo($row['recordingfile']); |
||
| 144 | $file_list = [ |
||
| 145 | $p_info['dirname'] . '/' . $p_info['filename'] . '.mp3', |
||
| 146 | $p_info['dirname'] . '/' . $p_info['filename'] . '.wav', |
||
| 147 | $p_info['dirname'] . '/' . $p_info['filename'] . '_in.wav', |
||
| 148 | $p_info['dirname'] . '/' . $p_info['filename'] . '_out.wav', |
||
| 149 | ]; |
||
| 150 | foreach ($file_list as $file) { |
||
| 151 | if ( ! file_exists($file)) { |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | @unlink($file); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($disposition !== 'ANSWERED') { |
||
| 160 | if (file_exists($row['recordingfile'])) { |
||
| 161 | @unlink($row['recordingfile']); |
||
| 162 | } |
||
| 163 | } elseif ( ! file_exists(Util::trimExtensionForFile($row['recordingfile']) . 'wav') && ! file_exists( |
||
| 164 | $row['recordingfile'] |
||
| 165 | )) { |
||
| 166 | /** @var CallDetailRecordsTmp $rec_data */ |
||
| 167 | $rec_data = CallDetailRecordsTmp::findFirst( |
||
| 168 | "linkedid='{$row['linkedid']}' AND dst_chan='{$row['dst_chan']}'" |
||
| 169 | ); |
||
| 170 | if ($rec_data !== null) { |
||
| 171 | $row['recordingfile'] = $rec_data->recordingfile; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | $data = [ |
||
| 176 | 'work_completed' => 1, |
||
| 177 | 'duration' => $duration, |
||
| 178 | 'billsec' => $billsec, |
||
| 179 | 'disposition' => $disposition, |
||
| 180 | 'UNIQUEID' => $row['UNIQUEID'], |
||
| 181 | 'recordingfile' => ($disposition === 'ANSWERED') ? $row['recordingfile'] : '', |
||
| 182 | 'tmp_linked_id' => $row['linkedid'], |
||
| 183 | ]; |
||
| 184 | |||
| 185 | $arr_update_cdr[] = $data; |
||
| 186 | $this->checkNoAnswerCall(array_merge($row, $data)); |
||
| 187 | } |
||
| 188 | |||
| 189 | foreach ($arr_update_cdr as $data) { |
||
| 190 | $linkedid = $data['tmp_linked_id']; |
||
| 191 | $data['GLOBAL_STATUS'] = $data['disposition']; |
||
| 192 | if (isset($this->no_answered_calls[$linkedid]) && |
||
| 193 | isset($this->no_answered_calls[$linkedid]['NOANSWER']) && |
||
| 194 | $this->no_answered_calls[$linkedid]['NOANSWER'] == false) { |
||
| 195 | $data['GLOBAL_STATUS'] = 'ANSWERED'; |
||
| 196 | } |
||
| 197 | unset($data['tmp_linked_id']); |
||
| 198 | $this->client_queue->publish(json_encode($data), null, self::UPDATE_CDR_TUBE); |
||
| 199 | } |
||
| 200 | |||
| 201 | $this->notifyByEmail(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Функция позволяет получить активные каналы. |
||
| 206 | * Возвращает ассоциативный массив. Ключ - Linkedid, значение - массив каналов. |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | private function getActiveIdChannels(): array |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Анализируем не отвеченные вызовы. Наполняем временный массив для дальнейшей обработки. |
||
| 221 | * |
||
| 222 | * @param $row |
||
| 223 | */ |
||
| 224 | private function checkNoAnswerCall($row): void |
||
| 251 | ]; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Постановка задачи в очередь на оповещение по email. |
||
| 256 | */ |
||
| 257 | private function notifyByEmail(): void |
||
| 263 | } |
||
| 264 | |||
| 265 | } |
||
| 279 | } |