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

SIPStackProcessor::getPeerStatus()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
rs 9.9
cc 2
nc 3
nop 1
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;
21
22
23
use MikoPBX\PBXCoreREST\Lib\Sip\GetPeersStatuses;
24
use MikoPBX\PBXCoreREST\Lib\Sip\GetPeerStatus;
25
use MikoPBX\PBXCoreREST\Lib\Sip\GetRegistry;
26
use MikoPBX\PBXCoreREST\Lib\Sip\GetSipSecret;
27
use Phalcon\Di\Injectable;
28
29
/**
30
 * Class SIPStackProcessor
31
 *
32
 * @package MikoPBX\PBXCoreREST\Lib
33
 *
34
 */
35
class SIPStackProcessor extends Injectable
36
{
37
    /**
38
     * Processes SIP requests
39
     *
40
     * @param array $request The request data
41
     *   - action: The action to be performed
42
     *   - data: Additional data related to the action
43
     *
44
     * @return PBXApiResult An object containing the result of the API call.
45
     */
46
    public static function callBack(array $request): PBXApiResult
47
    {
48
        $action = $request['action'];
49
        $data = $request['data'];
50
        $res = new PBXApiResult();
51
        $res->processor = __METHOD__;
52
        switch ($action) {
53
            case 'getPeersStatuses':
54
                $res = GetPeersStatuses::main();
55
                break;
56
            case 'getSipPeer':
57
                if (!empty($data['peer'])) {
58
                    $res = GetPeerStatus::main($data['peer']);
59
                } else {
60
                    $res->messages['error'][] = 'Empty peer value in POST/GET data';
61
                }
62
                break;
63
            case 'getRegistry':
64
                $res = GetRegistry::main();
65
                break;
66
            case 'getSecret':
67
                if (!empty($data['number'])) {
68
                    $res = GetSipSecret::main($data['number']);
69
                } else {
70
                    $res->messages['error'][] = 'Empty number value in POST/GET data';
71
                }
72
                break;
73
            default:
74
                $res->messages['error'][] = "Unknown action - $action in " . __CLASS__;
75
                break;
76
        }
77
78
        $res->function = $action;
79
80
        return $res;
81
    }
82
83
}