Passed
Push — develop ( d4856e...ea9ef1 )
by Портнов
04:32
created

ActionCelAttendedTransfer::execute()   D

Complexity

Conditions 14
Paths 272

Size

Total Lines 66
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 66
rs 4.5333
cc 14
nc 272
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
     */
42
    public static function execute(WorkerCallEvents $worker, array $data): void
43
    {
44
        $extra = json_decode($data['Extra'], true);
45
        $am = Util::getAstManager('off');
46
        if(stripos($extra['transferee_channel_name'],'local') === 0){
47
            $chanTarget = $am->GetVar($extra['transferee_channel_name'],'ATTENDEDTRANSFER', '' ,false);
48
        }else{
49
            $chanTarget = $extra['transferee_channel_name'];
50
        }
51
        $chanId1 = $worker->getActiveChanId($extra['channel2_name']);
52
        $chanId2 = $worker->getActiveChanId($chanTarget);
53
54
        if($chanId1 !== $chanId2){
55
            $filter = [
56
                'linkedid=:linkedid1: OR linkedid=:linkedid2:',
57
                'bind' => [
58
                    'linkedid1' => $chanId1,
59
                    'linkedid2' => $chanId2,
60
                ],
61
            ];
62
            $n_data['action'] = 'sip_transfer';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$n_data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $n_data = array(); before regardless.
Loading history...
63
            /** @var CallDetailRecordsTmp $row */
64
            $m_data = CallDetailRecordsTmp::find($filter);
65
            foreach ($m_data as $row){
66
                if(empty($row->endtime)){
67
                    if(in_array($row->src_chan, [$extra['channel2_name'], $data['Channel']], true)) {
68
                        $n_data['dst_chan'] = $row->dst_chan;
69
                        $n_data['dst_num']  = $row->dst_num;
70
                        $n_data['did'] = $row->did;
71
                        $worker->StopMixMonitor($n_data['dst_chan']);
72
                    }elseif(in_array($row->dst_chan, [$extra['channel2_name'], $data['Channel']], true)){
73
                        $n_data['src_chan'] = $row->src_chan;
74
                        $n_data['src_num']  = $row->src_num;
75
                        $n_data['did'] = $row->did;
76
                        $worker->StopMixMonitor($n_data['src_chan']);
77
                    }
78
                }
79
            }
80
            $m_data = CallDetailRecordsTmp::find($filter);
81
            foreach ($m_data as $row) {
82
                // Set new linked ID.
83
                if($row->linkedid !== $chanId2){
84
                    $row->writeAttribute('linkedid', $chanId2);
85
                }
86
                if(empty($row->endtime)){
87
                    if(in_array($row->src_chan, [$extra['channel2_name'], $data['Channel']], true)) {
88
                        $row->writeAttribute('endtime', date('Y-m-d H:i:s'));
89
                    }elseif(in_array($row->dst_chan, [$extra['channel2_name'], $data['Channel']], true)){
90
                        $row->writeAttribute('endtime', date('Y-m-d H:i:s'));
91
                    }
92
                }
93
                $row->save();
94
            }
95
            $n_data['start']    = date('Y-m-d H:i:s');
96
            $n_data['answer']   = date('Y-m-d H:i:s');
97
            $n_data['linkedid'] = $chanId2;
98
            $n_data['UNIQUEID'] = $chanId2 . Util::generateRandomString();
99
            $n_data['transfer'] = '0';
100
            if(isset($n_data['dst_chan'], $n_data['src_chan'])){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $n_data seems to never exist and therefore isset should always be false.
Loading history...
101
                if ($worker->enableMonitor($n_data['src_num'] ?? '', $n_data['dst_num'] ?? '')) {
102
                    $n_data['recordingfile'] = $worker->MixMonitor($n_data['dst_chan'], $n_data['UNIQUEID'], '', '', 'hangupChanCheckSipAttTrtansfer');
103
                }
104
                InsertDataToDB::execute($n_data);
105
                // Sending UserEvent
106
                $AgiData = base64_encode(json_encode($n_data));
107
                $am->UserEvent('CdrConnector', ['AgiData' => $AgiData]);
108
            }
109
        }
110
    }
111
}