Passed
Push — develop ( 578d35...1edb03 )
by Nikolay
13:40 queued 12s
created

GetRecord::main()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 84
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 84
rs 8.72
cc 2
nc 2
nop 1

How to fix   Long Method   

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\Extensions;
21
22
use MikoPBX\Common\Models\ExtensionForwardingRights;
23
use MikoPBX\Common\Models\Extensions;
24
use MikoPBX\Common\Models\ExternalPhones;
25
use MikoPBX\Common\Models\Sip;
26
use MikoPBX\Common\Models\Users;
27
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
28
use Phalcon\Di;
29
30
/**
31
 *  Class GetRecord
32
 *  Delete an internal number and all its dependencies including mobile and forwarding settings.
33
 *
34
 * @package MikoPBX\PBXCoreREST\Lib\Extensions
35
 */
36
class GetRecord extends \Phalcon\Di\Injectable
37
{
38
39
    /**
40
     * Gets extension data for existing or for the new record
41
     *
42
     * @param string $id ID of the extension to be received.
43
     * @return PBXApiResult Result of the delete operation.
44
     */
45
    public static function main(string $id): PBXApiResult
46
    {
47
        $res = new PBXApiResult();
48
        $res->processor = __METHOD__;
49
        $res->success = true;
50
51
        $parameters = [
52
            'models' => [
53
                'Extensions' => Extensions::class,
54
            ],
55
            'conditions' => 'Extensions.id = :ext_id: AND Extensions.type="' . Extensions::TYPE_SIP.'"',
56
            'bind' => [
57
                'ext_id' => $id
58
            ],
59
            'columns' => [
60
                'id' => 'Extensions.id',
61
                'number' => 'Extensions.number',
62
                'user_id' => 'Users.id',
63
                'user_username' => 'Users.username',
64
                'user_email' => 'Users.email',
65
                'user_avatar'=>  'Users.avatar',
66
                'fwd_ringlength'=>'ExtensionForwardingRights.ringlength',
67
                'fwd_forwardingonbusy'=>'ExtensionForwardingRights.forwardingonbusy',
68
                'fwd_forwardingonunavailable'=>'ExtensionForwardingRights.forwardingonunavailable',
69
                'fwd_forwarding'=>'ExtensionForwardingRights.forwarding',
70
                'mobile_number'=>'ExternalPhones.extension',
71
                'mobile_uniqid'=>'ExternalPhones.uniqid',
72
                'mobile_dialstring'=>'ExternalPhones.dialstring',
73
                'sip_enableRecording' =>'Sip.enableRecording',
74
                'sip_dtmfmode' =>'Sip.dtmfmode',
75
                'sip_manualattributes' => 'Sip.manualattributes',
76
                'sip_uniqid' => 'Sip.uniqid',
77
                'sip_secret' => 'Sip.secret',
78
                'sip_networkfilterid'=>'Sip.networkfilterid',
79
                'sip_transport' => 'Sip.transport'
80
            ],
81
            'joins' => [
82
                'Users' => [
83
                    0 => Users::class,
84
                    1 => 'Users.id = Extensions.userid' ,
85
                    2 => 'Users',
86
                    3 => 'INNER',
87
                ],
88
                'Sip' => [
89
                    0 => Sip::class,
90
                    1 => 'Sip.extension = Extensions.number',
91
                    2 => 'Sip',
92
                    3 => 'LEFT',
93
                ],
94
                'ExtensionsExternal' => [
95
                    0 => Extensions::class,
96
                    1 => 'ExtensionsExternal.userid = Users.id AND ExtensionsExternal.type="' . Extensions::TYPE_EXTERNAL.'"',
97
                    2 => 'ExtensionsExternal',
98
                    3 => 'LEFT',
99
                ],
100
                'ExternalPhones' => [
101
                    0 => ExternalPhones::class,
102
                    1 => 'ExternalPhones.extension = ExtensionsExternal.number',
103
                    2 => 'ExternalPhones',
104
                    3 => 'LEFT',
105
                ],
106
                'ExtensionForwardingRights' => [
107
                    0 => ExtensionForwardingRights::class,
108
                    1 => 'ExtensionForwardingRights.extension = Extensions.number',
109
                    2 => 'ExtensionForwardingRights',
110
                    3 => 'LEFT',
111
                ],
112
113
            ],
114
        ];
115
        $extension = Di::getDefault()->get('modelsManager')->createBuilder($parameters)
116
            ->getQuery()
117
            ->getSingleResult();
118
119
        if (!$extension) {
120
            $dataFromRequest = [];
121
        } else {
122
            $dataFromRequest = $extension->toArray();
123
            $dataFromRequest['sip_manualattributes']=base64_decode($dataFromRequest['sip_manualattributes']);
124
        }
125
126
        $dataStructure = new DataStructure($dataFromRequest);
127
        $res->data = $dataStructure->toArray();
128
        return  $res;
129
    }
130
}