Passed
Push — develop ( d5179e...bede24 )
by Портнов
05:56 queued 11s
created

ActionTransferDialHangup   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 53
c 1
b 0
f 0
dl 0
loc 109
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B fillNotAnsweredCdr() 0 38 6
A fillLocalChannelCdr() 0 39 5
A execute() 0 8 3
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2021 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
class ActionTransferDialHangup
28
{
29
    /**
30
     * Завершение канала при прееадресации.
31
     * @param $worker
32
     * @param $data
33
     */
34
    public static function execute(WorkerCallEvents $worker, $data):void
35
    {
36
        $pos = stripos($data['agi_channel'], 'local/');
37
        if ($pos === false) {
38
            // Обработка локального канала.
39
            self::fillLocalChannelCdr($worker, $data);
40
        } elseif ('' === $data['ANSWEREDTIME']) {
41
            self::fillNotAnsweredCdr($worker, $data);
42
        }
43
    }
44
45
    /**
46
     * Заполнение не отвеченного звонка.
47
     * @param $worker
48
     * @param $data
49
     */
50
    private static function fillNotAnsweredCdr($worker, $data):void{
51
        $filter = [
52
            'linkedid=:linkedid: AND endtime = "" AND (src_chan=:src_chan: OR dst_chan=:dst_chan:)',
53
            'bind' => [
54
                'linkedid' => $data['linkedid'],
55
                'src_chan' => $data['TRANSFERERNAME'],
56
                'dst_chan' => $data['dst_chan'],
57
            ],
58
        ];
59
        /** @var CallDetailRecordsTmp $m_data */
60
        /** @var CallDetailRecordsTmp $row */
61
        $m_data = CallDetailRecordsTmp::find($filter);
62
        foreach ($m_data as $row) {
63
            // Ответа не было. Переадресация отменена.
64
            $row->writeAttribute('endtime', $data['end']);
65
            $row->writeAttribute('transfer', 0);
66
            if ( ! $row->save()) {
67
                Util::sysLogMsg('Action_transfer_dial_answer', implode(' ', $row->getMessages()), LOG_DEBUG);
68
            }
69
        }
70
71
        // Попробуем возобновить запись разговора.
72
        $filter = [
73
            'linkedid=:linkedid: AND endtime = ""',
74
            'bind' => [
75
                'linkedid' => $data['linkedid'],
76
            ],
77
        ];
78
        $m_data = CallDetailRecordsTmp::find($filter);
79
        foreach ($m_data as $row) {
80
            $info      = pathinfo($row->recordingfile);
81
            $data_time = ($row->answer === '') ? $row->start : $row->answer;
82
            $subdir    = date('Y/m/d/H/', strtotime($data_time));
83
            $worker->MixMonitor($row->dst_chan, $info['filename'], $subdir);
84
            // Снимем со строк признак переадресации.
85
            $row->writeAttribute('transfer', 0);
86
            if ( ! $row->save()) {
87
                Util::sysLogMsg('Action_transfer_dial_answer', implode(' ', $row->getMessages()), LOG_DEBUG);
88
            }
89
        }
90
    }
91
92
    /**
93
     * Обработка Local канала.
94
     * @param $worker
95
     * @param $data
96
     */
97
    private static function fillLocalChannelCdr($worker, $data):void{
98
99
        // Это НЕ локальный канал.
100
        // Если это завершение переадресации (консультативной). Создадим новую строку CDR.
101
        CreateRowTransfer::execute($worker, 'transfer_dial_hangup', $data);
102
103
        // Найдем записанные ранее строки.
104
        $filter = [
105
            'linkedid=:linkedid: AND endtime = "" AND (src_chan=:chan: OR dst_chan=:chan:)',
106
            'bind' => [
107
                'linkedid' => $data['linkedid'],
108
                'chan'     => $data['agi_channel'],
109
            ],
110
        ];
111
        /** @var CallDetailRecordsTmp $m_data */
112
        /** @var CallDetailRecordsTmp $row */
113
        $m_data = CallDetailRecordsTmp::find($filter);
114
        foreach ($m_data as $row) {
115
            // Завершим вызов в CDR.
116
            $row->writeAttribute('endtime', $data['end']);
117
            $row->writeAttribute('transfer', 0);
118
            if ( ! $row->save()) {
119
                Util::sysLogMsg('Action_transfer_dial_answer', implode(' ', $row->getMessages()), LOG_DEBUG);
120
            }
121
        }
122
        // Попробуем возобновить запись разговора.
123
        $filter = [
124
            'linkedid=:linkedid: AND endtime = "" AND transfer=1',
125
            'bind' => [
126
                'linkedid' => $data['linkedid'],
127
            ],
128
        ];
129
        /** @var CallDetailRecordsTmp $res */
130
        $res = CallDetailRecordsTmp::findFirst($filter);
131
        if ($res !== null) {
132
            $info      = pathinfo($res->recordingfile);
0 ignored issues
show
Bug introduced by
It seems like $res->recordingfile can also be of type null; however, parameter $path of pathinfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
            $info      = pathinfo(/** @scrutinizer ignore-type */ $res->recordingfile);
Loading history...
133
            $data_time = (empty($res->answer)) ? $res->start : $res->answer;
134
            $subdir    = date('Y/m/d/H/', strtotime($data_time));
0 ignored issues
show
Bug introduced by
It seems like $data_time can also be of type null; however, parameter $datetime of strtotime() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
            $subdir    = date('Y/m/d/H/', strtotime(/** @scrutinizer ignore-type */ $data_time));
Loading history...
135
            $worker->MixMonitor($res->dst_chan, $info['filename'], $subdir);
136
        }
137
    }
138
139
}