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

FirmwareDownloadStatusAction::main()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 52
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 52
rs 8.0355
cc 8
nc 24
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-2024 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\Files;
21
22
23
use MikoPBX\Core\System\Processes;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
26
/**
27
 * Class FirmwareDownloadStatus
28
 * Get the progress status of the firmware file download.
29
 *
30
 * @package MikoPBX\PBXCoreREST\Lib\Files
31
 */
32
class FirmwareDownloadStatusAction extends \Phalcon\Di\Injectable
33
{
34
    /**
35
     * Get the progress status of the firmware file download.
36
     *
37
     * @param string $imgFileName The filename of the firmware file.
38
     *
39
     * @return PBXApiResult An object containing the result of the API call.
40
     */
41
    public static function main(string $imgFileName): PBXApiResult
42
    {
43
        clearstatcache();
44
        $res = new PBXApiResult();
45
        $res->processor = __METHOD__;
46
        $res->success = true;
47
48
        $firmwareDirTmp = dirname($imgFileName);
49
        $progress_file = $firmwareDirTmp . '/progress';
50
51
        // Wait until a download process started
52
        $d_pid = Processes::getPidOfProcess("{$firmwareDirTmp}/download_settings.json");
53
        if (empty($d_pid)) {
54
            usleep(500000);
55
        }
56
        $error = '';
57
        if (file_exists("{$firmwareDirTmp}/error")) {
58
            $error = trim(file_get_contents("{$firmwareDirTmp}/error"));
59
        }
60
61
        if (!file_exists($progress_file)) {
62
            $res->data[FilesConstants::D_STATUS_PROGRESS] = '0';
63
            $res->messages[] = FilesConstants::STATUS_NOT_FOUND;
64
            $res->success = false;
65
        } elseif ('' !== $error) {
66
            $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_ERROR;
67
            $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file);
68
            $res->messages[] = file_get_contents("{$firmwareDirTmp}/error");
69
            $res->success = false;
70
        } elseif ('100' === file_get_contents($progress_file)) {
71
            $res->data[FilesConstants::D_STATUS_PROGRESS] = '100';
72
            $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_COMPLETE;
73
            $res->data[FilesConstants::FILE_PATH] = $imgFileName;
74
            $res->success = true;
75
        } else {
76
            $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file);
77
            $d_pid = Processes::getPidOfProcess("{$firmwareDirTmp}/download_settings.json");
78
            if (empty($d_pid)) {
79
                $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_ERROR;
80
                if (file_exists("{$firmwareDirTmp}/error")) {
81
                    $res->messages[] = file_get_contents("{$firmwareDirTmp}/error");
82
                } else {
83
                    $res->messages[] = "Download process interrupted at {$res->data['d_status_progress']}%";
84
                }
85
                $res->success = false;
86
            } else {
87
                $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_IN_PROGRESS;
88
                $res->success = true;
89
            }
90
        }
91
92
        return $res;
93
    }
94
}