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

UninstallModule::main()   A

Complexity

Conditions 5
Paths 28

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 53
rs 9.1288
c 1
b 0
f 0
cc 5
nc 28
nop 2

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\Modules;
21
22
use MikoPBX\Core\System\Processes;
23
use MikoPBX\Core\System\Util;
24
use MikoPBX\Modules\PbxExtensionUtils;
25
use MikoPBX\Modules\Setup\PbxExtensionSetupFailure;
26
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
27
28
/**
29
 *  Class UninstallModule
30
 *  Uninstall extension module
31
 *
32
 * @package MikoPBX\PBXCoreREST\Lib\Modules
33
 */
34
class UninstallModule extends \Phalcon\Di\Injectable
35
{
36
    /**
37
     * Uninstall extension module
38
     *
39
     * @param string $moduleUniqueID The unique ID of the module to uninstall.
40
     * @param bool $keepSettings Indicates whether to keep the module settings.
41
     *
42
     * @return PBXApiResult An object containing the result of the API call.
43
     */
44
    public static function main(string $moduleUniqueID, bool $keepSettings): PBXApiResult
45
    {
46
        $res = new PBXApiResult();
47
        $res->processor = __METHOD__;
48
        $currentModuleDir = PbxExtensionUtils::getModuleDir($moduleUniqueID);
49
50
        // Kill all module processes
51
        if (is_dir("{$currentModuleDir}/bin")) {
52
            $busyboxPath = Util::which('busybox');
53
            $killPath = Util::which('kill');
54
            $lsofPath = Util::which('lsof');
55
            $grepPath = Util::which('grep');
56
            $awkPath = Util::which('awk');
57
            $uniqPath = Util::which('uniq');
58
59
            // Execute the command to kill all processes related to the module
60
            Processes::mwExec(
61
                "{$busyboxPath} {$killPath} -9 $({$lsofPath} {$currentModuleDir}/bin/* |  {$busyboxPath} {$grepPath} -v COMMAND | {$busyboxPath} {$awkPath}  '{ print $2}' | {$busyboxPath} {$uniqPath})"
62
            );
63
        }
64
65
        // Uninstall module with keep settings and backup db
66
        $moduleClass = "Modules\\{$moduleUniqueID}\\Setup\\PbxExtensionSetup";
67
68
        try {
69
            if (class_exists($moduleClass)
70
                && method_exists($moduleClass, 'uninstallModule')) {
71
                // Instantiate the module setup class and call the uninstallModule method
72
                $setup = new $moduleClass($moduleUniqueID);
73
            } else {
74
75
                // Use a fallback class to uninstall the module from the database if it doesn't exist on disk
76
                $moduleClass = PbxExtensionSetupFailure::class;
77
                $setup = new $moduleClass($moduleUniqueID);
78
            }
79
            $setup->uninstallModule($keepSettings);
80
        } finally {
81
            if (is_dir($currentModuleDir)) {
82
                // If the module directory still exists, force uninstallation
83
                $rmPath = Util::which('rm');
84
85
                // Remove the module directory recursively
86
                Processes::mwExec("{$rmPath} -rf {$currentModuleDir}");
87
88
                // Use the fallback class to unregister the module from the database
89
                $moduleClass = PbxExtensionSetupFailure::class;
90
                $setup = new $moduleClass($moduleUniqueID);
91
                $setup->unregisterModule();
92
            }
93
        }
94
        $res->success = true;
95
96
        return $res;
97
    }
98
99
}