Passed
Push — develop ( ea9ef1...75306b )
by Портнов
04:55
created

ActionCelAnswer::execute()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 61
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 61
rs 7.7564
c 1
b 0
f 0
cc 9
nc 8
nop 2

How to fix   Long Method   

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 ActionCelAnswer
29
 * This class is responsible for handling certain events when a call is answered.
30
 *
31
 * @package MikoPBX\Core\Workers\Libs\WorkerCallEvents
32
 */
33
class ActionCelAnswer
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, $data): void
43
    {
44
        $channel = $data['Channel'] ?? '';
45
        $worker->addActiveChan($channel, $data['LinkedID']);
46
47
        // If no channel data, or if the channel is local, or if the channel is already active, return immediately
48
        if (empty($channel) || stripos($channel, 'local') === 0 || $worker->existsActiveChan($channel)) {
49
            return;
50
        }
51
        usleep(100000);  // delay to ensure the channel is properly added
52
53
        // Retrieve the linked identifier for the channel
54
        $linkedId = Util::getAstManager('off')->GetVar($channel, 'CHANNEL(linkedid)', '', false);
55
56
        // If the linkedId matches the data LinkedID, return immediately
57
        if ($linkedId === $data['LinkedID']) {
58
            return;
59
        }
60
61
        // This is a return call after consultative transfer
62
        $filter = [
63
            'linkedid=:linkedid: AND a_transfer = "1"',
64
            'bind' => [
65
                'linkedid' => $linkedId,
66
            ],
67
        ];
68
        /** @var CallDetailRecordsTmp $m_data */
69
        /** @var CallDetailRecordsTmp $row */
70
        $m_data = CallDetailRecordsTmp::find($filter);
71
        foreach ($m_data as $row) {
72
            $row->a_transfer = '0';
73
            $row->save();
74
            if ($worker->existsActiveChan($row->src_chan)) {
75
                $chan = $row->src_chan;
76
                $number = $row->src_num;
77
            } elseif ($worker->existsActiveChan($row->dst_chan)) {
78
                $chan = $row->dst_chan;
79
                $number = $row->dst_num;
80
            } else {
81
                continue;
82
            }
83
84
            // Data for the returned call after transfer
85
            $insert_data['action'] = "ret_after_trasfer";
86
            $insert_data['start'] = date('Y-m-d H:i:s.v', str_replace('mikopbx-', '', $data['LinkedID']));
87
            $insert_data['answer'] = $data['EventTime'];
88
            $insert_data['src_chan'] = $chan;
89
            $insert_data['src_num'] = $number;
90
            $insert_data['dst_chan'] = $channel;
91
            $insert_data['dst_num'] = $data['CallerIDnum'];
92
            $insert_data['linkedid'] = $linkedId;
93
            $insert_data['UNIQUEID'] = $data['UniqueID'] . "_" . Util::generateRandomString();
94
            $insert_data['did'] = $row->did;
95
            $insert_data['transfer'] = '0';
96
97
            if ($worker->enableMonitor($insert_data['src_num'] ?? '', $insert_data['dst_num'] ?? '')) {
98
                $insert_data['recordingfile'] = $worker->MixMonitor($insert_data['dst_chan'], $insert_data['UNIQUEID'], '', '', 'ret_after_trasfer');
99
            }
100
101
            // Insert the new data into the database
102
            InsertDataToDB::execute($insert_data);
103
        }
104
    }
105
106
}