| Conditions | 28 |
| Paths | 8656 |
| Total Lines | 112 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 279 | } |