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

GetRegistryAction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 42
c 1
b 0
f 0
dl 0
loc 71
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 64 11
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\PBXCoreREST\Lib\Iax;
21
22
use MikoPBX\Common\Models\Iax;
23
use MikoPBX\Core\System\Util;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
26
/**
27
 * Retrieves the statuses of IAX providers registration.
28
 *
29
 * @package MikoPBX\PBXCoreREST\Lib\Iax
30
 */
31
class GetRegistryAction extends \Phalcon\Di\Injectable
32
{
33
    /**
34
     * Retrieves the statuses of IAX providers registration.
35
     *
36
     * @return PBXApiResult An object containing the result of the API call.
37
     */
38
    public static function main(): PBXApiResult
39
    {
40
        $res = new PBXApiResult();
41
        $res->processor = __METHOD__;
42
43
44
        try {
45
            $peers = [];
46
            $providers = Iax::find();
47
            foreach ($providers as $provider) {
48
                $peers[] = [
49
                    'state' => 'OFF',
50
                    'id' => $provider->uniqid,
51
                    'username' => trim($provider->username),
52
                    'host' => trim($provider->host),
53
                    'noregister' => $provider->noregister,
54
                ];
55
            }
56
57
            if (Iax::findFirst("disabled = '0'") !== null) {
58
                // Find them over AMI
59
                $am = Util::getAstManager('off');
60
                $amiRegs = $am->IAXregistry(); // Registrations
61
                $amiPeers = $am->IAXpeerlist(); // Peers
62
                foreach ($amiPeers as $amiPeer) {
63
                    $key = array_search($amiPeer['ObjectName'], array_column($peers, 'id'), true);
64
                    if ($key !== false) {
65
                        $currentPeer = &$peers[$key];
66
                        if ($currentPeer['noregister'] === '1') {
67
                            // Peer without registration.
68
                            $arr_status = explode(' ', $amiPeer['Status']);
69
                            $currentPeer['state'] = strtoupper($arr_status[0]);
70
                            // Check if the expected index exists before trying to access it.
71
                            if (isset($arr_status[1])) {
72
                                $currentPeer['time-response'] = strtoupper(str_replace(['(', ')'], '', $arr_status[1]));
73
                            } else {
74
                                // Handle the case where $arr_status[1] is not set.
75
                                // You might want to assign a default value or handle this scenario appropriately.
76
                                $currentPeer['time-response'] = 'N/A';
77
                            }
78
                        } else {
79
                            $currentPeer['state'] = 'Error register.';
80
                            // Parse active registrations
81
                            foreach ($amiRegs as $reg) {
82
                                if (
83
                                    strcasecmp($reg['Addr'], $currentPeer['host']) === 0
84
                                    && strcasecmp($reg['Username'], $currentPeer['username']) === 0
85
                                ) {
86
                                    $currentPeer['state'] = $reg['State'];
87
                                    break;
88
                                }
89
                            }
90
                        }
91
                    }
92
                }
93
            }
94
95
            $res->data = $peers;
96
            $res->success = true;
97
        } catch (\Throwable $e) {
98
            $res->success = false;
99
            $res->messages[] = $e->getMessage();
100
        }
101
        return $res;
102
    }
103
}