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

StartDownload   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 31
dl 0
loc 52
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A main() 0 41 4
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\Core\System\Util;
25
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
26
use MikoPBX\PBXCoreREST\Workers\WorkerDownloader;
27
use Phalcon\Di;
28
29
/**
30
 *  Class StartDownload
31
 *  Starts the module download in a separate background process.
32
 *
33
 * @package MikoPBX\PBXCoreREST\Lib\Modules
34
 */
35
class StartDownload extends \Phalcon\Di\Injectable
36
{
37
    /**
38
     * Starts the module download in a separate background process.
39
     *
40
     * @param string $moduleUniqueID The module unique id.
41
     * @param string $url The download URL of the module.
42
     * @param string $md5 The MD5 hash of the module file.
43
     *
44
     * @return PBXApiResult An object containing the result of the API call.
45
     */
46
    public static function main(string $moduleUniqueID, string $url, string $md5): PBXApiResult
47
    {
48
        $res = new PBXApiResult();
49
        $res->processor = __METHOD__;
50
        $di = Di::getDefault();
51
        if ($di !== null) {
52
            $tempDir = $di->getShared(ConfigProvider::SERVICE_NAME)->path('www.uploadDir');
53
        } else {
54
            $tempDir = '/tmp';
55
        }
56
57
        $moduleDirTmp = "{$tempDir}/{$moduleUniqueID}";
58
        Util::mwMkdir($moduleDirTmp);
59
60
        $download_settings = [
61
            'res_file' => "$moduleDirTmp/modulefile.zip",
62
            'url' => $url,
63
            'module' => $moduleUniqueID,
64
            'md5' => $md5,
65
            'action' => 'moduleInstall',
66
        ];
67
        if (file_exists("$moduleDirTmp/error")) {
68
            unlink("$moduleDirTmp/error");
69
        }
70
        if (file_exists("$moduleDirTmp/installed")) {
71
            unlink("$moduleDirTmp/installed");
72
        }
73
        file_put_contents("$moduleDirTmp/progress", '0');
74
        file_put_contents(
75
            "$moduleDirTmp/download_settings.json",
76
            json_encode($download_settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
77
        );
78
        $workerDownloaderPath = Util::getFilePathByClassName(WorkerDownloader::class);
79
        $phpPath = Util::which('php');
80
        Processes::mwExecBg("{$phpPath} -f {$workerDownloaderPath} start {$moduleDirTmp}/download_settings.json");
81
82
        $res->data['uniqid'] = $moduleUniqueID;
83
        $res->data['d_status'] = 'DOWNLOAD_IN_PROGRESS';
84
        $res->success = true;
85
86
        return $res;
87
    }
88
}