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

DownloadStatusAction::main()   B

Complexity

Conditions 9
Paths 48

Size

Total Lines 57
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
c 0
b 0
f 0
dl 0
loc 57
rs 7.6444
cc 9
nc 48
nop 1

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