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

ExtensionsManagementProcessor::callBack()   C

Complexity

Conditions 16
Paths 15

Size

Total Lines 61
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 61
rs 5.5666
cc 16
nc 15
nop 1

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\PBXCoreREST\Lib;
21
22
use Facebook\WebDriver\Exception\ElementNotSelectableException;
23
use MikoPBX\PBXCoreREST\Lib\Extensions\DeleteRecord;
24
use MikoPBX\PBXCoreREST\Lib\Extensions\Dropdowns;
25
use MikoPBX\PBXCoreREST\Lib\Extensions\GetRecord;
26
use MikoPBX\PBXCoreREST\Lib\Extensions\SaveRecord;
27
use MikoPBX\PBXCoreREST\Lib\Extensions\Utils;
28
use Phalcon\Di\Injectable;
29
30
/**
31
 * Class ExtensionManagementProcessor
32
 *
33
 * @package MikoPBX\PBXCoreREST\Lib
34
 *
35
 */
36
class ExtensionsManagementProcessor extends Injectable
37
{
38
    /**
39
     * Processes Extensions management requests
40
     *
41
     * @param array $request
42
     *
43
     * @return PBXApiResult An object containing the result of the API call.
44
     */
45
    public static function callBack(array $request): PBXApiResult
46
    {
47
        $res = new PBXApiResult();
48
        $res->processor = __METHOD__;
49
50
        $action = $request['action'];
51
        $data = $request['data'];
52
        switch ($action) {
53
            case 'getRecord':
54
                $res = GetRecord::main($data['id'] ?? '');
55
                break;
56
            case 'saveRecord':
57
                // Start xdebug session, don't forget to install xdebug.remote_mode = jit on xdebug.ini
58
                // and set XDEBUG_SESSION Cookie header on REST request to debug it
59
                if (isset($request['debug']) && $request['debug'] === true) {
60
                    xdebug_break();
61
                }
62
                if (!empty($data['number'])) {
63
                    $res = SaveRecord::main($data);
64
                } else {
65
                    $res->messages['error'][] = 'Empty number value in POST/GET data';
66
                }
67
                break;
68
            case 'deleteRecord':
69
                if (!empty($data['id'])) {
70
                    $res = DeleteRecord::main($data['id']);
71
                } else {
72
                    $res->messages['error'][] = 'empty ID in POST/GET data';
73
                }
74
                break;
75
            case 'getForSelect':
76
                $res = Dropdowns::getForSelect($data['type'] ?? 'all');
77
                break;
78
            case 'available':
79
                if (!empty($data['number'])) {
80
                    $res = Utils::ifNumberAvailable($data['number']);
81
                } else {
82
                    $res->messages['error'][] = 'Empty number value in POST/GET data';
83
                }
84
                break;
85
            case 'getPhonesRepresent':
86
                if (!empty($data['numbers']) || !is_array($data['numbers'])) {
87
                    $res = Utils::getPhonesRepresent($data['numbers']);
88
                } else {
89
                    $res->messages['error'][] = 'Wrong numbers value in POST/GET data';
90
                }
91
                break;
92
            case 'getPhoneRepresent':
93
                if (!empty($data['number'])) {
94
                    $res = Utils::getPhoneRepresent($data['number']);
95
                } else {
96
                    $res->messages['error'][] = 'Empty number value in POST/GET data';
97
                }
98
                break;
99
            default:
100
                $res->messages['error'][] = "Unknown action - $action in " . __CLASS__;
101
        }
102
103
        $res->function = $action;
104
105
        return $res;
106
    }
107
108
109
}