Passed
Push — develop ( c8e8dc...cb2748 )
by Nikolay
06:11 queued 13s
created

StatusUploadFile   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 48
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 38 7
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\PBXCoreREST\Lib\PBXApiResult;
24
use Phalcon\Di;
25
26
/**
27
 *  Class StatusUploadFile
28
 *  Returns Status of uploading and merging process
29
 *
30
 * @package MikoPBX\PBXCoreREST\Lib\Modules
31
 */
32
class StatusUploadFile extends \Phalcon\Di\Injectable
33
{
34
35
    /**
36
     * Returns Status of uploading and merging process
37
     *
38
     * @param string $upload_id
39
     *
40
     * @return PBXApiResult An object containing the result of the API call.
41
     */
42
    public static function main(string $upload_id): PBXApiResult
43
    {
44
        $res = new PBXApiResult();
45
        $res->processor = __METHOD__;
46
        $di = Di::getDefault();
47
        if ($di === null) {
48
            $res->messages[] = 'Dependency injector does not initialized';
49
50
            return $res;
51
        }
52
        $uploadDir = $di->getShared('config')->path('www.uploadDir');
53
54
        $progress_dir = $uploadDir . '/' . $upload_id;
55
        $progress_file = $progress_dir . '/merging_progress';
56
        if (empty($upload_id)) {
57
            $res->success = false;
58
            $res->data[FilesConstants::D_STATUS_PROGRESS] = '0';
59
            $res->data[FilesConstants::D_STATUS] = FilesConstants::STATUS_NOT_FOUND;
60
            $res->messages[] = 'Upload ID does not set';
61
        } elseif (!file_exists($progress_file) && file_exists($progress_dir)) {
62
            $res->success = true;
63
            $res->data[FilesConstants::D_STATUS_PROGRESS] = '0';
64
            $res->data[FilesConstants::D_STATUS] = FilesConstants::UPLOAD_IN_PROGRESS;
65
        } elseif (!file_exists($progress_dir)) {
66
            $res->success = false;
67
            $res->data[FilesConstants::D_STATUS_PROGRESS] = '0';
68
            $res->data[FilesConstants::D_STATUS] = FilesConstants::STATUS_NOT_FOUND;
69
            $res->messages[] = 'Does not found anything with path: ' . $progress_dir;
70
        } elseif ('100' === file_get_contents($progress_file)) {
71
            $res->success = true;
72
            $res->data[FilesConstants::D_STATUS_PROGRESS] = '100';
73
            $res->data[FilesConstants::D_STATUS] = FilesConstants::UPLOAD_COMPLETE;
74
        } else {
75
            $res->success = true;
76
            $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file);
77
        }
78
79
        return $res;
80
    }
81
}