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

Utils::getSipSecret()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 2
nc 2
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\Extensions;
21
22
use MikoPBX\Common\Models\Extensions;
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
use Phalcon\Di\Injectable;
26
27
/**
28
 * Class Utils
29
 * Provides utility functions for handling phone numbers and availability checks.
30
 *
31
 * @package MikoPBX\PBXCoreREST\Lib\Extensions
32
 */
33
class Utils extends Injectable
34
{
35
36
    /**
37
     * Retrieves the view for the phone numbers list via AJAX request.
38
     *
39
     * @param array $numbers List of phone numbers.
40
     * @return PBXApiResult Result of the operation.
41
     */
42
    public static function getPhonesRepresent(array $numbers): PBXApiResult
43
    {
44
        $res = new PBXApiResult();
45
        $res->processor = __METHOD__;
46
        $res->success = true;
47
48
        $result = [];
49
        foreach ($numbers as $number) {
50
            $result[$number] = [
51
                'number' => $number,
52
                'represent' => self::getPhoneRepresent($number)->data,
53
            ];
54
        }
55
        $res->data = $result;
56
57
        return $res;
58
    }
59
60
    /**
61
     * Retrieves the view for the phone number via AJAX request.
62
     *
63
     * @param string $phoneNumber The phone number.
64
     * @return PBXApiResult Result of the operation.
65
     */
66
    public static function getPhoneRepresent(string $phoneNumber): PBXApiResult
67
    {
68
        $res = new PBXApiResult();
69
        $res->processor = __METHOD__;
70
        $res->success = true;
71
72
        $response = $phoneNumber;
73
74
        if (strlen($phoneNumber) > 10) {
75
            $seekNumber = substr($phoneNumber, -9);
76
            $parameters = [
77
                'conditions' => 'number LIKE :SearchPhrase1:',
78
                'bind' => [
79
                    'SearchPhrase1' => "%{$seekNumber}",
80
                ],
81
            ];
82
        } else {
83
            $parameters = [
84
                'conditions' => 'number = :SearchPhrase1:',
85
                'bind' => [
86
                    'SearchPhrase1' => $phoneNumber,
87
                ],
88
            ];
89
        }
90
        $result = Extensions::findFirst($parameters);
91
        if ($result !== null) {
92
            $response = $result->getRepresent();
93
        }
94
        $res->data[] = $response;
95
96
        return $res;
97
    }
98
99
    /**
100
     * Check the availability of a number in the extensionsAPI.js JavaScript script.
101
     *
102
     * @param string $number The internal number of the user.
103
     * @return PBXApiResult Result of the availability check.
104
     */
105
    public static function ifNumberAvailable(string $number): PBXApiResult
106
    {
107
        $res = new PBXApiResult();
108
        $res->processor = __METHOD__;
109
        $res->success = true;
110
        $res->data['available'] = true;
111
112
        // Check for overlap with internal number plan
113
        $extension = Extensions::findFirstByNumber($number);
114
        if ($extension !== null) {
115
            $res->data['available'] = false;
116
            switch ($extension->type){
117
                case Extensions::TYPE_SIP:
118
                case Extensions::TYPE_EXTERNAL:
119
                    $res->data['userId'] = $extension->userid;
120
                    $res->data['represent'] = $extension->getRepresent();
121
                    break;
122
                case Extensions::TYPE_PARKING:
123
                    $res->data['represent'] = 'ex_ThisNumberOverlapWithParkingSlots';
124
                    break;
125
                default:
126
                    $res->data['available'] = false;
127
                    $res->data['represent'] = 'ex_ThisNumberIsNotFree';
128
            }
129
130
            return $res;
131
        }
132
        return $res;
133
    }
134
135
}