Passed
Push — develop ( d617cb...49fd37 )
by Nikolay
05:15 queued 36s
created

ModulesManagementProcessor::moduleStartDownload()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 41
rs 9.44
cc 4
nc 8
nop 3
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 MikoPBX\PBXCoreREST\Lib\Modules\DisableModule;
23
use MikoPBX\PBXCoreREST\Lib\Modules\enableModule;
24
use MikoPBX\PBXCoreREST\Lib\Modules\getAvailableModules;
25
use MikoPBX\PBXCoreREST\Lib\Modules\GetModuleInfo;
26
use MikoPBX\PBXCoreREST\Lib\Modules\GetModuleLink;
27
use MikoPBX\PBXCoreREST\Lib\Modules\InstallFromPackage;
28
use MikoPBX\PBXCoreREST\Lib\Modules\DownloadStatus;
29
use MikoPBX\PBXCoreREST\Lib\Modules\InstallFromRepo;
30
use MikoPBX\PBXCoreREST\Lib\Modules\StartDownload;
31
use MikoPBX\PBXCoreREST\Lib\Modules\StatusOfModuleInstallation;
32
use MikoPBX\PBXCoreREST\Lib\Modules\UninstallModule;
33
use Phalcon\Di;
34
use Phalcon\Di\Injectable;
35
36
/**
37
 * Class ModulesManagementProcessor
38
 *
39
 * Manages external modules for download, install, uninstall, enable, disable.
40
 *
41
 * @property Di di
42
 * @package MikoPBX\PBXCoreREST\Lib
43
 */
44
class ModulesManagementProcessor extends Injectable
45
{
46
    /**
47
     * Processes module management requests.
48
     *
49
     * @param array $request The request data.
50
     *
51
     * @return PBXApiResult An object containing the result of the API call.
52
     *
53
     */
54
    public static function callBack(array $request): PBXApiResult
55
    {
56
        $action = $request['action'];
57
        $data = $request['data'];
58
        $res = new PBXApiResult();
59
        $res->processor = __METHOD__;
60
            switch ($action) {
61
                case 'moduleStartDownload':
62
                    $module = $request['data']['uniqid'];
63
                    $url = $request['data']['url'];
64
                    $md5 = $request['data']['md5'];
65
                    $res = StartDownload::main($module, $url, $md5);
66
                    break;
67
                case 'moduleDownloadStatus':
68
                    $module = $request['data']['uniqid'];
69
                    $res = DownloadStatus::main($module);
70
                    break;
71
                case 'installFromPackage':
72
                    $filePath = $data['filePath'];
73
                    $res = InstallFromPackage::main($filePath);
74
                    break;
75
                case 'installFromRepo':
76
                    $moduleUniqueID = $data['uniqid'];
77
                    $releaseId = $data['releaseId'];
78
                    InstallFromRepo::main($moduleUniqueID, $releaseId);
79
                    break;
80
                case 'getModuleInfo':
81
                    $moduleUniqueID = $data['uniqid'];
82
                    $res = GetModuleInfo::main($moduleUniqueID);
83
                    break;
84
                case 'statusOfModuleInstallation':
85
                    $filePath = $data['filePath'];
86
                    $res = StatusOfModuleInstallation::main($filePath);
87
                    break;
88
                case 'enableModule':
89
                    $moduleUniqueID = $data['uniqid'];
90
                    $res = EnableModule::main($moduleUniqueID);
91
                    break;
92
                case 'disableModule':
93
                    $moduleUniqueID = $data['uniqid'];
94
                    $reason = $data['reason']??'';
95
                    $reasonText = $data['reasonText']??'';
96
                    $res = DisableModule::main($moduleUniqueID, $reason, $reasonText);
97
                    break;
98
                case 'uninstallModule':
99
                    $moduleUniqueID = $data['uniqid'];
100
                    $keepSettings = $data['keepSettings'] === 'true';
101
                    $res = UninstallModule::main($moduleUniqueID, $keepSettings);
102
                    break;
103
                case 'getAvailableModules':
104
                    $res = GetAvailableModules::main();
105
                    break;
106
                case 'getModuleLink':
107
                    $moduleReleaseId = $data['releaseId'];
108
                    $res = GetModuleLink::main($moduleReleaseId);
109
                    break;
110
                default:
111
                    $res->messages['error'][] = "Unknown action - $action in ".__CLASS__;
112
            }
113
        $res->function = $action;
114
115
        return $res;
116
    }
117
}