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

DownloadStatus   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 54
dl 0
loc 76
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 57 9
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\Common\Providers\ConfigProvider;
23
use MikoPBX\Core\System\Processes;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
use Phalcon\Di;
26
27
/**
28
 *  Class DownloadStatus
29
 *  Returns the download status of a module.
30
 *
31
 * @package MikoPBX\PBXCoreREST\Lib\Modules
32
 */
33
class DownloadStatus extends \Phalcon\Di\Injectable
34
{
35
    const STATUS_NOT_FOUND = 'NOT_FOUND';
36
    const DOWNLOAD_ERROR = 'DOWNLOAD_ERROR';
37
    const DOWNLOAD_COMPLETE = 'DOWNLOAD_COMPLETE';
38
    const DOWNLOAD_IN_PROGRESS = 'DOWNLOAD_IN_PROGRESS';
39
    const D_STATUS  = 'd_status';
40
    const D_STATUS_PROGRESS = 'd_status_progress';
41
    const D_ERROR  = 'd_error';
42
    const FILE_PATH = 'filePath';
43
44
45
    /**
46
     * Returns the download status of a module.
47
     *
48
     * @param string $moduleUniqueID The unique ID of the module.
49
     *
50
     * @return PBXApiResult An object containing the result of the API call.
51
     */
52
    public static function main(string $moduleUniqueID): PBXApiResult
53
    {
54
        clearstatcache();
55
        $res = new PBXApiResult();
56
        $res->processor = __METHOD__;
57
        $di = Di::getDefault();
58
        if ($di !== null) {
59
            $tempDir = $di->getShared(ConfigProvider::SERVICE_NAME)->path('www.uploadDir');
60
        } else {
61
            $tempDir = '/tmp';
62
        }
63
        $moduleDirTmp = $tempDir . '/' . $moduleUniqueID;
64
        $progress_file = $moduleDirTmp . '/progress';
65
        $error = '';
66
        if (file_exists($moduleDirTmp . '/error')) {
67
            $error = trim(file_get_contents($moduleDirTmp . '/error'));
68
        }
69
70
        // Wait until a download process started
71
        $d_pid = Processes::getPidOfProcess("{$moduleDirTmp}/download_settings.json");
72
        if (empty($d_pid)) {
73
            usleep(500000);
74
        }
75
76
        if (!file_exists($progress_file)) {
77
            $res->data[self::D_STATUS_PROGRESS] = '0';
78
            $res->data[self::D_STATUS] = self::STATUS_NOT_FOUND;
79
            $res->success = false;
80
        } elseif ('' !== $error) {
81
            $res->data[self::D_STATUS] = self::DOWNLOAD_ERROR;
82
            $res->data[self::D_STATUS_PROGRESS] = file_get_contents($progress_file);
83
            $res->data[self::D_ERROR] = $error;
84
            $res->messages[] = file_get_contents($moduleDirTmp . '/error');
85
            $res->success = false;
86
        } elseif ('100' === file_get_contents($progress_file)) {
87
            $res->data[self::D_STATUS_PROGRESS] = '100';
88
            $res->data[self::D_STATUS] = self::DOWNLOAD_COMPLETE;
89
            $res->data[self::FILE_PATH] = "$moduleDirTmp/modulefile.zip";
90
            $res->success = true;
91
        } else {
92
            $res->data[self::D_STATUS_PROGRESS] = file_get_contents($progress_file);
93
            $d_pid = Processes::getPidOfProcess($moduleDirTmp . '/download_settings.json');
94
            if (empty($d_pid)) {
95
                $res->data[self::D_STATUS] = self::DOWNLOAD_ERROR;
96
                if (file_exists($moduleDirTmp . '/error')) {
97
                    $res->messages[] = file_get_contents($moduleDirTmp . '/error');
98
                } else {
99
                    $res->messages[] = "Download process interrupted at {$res->data['d_status_progress']}%";
100
                }
101
                $res->success = false;
102
            } else {
103
                $res->data[self::D_STATUS] = self::DOWNLOAD_IN_PROGRESS;
104
                $res->success = true;
105
            }
106
        }
107
108
        return $res;
109
    }
110
111
}