| Total Complexity | 47 |
| Total Lines | 255 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ActionHangupChan 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 ActionHangupChan, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class ActionHangupChan |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Executes the hangup channel action. |
||
| 39 | * |
||
| 40 | * @param WorkerCallEvents $worker The worker instance. |
||
| 41 | * @param array $data The data containing call details. |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | * @throws \Exception |
||
| 45 | */ |
||
| 46 | public static function execute(WorkerCallEvents $worker, array $data): void |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Hangs up the channel for end calls. |
||
| 75 | * |
||
| 76 | * @param WorkerCallEvents $worker The worker instance. |
||
| 77 | * @param array $data The data containing call details. |
||
| 78 | * @param array $transfer_calls The array to store transfer calls. |
||
| 79 | * @param array $channels The array to store channels. |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | private static function hangupChanEndCalls(WorkerCallEvents $worker, array $data, array &$transfer_calls, array &$channels): void |
||
| 84 | { |
||
| 85 | $filter = [ |
||
| 86 | 'linkedid=:linkedid: AND endtime = ""', |
||
| 87 | 'bind' => [ |
||
| 88 | 'linkedid' => $data['linkedid'], |
||
| 89 | ], |
||
| 90 | ]; |
||
| 91 | /** @var CallDetailRecordsTmp $m_data */ |
||
| 92 | /** @var CallDetailRecordsTmp $row */ |
||
| 93 | $m_data = CallDetailRecordsTmp::find($filter); |
||
| 94 | $countRows = 0; |
||
| 95 | $transferCdrAnswered = true; |
||
| 96 | foreach ($m_data as $row) { |
||
| 97 | if ($row->dst_chan !== $data['agi_channel'] && $data['agi_channel'] !== $row->src_chan) { |
||
| 98 | continue; |
||
| 99 | } |
||
| 100 | $countRows++; |
||
| 101 | $vmState = $data['VMSTATUS'] ?? ''; |
||
| 102 | if ($row->dst_num === VoiceMailConf::VOICE_MAIL_EXT && $vmState !== 'FAILED') { |
||
| 103 | // This call will be ended by the voicemail_end event. |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | if ($row->transfer === '1' && !empty($row->dst_chan)) { |
||
| 107 | // Make sure the destination channel is not empty. |
||
| 108 | // Otherwise, it's not a transfer. |
||
| 109 | $transfer_calls[] = $row->toArray(); |
||
| 110 | $transferCdrAnswered = min($transferCdrAnswered, empty($row->answer)); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | foreach ($m_data as $row) { |
||
| 114 | if ($row->dst_chan !== $data['agi_channel'] && $data['agi_channel'] !== $row->src_chan) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | $vmState = $data['VMSTATUS'] ?? ''; |
||
| 118 | if ($row->dst_num === VoiceMailConf::VOICE_MAIL_EXT && $vmState !== 'FAILED') { |
||
| 119 | // This call will be ended by the voicemail_end event. |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | if ($row->dialstatus === 'ORIGINATE') { |
||
| 123 | $row->writeAttribute('dialstatus', ''); |
||
| 124 | if ($row->answer === '') { |
||
| 125 | $newId = $row->linkedid . '_' . $row->src_num . '_' . substr($row->src_chan, strpos($row->src_chan, '-') + 1); |
||
| 126 | $row->writeAttribute('UNIQUEID', $newId); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | $row->writeAttribute('endtime', $data['end']); |
||
| 130 | $row->writeAttribute('transfer', 0); |
||
| 131 | if ($transferCdrAnswered === false && count($transfer_calls) === 2) { |
||
| 132 | // Call termination for consultative transfer. Initiator hung up before the destination answered. |
||
| 133 | $row->writeAttribute('a_transfer', 1); |
||
| 134 | } |
||
| 135 | if ($data['dialstatus'] !== '') { |
||
| 136 | if ($data['dialstatus'] === 'ORIGINATE') { |
||
| 137 | $row->writeAttribute('dst_chan', ''); |
||
| 138 | } |
||
| 139 | $row->writeAttribute('dialstatus', $data['dialstatus']); |
||
| 140 | } |
||
| 141 | $res = $row->update(); |
||
| 142 | if (!$res) { |
||
| 143 | SystemMessages::sysLogMsg('Action_hangup_chan', implode(' ', $row->getMessages()), LOG_DEBUG); |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($row->src_chan !== $data['agi_channel']) { |
||
| 147 | $channels[] = [ |
||
| 148 | 'chan' => $row->src_chan, |
||
| 149 | 'did' => $row->did, |
||
| 150 | 'num' => $row->src_num, |
||
| 151 | 'out' => true, |
||
| 152 | ]; |
||
| 153 | } else { |
||
| 154 | $worker->StopMixMonitor($row->dst_chan, 'hangupChanEndCalls'); |
||
| 155 | $channels[] = [ |
||
| 156 | 'chan' => $row->dst_chan, |
||
| 157 | 'did' => $row->did, |
||
| 158 | 'num' => $row->dst_num, |
||
| 159 | 'out' => false, |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // The SRC channel has been completed and DST channel has not been created |
||
| 165 | $filter = [ |
||
| 166 | 'verbose_call_id=:verbose_call_id: AND endtime = "" AND dst_chan = "" AND src_chan = :src_chan:', |
||
| 167 | 'bind' => [ |
||
| 168 | 'verbose_call_id' => $data['verbose_call_id'], |
||
| 169 | 'src_chan' => $data['agi_channel'], |
||
| 170 | ], |
||
| 171 | ]; |
||
| 172 | $m_data = CallDetailRecordsTmp::find($filter); |
||
| 173 | foreach ($m_data as $row) { |
||
| 174 | $row->writeAttribute('endtime', $row->start); |
||
| 175 | $row->writeAttribute('transfer', 0); |
||
| 176 | $res = $row->update(); |
||
| 177 | if (!$res) { |
||
| 178 | SystemMessages::sysLogMsg('Action_hangup_chan', implode(' ', $row->getMessages()), LOG_DEBUG); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | self::regMissedCall($data, $countRows); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Registers a missed call. |
||
| 187 | * |
||
| 188 | * @param array $data The data containing call details. |
||
| 189 | * @param int $tmpCdrCount The count of temporary CDRs. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | private static function regMissedCall(array $data, int $tmpCdrCount): void |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Checks for SIP transfers when hanging up a channel. |
||
| 226 | * |
||
| 227 | * @param WorkerCallEvents $worker The worker instance. |
||
| 228 | * @param array $data The data containing call details. |
||
| 229 | * @param array $channels The list of channels. |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | * @throws \Exception |
||
| 233 | */ |
||
| 234 | public static function hangupChanCheckSipTrtansfer(WorkerCallEvents $worker, array $data, array $channels): void |
||
| 290 | } |
||
| 291 | } |
||
| 293 |