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

InstallFromPackage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 37
dl 0
loc 70
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A main() 0 58 5
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\PBXCoreREST\Lib\PBXApiResult;
26
use MikoPBX\PBXCoreREST\Workers\WorkerModuleInstaller;
27
28
/**
29
 *  Class InstallModuleFromPackage
30
 *  Installs a new additional extension module from an early uploaded zip archive.
31
 *
32
 * @package MikoPBX\PBXCoreREST\Lib\Modules
33
 */
34
class InstallFromPackage extends \Phalcon\Di\Injectable
35
{
36
    const MODULE_WAS_ENABLED='moduleWasEnabled';
37
    const FILE_PATH = 'filePath';
38
39
    /**
40
     * Installs a new additional extension module from an early uploaded zip archive.
41
     *
42
     * @param string $filePath The path to the module file.
43
     *
44
     * @return PBXApiResult An object containing the result of the API call.
45
     */
46
    public static function main(string $filePath): PBXApiResult
47
    {
48
        $res = new PBXApiResult();
49
        $res->processor = __METHOD__;
50
        $resModuleMetadata = Common::getMetadataFromModuleFile($filePath);
51
        if (!$resModuleMetadata->success) {
52
            return $resModuleMetadata;
53
        }
54
55
        $moduleUniqueID = $resModuleMetadata->data['uniqid'];
56
        // Disable the module if it's enabled
57
        $moduleWasEnabled = false;
58
        if (PbxExtensionUtils::isEnabled($moduleUniqueID)) {
59
            $res = DisableModule::main($moduleUniqueID);
60
            if (!$res->success) {
61
                return $res;
62
            }
63
            $moduleWasEnabled = true;
64
        }
65
66
        $currentModuleDir = PbxExtensionUtils::getModuleDir($moduleUniqueID);
67
        $needBackup = is_dir($currentModuleDir);
68
69
        if ($needBackup) {
70
            UninstallModule::main($moduleUniqueID, true);
71
        }
72
73
        // Start the background process to install the module
74
        $temp_dir = dirname($filePath);
75
76
        // Create a progress file to track the installation progress
77
        file_put_contents($temp_dir . '/installation_progress', '0');
78
79
        // Create an error file to store any installation errors
80
        file_put_contents($temp_dir . '/installation_error', '');
81
82
        $install_settings = [
83
            self::FILE_PATH => $filePath,
84
            'currentModuleDir' => $currentModuleDir,
85
            'uniqid' => $moduleUniqueID,
86
        ];
87
88
        // Save the installation settings to a JSON file
89
        $settings_file = "{$temp_dir}/install_settings.json";
90
        file_put_contents(
91
            $settings_file,
92
            json_encode($install_settings, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
93
        );
94
        $phpPath = Util::which('php');
95
        $workerFilesMergerPath = Util::getFilePathByClassName(WorkerModuleInstaller::class);
96
97
        // Execute the background process to install the module
98
        Processes::mwExecBg("{$phpPath} -f {$workerFilesMergerPath} start '{$settings_file}'");
99
        $res->data[self::FILE_PATH] = $filePath;
100
        $res->data[self::MODULE_WAS_ENABLED] = $moduleWasEnabled;
101
        $res->success = true;
102
103
        return $res;
104
    }
105
}