Conditions | 23 |
Paths | 1125 |
Total Lines | 100 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
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 | } |
||
293 |