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

ExtensionsController::saveAction()   F

Complexity

Conditions 18
Paths 271

Size

Total Lines 121
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 121
rs 3.1458
c 0
b 0
f 0
cc 18
nc 271
nop 0

How to fix   Long Method    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\AdminCabinet\Controllers;
21
22
use MikoPBX\AdminCabinet\Forms\ExtensionEditForm;
23
use MikoPBX\Common\Models\{
24
    Extensions,
25
    Sip,
26
    Users
27
};
28
29
use MikoPBX\Common\Providers\PBXCoreRESTClientProvider;
30
use function MikoPBX\Common\Config\appPath;
31
32
class ExtensionsController extends BaseController
33
{
34
35
    /**
36
     * Build the list of internal numbers and employees.
37
     */
38
    public function indexAction(): void
39
    {
40
        $extensionTable = []; // Initialize an empty array to store extension data
41
42
        $parameters = [
43
            'models' => [
44
                'Extensions' => Extensions::class,
45
            ],
46
            'conditions' => 'Extensions.is_general_user_number = "1"',
47
            'columns' => [
48
                'id' => 'Extensions.id',
49
                'username' => 'Users.username',
50
                'number' => 'Extensions.number',
51
                'userid' => 'Extensions.userid',
52
                'disabled' => 'Sip.disabled',
53
                'secret' => 'Sip.secret',
54
                'email' => 'Users.email',
55
                'type' => 'Extensions.type',
56
                'avatar' => 'Users.avatar',
57
58
            ],
59
            'order' => 'number',
60
            'joins' => [
61
                'Sip' => [
62
                    0 => Sip::class,
63
                    1 => 'Sip.extension=Extensions.number',
64
                    2 => 'Sip',
65
                    3 => 'LEFT',
66
                ],
67
                'Users' => [
68
                    0 => Users::class,
69
                    1 => 'Users.id = Extensions.userid',
70
                    2 => 'Users',
71
                    3 => 'INNER',
72
                ],
73
            ],
74
        ];
75
        $query = $this->di->get('modelsManager')->createBuilder($parameters)->getQuery();
76
        $extensions = $query->execute(); // Execute the query and retrieve the extensions data
77
78
        foreach ($extensions as $extension) {
79
            switch ($extension->type) {
80
                case Extensions::TYPE_SIP:
81
                    // Process SIP extensions
82
                    $extensionTable[$extension->userid]['userid'] = $extension->userid;
83
                    $extensionTable[$extension->userid]['number'] = $extension->number;
84
                    $extensionTable[$extension->userid]['status'] = ($extension->disabled === '1') ? 'disabled' : '';
85
                    $extensionTable[$extension->userid]['id'] = $extension->id;
86
                    $extensionTable[$extension->userid]['username'] = $extension->username;
87
                    $extensionTable[$extension->userid]['email'] = $extension->email;
88
                    $extensionTable[$extension->userid]['secret'] = $extension->secret;
89
90
                    if (!array_key_exists('mobile', $extensionTable[$extension->userid])) {
91
                        $extensionTable[$extension->userid]['mobile'] = '';
92
                    }
93
                    if ($extension->avatar) {
94
                        $filename = md5($extension->avatar);
95
                        $imgCacheDir = appPath('sites/admin-cabinet/assets/img/cache');
96
                        $imgFile = "{$imgCacheDir}/$filename.jpg";
97
                        if (!file_exists($imgFile)) {
98
                            $this->base64ToJpeg($extension->avatar, $imgFile);
99
                        }
100
101
                        $extensionTable[$extension->userid]['avatar'] = "{$this->url->get()}assets/img/cache/{$filename}.jpg";
102
                    } else {
103
                        $extensionTable[$extension->userid]['avatar'] = "{$this->url->get()}assets/img/unknownPerson.jpg";
104
                    }
105
106
                    break;
107
                case Extensions::TYPE_EXTERNAL:
108
                    // Process external extensions
109
                    $extensionTable[$extension->userid]['mobile'] = $extension->number;
110
                    break;
111
                default:
112
                    // Handle other extension types
113
            }
114
        }
115
        $this->view->extensions = $extensionTable; // Pass the extension data to the view
116
    }
117
118
    /**
119
     * Modify extension settings.
120
     *
121
     * @param string $id The ID of the extension being modified.
122
     *
123
     * @return void
124
     */
125
    public function modifyAction(string $id = ''): void
126
    {
127
        $restAnswer = $this->di->get(PBXCoreRESTClientProvider::SERVICE_NAME, [
128
            '/pbxcore/api/extensions/getRecord',
129
            PBXCoreRESTClientProvider::HTTP_METHOD_GET,
130
            ['id' => $id]
131
        ]);
132
        if ($restAnswer->success) {
133
            $getRecordStructure = (object)$restAnswer->data;
134
        } else {
135
            $this->flash->error(implode (', ', $restAnswer->messages));
136
            $this->dispatcher->forward([
137
                'controller' => 'extensions',
138
                'action' => 'index'
139
            ]);
140
            return;
141
        }
142
143
        // Create the form for editing the extension
144
        $form = new ExtensionEditForm($getRecordStructure);
145
146
        // Pass the form and extension details to the view
147
        $this->view->form = $form;
148
        $extension = Extensions::findFirstById($getRecordStructure->id)?? new Extensions();
149
        $this->view->represent = $extension->getRepresent();
150
        $this->view->avatar = $getRecordStructure->user_avatar;
151
    }
152
153
}