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

ActionHangupChanMeetme   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C execute() 0 58 14
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 ActionHangupChanMeetme
28
{
29
    /**
30
     * Завершение / уничтожение канала.
31
     * @param $worker
32
     * @param $data
33
     */
34
    public static function execute(WorkerCallEvents $worker, $data):void
35
    {
36
        clearstatcache();
37
        $recordingfile = '';
38
        $dest_chan     = "MeetMe:{$data['conference']}";
39
        // Отбираем все строки по текущей конференции. Не завершенные вызовы.
40
        $filter = [
41
            'dst_chan=:dst_chan: OR linkedid=:linkedid:',
42
            'bind' => [
43
                'linkedid' => $data['linkedid'],
44
                'dst_chan' => $dest_chan,
45
            ],
46
        ];
47
        $m_data = CallDetailRecordsTmp::find($filter);
48
        /** @var CallDetailRecordsTmp $row */
49
        foreach ($m_data as $row) {
50
            if ($dest_chan === $row->dst_chan) {
51
                $is_local        = (stripos($data['src_chan'], 'local/') !== false);
52
                $is_stored_local = (stripos($row->src_chan, 'local/') !== false);
53
                if ($row->UNIQUEID === $data['UNIQUEID'] && $is_local && ! $is_stored_local) {
54
                    $data['src_chan'] = $row->src_chan;
55
                }
56
                if (file_exists($row->recordingfile) || file_exists(
57
                        Util::trimExtensionForFile($row->recordingfile) . '.wav'
58
                    )) {
59
                    // Переопределим путь к файлу записи разговора. Для конферецнии файл один.
60
                    $recordingfile = $row->recordingfile;
61
                }
62
            }
63
            if ($row->linkedid === $data['meetme_id']) {
64
                continue;
65
            }
66
            // Подменим ID на идентификатор конференции.
67
            $row->writeAttribute('linkedid', $data['meetme_id']);
68
            $res = $row->save();
69
            if ( ! $res) {
70
                Util::sysLogMsg('Action_hangup_chan_meetme', implode(' ', $row->getMessages()), LOG_DEBUG);
71
            }
72
        }
73
74
        /** @var CallDetailRecordsTmp $m_data */
75
        /** @var CallDetailRecordsTmp $row */
76
        /** @var CallDetailRecordsTmp $rec_data */
77
        foreach ($m_data as $row) {
78
            if ($row->src_chan !== $data['src_chan']) {
79
                continue;
80
            }
81
            // Заполняем данные для текущего оповещения.
82
            $row->writeAttribute('endtime', $data['end']);
83
            $row->writeAttribute('transfer', 0);
84
            $row->writeAttribute('linkedid', $data['meetme_id']);
85
86
            if ( ! empty($recordingfile)) {
87
                $row->writeAttribute('recordingfile', $recordingfile);
88
            }
89
            $res = $row->save();
90
            if ( ! $res) {
91
                Util::sysLogMsg('Action_hangup_chan_meetme', implode(' ', $row->getMessages()), LOG_DEBUG);
92
            }
93
        }
94
    }
95
96
}