Passed
Push — develop ( a56058...ddfce4 )
by Nikolay
04:39
created

StatusOfModuleInstallationAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 37
c 0
b 0
f 0
dl 0
loc 55
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 39 7
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\PBXCoreREST\Lib\PBXApiResult;
23
use Phalcon\Di;
24
25
/**
26
 *  Class StatusOfModuleInstallation
27
 *  Checks the status of a module installation by the provided zip file path.
28
 *
29
 * @package MikoPBX\PBXCoreREST\Lib\Modules
30
 */
31
class StatusOfModuleInstallationAction extends \Phalcon\Di\Injectable
32
{
33
    const PROGRESS_FILE_NOT_FOUND = 'PROGRESS_FILE_NOT_FOUND';
34
    const INSTALLATION_ERROR = 'INSTALLATION_ERROR';
35
    const INSTALLATION_COMPLETE = 'INSTALLATION_COMPLETE';
36
    const INSTALLATION_IN_PROGRESS = 'INSTALLATION_IN_PROGRESS';
37
    const I_STATUS = 'i_status';
38
    const I_STATUS_PROGRESS = 'i_status_progress';
39
40
    /**
41
     * Checks the status of a module installation by the provided zip file path.
42
     *
43
     * @param string $filePath The path of the module installation file.
44
     *
45
     * @return PBXApiResult An object containing the result of the API call.
46
     */
47
    public static function main(string $filePath): PBXApiResult
48
    {
49
        $res = new PBXApiResult();
50
        $res->processor = __METHOD__;
51
        $di = Di::getDefault();
52
        if ($di === null) {
53
            $res->messages[] = 'Dependency injector does not initialized';
54
55
            return $res;
56
        }
57
        $temp_dir = dirname($filePath);
58
        $progress_file = $temp_dir . '/installation_progress';
59
        $error_file = $temp_dir . '/installation_error';
60
        if (!file_exists($error_file) || !file_exists($progress_file)) {
61
            $res->success = false;
62
            $res->data[self::I_STATUS] = self::PROGRESS_FILE_NOT_FOUND;
63
            $res->data[self::I_STATUS_PROGRESS] = '0';
64
        } elseif (file_get_contents($error_file) !== '') {
65
            $res->success = false;
66
            $res->data[self::I_STATUS] = self::INSTALLATION_ERROR;
67
            $res->data[self::I_STATUS_PROGRESS] = '0';
68
            $res->messages['error'][] = file_get_contents($error_file);
69
        } elseif ('100' === file_get_contents($progress_file)) {
70
            $res->success = true;
71
            $res->data[self::I_STATUS_PROGRESS] = '100';
72
            $res->data[self::I_STATUS] = self::INSTALLATION_COMPLETE;
73
74
            $resModuleMetadata = GetMetadataFromModulePackageAction::main($filePath);
75
            if ($resModuleMetadata->success) {
76
                $res->data['uniqid'] = $resModuleMetadata->data['uniqid'];
77
            }
78
79
        } else {
80
            $res->success = true;
81
            $res->data[self::I_STATUS] = self::INSTALLATION_IN_PROGRESS;
82
            $res->data[self::I_STATUS_PROGRESS] = file_get_contents($progress_file);
83
        }
84
85
        return $res;
86
    }
87
}