Passed
Push — develop ( d906b2...768883 )
by Nikolay
05:15 queued 16s
created

GetRegistry::main()   C

Complexity

Conditions 12
Paths 22

Size

Total Lines 47
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 47
rs 6.9666
cc 12
nc 22
nop 0

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-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\Sip;
21
22
use MikoPBX\Common\Models\Sip;
23
use MikoPBX\Core\System\Util;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
26
/**
27
 * Retrieves the statuses of SIP providers registration.
28
 *
29
 * @package MikoPBX\PBXCoreREST\Lib\Sip
30
 */
31
class GetRegistry extends \Phalcon\Di\Injectable
32
{
33
    /**
34
     * Retrieves the statuses of SIP 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
        try {
44
            $am = Util::getAstManager('off');
45
            $peers = $am->getPjSipRegistry();
46
            $providers = Sip::find("type = 'friend'");
47
            foreach ($providers as $provider) {
48
                if ($provider->disabled === '1') {
49
                    $peers[] = [
50
                        'state' => 'OFF',
51
                        'id' => $provider->uniqid,
52
                        'username' => $provider->username,
53
                        'host' => $provider->host,
54
                    ];
55
                    continue;
56
                }
57
                if ($provider->registration_type === Sip::REG_TYPE_INBOUND || $provider->registration_type === Sip::REG_TYPE_NONE) {
58
                    $peers_status = $am->getPjSipPeer($provider->uniqid);
59
                    $peers[] = [
60
                        'state' => ($peers_status['state'] === 'OK' && $provider->registration_type === Sip::REG_TYPE_INBOUND) ? 'REGISTERED' : $peers_status['state'],
61
                        'id' => $provider->uniqid,
62
                        'username' => $provider->username,
63
                        'host' => $provider->host,
64
                    ];
65
                    continue;
66
                }
67
                foreach ($peers as &$peer) {
68
                    if (!empty($peer['id'])) {
69
                        continue;
70
                    }
71
                    if ($peer['host'] !== $provider->host || $peer['username'] !== $provider->username) {
72
                        continue;
73
                    }
74
                    $peer['id'] = $provider->uniqid;
75
                }
76
                unset($peer);
77
            }
78
            $res->data = $peers;
79
            $res->success = true;
80
        } catch (\Throwable $e) {
81
            $res->success = false;
82
            $res->messages[] = $e->getMessage();
83
        }
84
        return $res;
85
    }
86
}