Passed
Push — develop ( fe499f...dd242c )
by Nikolay
05:43 queued 12s
created

IAXStackProcessor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B getRegistry() 0 52 9
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 7 2020
7
 *
8
 */
9
10
namespace MikoPBX\PBXCoreREST\Lib;
11
12
13
use MikoPBX\Common\Models\Iax;
14
use MikoPBX\Core\System\Util;
15
use Phalcon\Di\Injectable;
16
17
class IAXStackProcessor extends Injectable
18
{
19
    /**
20
     * Получение статусов регистраций IAX
21
     *
22
     * @return PBXApiResult
23
     */
24
    public static function getRegistry(): PBXApiResult
25
    {
26
        $res = new PBXApiResult();
27
        $res->processor = __METHOD__;
28
        $peers  = [];
29
        $providers = Iax::find();
30
        foreach ($providers as $provider) {
31
            $peers[] = [
32
                'state'      => 'OFF',
33
                'id'         => $provider->uniqid,
34
                'username'   => trim($provider->username),
35
                'host'       => trim($provider->host),
36
                'noregister' => $provider->noregister,
37
            ];
38
        }
39
40
        if (Iax::findFirst("disabled = '0'") !== null) {
41
            // Find them over AMI
42
            $am       = Util::getAstManager('off');
43
            $amiRegs  = $am->IAXregistry(); // Registrations
44
            $amiPeers = $am->IAXpeerlist(); // Peers
45
            $am->Logoff();
46
            foreach ($amiPeers as $amiPeer) {
47
                $key = array_search($amiPeer['ObjectName'], array_column($peers, 'id'), true);
48
                if ($key !== false) {
49
                    $currentPeer = &$peers[$key];
50
                    if ($currentPeer['noregister'] === '1') {
51
                        // Пир без регистрации.
52
                        $arr_status                   = explode(' ', $amiPeer['Status']);
53
                        $currentPeer['state']         = strtoupper($arr_status[0]);
54
                        $currentPeer['time-response'] = strtoupper(str_replace(['(', ')'], '', $arr_status[1]));
55
                    } else {
56
                        $currentPeer['state'] = 'Error register.';
57
                        // Parse active registrations
58
                        foreach ($amiRegs as $reg) {
59
                            if (
60
                                strcasecmp($reg['Addr'], $currentPeer['host']) === 0
61
                                && strcasecmp($reg['Username'], $currentPeer['username']) === 0
62
                            ) {
63
                                $currentPeer['state'] = $reg['State'];
64
                                break;
65
                            }
66
                        }
67
                    }
68
                }
69
            }
70
        }
71
72
        $res->data = $peers;
73
        $res->success = true;
74
75
        return $res;
76
    }
77
}