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

ActionTransferDialCreateChan::execute()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 47
rs 7.6666
cc 10
nc 10
nop 2

How to fix   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-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 ActionTransferDialCreateChan {
28
    /**
29
     * Обработка события создания канала - пары, при начале переадресации звонка.
30
     * @param $worker
31
     * @param $data
32
     */
33
    public static function execute(WorkerCallEvents $worker, $data):void
34
    {
35
        $filter     = [
36
            'UNIQUEID=:UNIQUEID: AND endtime = "" AND answer = ""',
37
            'bind' => [
38
                'UNIQUEID' => $data['transfer_UNIQUEID'],
39
            ],
40
        ];
41
        $row_create = false;
42
        /** @var CallDetailRecordsTmp $m_data */
43
        /** @var CallDetailRecordsTmp $row */
44
        $m_data = CallDetailRecordsTmp::find($filter);
45
        foreach ($m_data as $row) {
46
            ///
47
            // Проверим, если более одного канала SIP/256 при входящем.
48
            if ( ! empty($row->dst_chan) && $data['dst_chan'] !== $row->dst_chan) {
49
                if ($row_create) {
50
                    continue;
51
                }
52
                // Необходимо дублировать строку звонка.
53
                $new_row = new CallDetailRecordsTmp();
54
                $f_list  = $row->toArray();
55
                foreach ($f_list as $attribute => $value) {
56
                    if ($attribute === 'id') {
57
                        continue;
58
                    }
59
                    $new_row->writeAttribute($attribute, $value);
60
                }
61
                $new_row->writeAttribute('dst_chan', $data['dst_chan']);
62
                $new_row->writeAttribute('UNIQUEID', $data['transfer_UNIQUEID'] . '_' . $data['dst_chan']);
63
                // $new_row->save();
64
                // Подмена $row;
65
                $row        = $new_row;
66
                $row_create = true;
67
            }
68
69
            // конец проверки
70
            ///
71
72
            $row->writeAttribute('dst_chan', $data['dst_chan']);
73
            $row->writeAttribute('recordingfile', $data['recordingfile']);
74
            if (isset($data['dst_call_id']) && ! empty($data['dst_call_id'])) {
75
                $row->writeAttribute('dst_call_id', $data['dst_call_id']);
76
            }
77
            $res = $row->save();
78
            if ( ! $res) {
79
                Util::sysLogMsg('Action_transfer_dial_create_chan', implode(' ', $row->getMessages()), LOG_DEBUG);
80
            }
81
        }
82
    }
83
}