ActionCelAttendedTransfer::execute()   F
last analyzed

Complexity

Conditions 16
Paths 274

Size

Total Lines 70
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 70
rs 3.8083
c 1
b 0
f 0
cc 16
nc 274
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\Workers\Libs\WorkerCallEvents;
21
22
23
use MikoPBX\Common\Models\CallDetailRecordsTmp;
24
use MikoPBX\Core\System\Util;
25
use MikoPBX\Core\Workers\WorkerCallEvents;
26
27
/**
28
 * Class ActionCelAttendedTransfer
29
 * This class is responsible for handling certain events when a call is transfer.
30
 *
31
 * @package MikoPBX\Core\Workers\Libs\WorkerCallEvents
32
 */
33
class ActionCelAttendedTransfer
34
{
35
    /**
36
     * Executes the action when a Call Event Log (CEL) answer event occurs.
37
     *
38
     * @param WorkerCallEvents $worker Instance of WorkerCallEvents.
39
     * @param array $data Data related to the event.
40
     * @return void
41
     * @throws \Exception
42
     */
43
    public static function execute(WorkerCallEvents $worker, array $data): void
44
    {
45
        $extra = json_decode($data['Extra'], true);
46
        $am = Util::getAstManager('off');
47
        if(stripos($extra['transferee_channel_name'],'local') === 0){
48
            $chanTarget = $am->GetVar($extra['transferee_channel_name'],'ATTENDEDTRANSFER', '' ,false);
49
        }else{
50
            $chanTarget = $extra['transferee_channel_name'];
51
        }
52
        $chanId1 = $worker->getActiveChanId($extra['channel2_name']);
53
        $chanId2 = $worker->getActiveChanId($chanTarget);
54
        if(empty($chanId1) || empty($chanId2)){
55
            return;
56
        }
57
58
        if($chanId1 !== $chanId2){
59
            $filter = [
60
                'linkedid=:linkedid1: OR linkedid=:linkedid2:',
61
                'bind' => [
62
                    'linkedid1' => $chanId1,
63
                    'linkedid2' => $chanId2,
64
                ],
65
            ];
66
            $n_data=[];
67
            $n_data['action'] = 'sip_transfer';
68
            /** @var CallDetailRecordsTmp $row */
69
            $m_data = CallDetailRecordsTmp::find($filter);
70
            foreach ($m_data as $row){
71
                if(empty($row->endtime)){
72
                    if(in_array($row->src_chan, [$extra['channel2_name'], $data['Channel']], true)) {
73
                        $n_data['dst_chan'] = $row->dst_chan;
74
                        $n_data['dst_num']  = $row->dst_num;
75
                        $n_data['did'] = $row->did;
76
                        $worker->StopMixMonitor($n_data['dst_chan']);
77
                    }elseif(in_array($row->dst_chan, [$extra['channel2_name'], $data['Channel']], true)){
78
                        $n_data['src_chan'] = $row->src_chan;
79
                        $n_data['src_num']  = $row->src_num;
80
                        $n_data['did'] = $row->did;
81
                        $worker->StopMixMonitor($n_data['src_chan']);
82
                    }
83
                }
84
            }
85
            $m_data = CallDetailRecordsTmp::find($filter);
86
            foreach ($m_data as $row) {
87
                // Set new linked ID.
88
                if($row->linkedid !== $chanId2){
89
                    $row->writeAttribute('linkedid', $chanId2);
90
                }
91
                if(empty($row->endtime)){
92
                    if(in_array($row->src_chan, [$extra['channel2_name'], $data['Channel']], true)) {
93
                        $row->writeAttribute('endtime', date('Y-m-d H:i:s'));
94
                    }elseif(in_array($row->dst_chan, [$extra['channel2_name'], $data['Channel']], true)){
95
                        $row->writeAttribute('endtime', date('Y-m-d H:i:s'));
96
                    }
97
                }
98
                $row->save();
99
            }
100
            $n_data['start']    = date('Y-m-d H:i:s');
101
            $n_data['answer']   = date('Y-m-d H:i:s');
102
            $n_data['linkedid'] = $chanId2;
103
            $n_data['UNIQUEID'] = $chanId2 . Util::generateRandomString();
104
            $n_data['transfer'] = '0';
105
            if(isset($n_data['dst_chan'], $n_data['src_chan'])){
106
                if ($worker->enableMonitor($n_data['src_num'] ?? '', $n_data['dst_num'] ?? '')) {
107
                    $n_data['recordingfile'] = $worker->MixMonitor($n_data['dst_chan'], $n_data['UNIQUEID'], '', '', 'hangupChanCheckSipAttTrtansfer');
108
                }
109
                InsertDataToDB::execute($n_data);
110
                // Sending UserEvent
111
                $AgiData = base64_encode(json_encode($n_data));
112
                $am->UserEvent('CdrConnector', ['AgiData' => $AgiData]);
113
            }
114
        }
115
    }
116
}