Passed
Push — develop ( 368e3d...115c7b )
by Nikolay
12:16
created

GetActiveChannelsAction::main()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 51
rs 6.9666
cc 12
nc 18
nop 0

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-2024 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\PBXCoreREST\Lib\CdrDB;
21
22
use MikoPBX\Core\System\BeanstalkClient;
23
use MikoPBX\Core\System\Util;
24
use MikoPBX\Core\Workers\WorkerCdr;
25
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
26
27
/**
28
 * Get active channels. These are the unfinished calls (endtime IS NULL).
29
 *
30
 * @package MikoPBX\PBXCoreREST\Lib\CdrDB
31
 */
32
class GetActiveChannelsAction extends \Phalcon\Di\Injectable
33
{
34
    /**
35
     * Get active channels. These are the unfinished calls (endtime IS NULL).
36
     *
37
     * @return PBXApiResult An object containing the result of the API call.
38
     */
39
    public static function main(): PBXApiResult
40
    {
41
        $res = new PBXApiResult();
42
        $res->processor = __METHOD__;
43
44
        try {
45
            $res->success = true;
46
47
            $filter = [
48
                'endtime=""',
49
                'order' => 'id',
50
                'columns' => 'start,answer,src_chan,dst_chan,src_num,dst_num,did,linkedid',
51
                'miko_tmp_db' => true,
52
                'miko_result_in_file' => true,
53
            ];
54
            $client = new BeanstalkClient(WorkerCdr::SELECT_CDR_TUBE);
55
            list($result, $message) = $client->sendRequest(json_encode($filter), 2);
56
            if ($result === false) {
57
                $res->data = [];
58
            } else {
59
                $am = Util::getAstManager('off');
60
                $active_chans = $am->GetChannels(true);
61
                $result_data = [];
62
63
                $result = json_decode($message);
64
                if (file_exists($result)) {
65
                    $data = json_decode(file_get_contents($result), true);
66
                    unlink($result);
67
                    foreach ($data as $row) {
68
                        if (!isset($active_chans[$row['linkedid']])) {
69
                            // The call no longer exists.
70
                            continue;
71
                        }
72
                        if (empty($row['dst_chan']) && empty($row['src_chan'])) {
73
                            // This is an erroneous situation. Ignore such a call.
74
                            continue;
75
                        }
76
                        $channels = $active_chans[$row['linkedid']];
77
                        if ((empty($row['src_chan']) || in_array($row['src_chan'], $channels))
78
                            && (empty($row['dst_chan']) || in_array($row['dst_chan'], $channels))) {
79
                            $result_data[] = $row;
80
                        }
81
                    }
82
                }
83
                $res->data = $result_data;
84
            }
85
        } catch (\Throwable $e) {
86
            $res->success = false;
87
            $res->messages[] = $e->getMessage();
88
        }
89
        return $res;
90
    }
91
}