Passed
Push — develop ( 15bc6b...5ea31b )
by Nikolay
05:50 queued 12s
created

WorkerMergeUploadedFile::start()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 37
rs 8.6026
c 0
b 0
f 0
cc 7
nc 8
nop 1
1
<?php
2
/**
3
 * Copyright © MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Alexey Portnov, 2 2020
7
 */
8
9
namespace MikoPBX\Core\Workers;
10
require_once 'Globals.php';
11
use MikoPBX\Core\System\{System, Util};
12
use Nats\Message;
13
use Phalcon\Exception;
14
15
16
class WorkerMergeUploadedFile extends WorkerBase
17
{
18
    public function start($argv): void
19
    {
20
        if (count($argv) <= 1) {
21
            // Не переданы аргументы.
22
            exit(2);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
23
        }
24
        $settings_file = trim($argv[1]);
25
        if ( ! file_exists($settings_file)) {
26
            // Не найлен файл с настройками.
27
            exit(3);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
28
        }
29
        $file_data = json_decode(file_get_contents($settings_file), true);
30
        if ( ! isset($file_data['action'])) {
31
            // Не корректный формат файла с настройками.
32
            exit(4);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
33
        }
34
35
        if ($file_data['action'] === 'merge') {
36
            $settings = $file_data['data'];
37
            if ( ! file_exists($settings['result_file'])) {
38
                Util::mergeFilesInDirectory(
39
                    $settings['temp_dir'],
40
                    $settings['resumableFilename'],
41
                    $settings['resumableTotalChunks'],
42
                    $settings['result_file'],
43
                    dirname($settings['result_file'])
44
                );
45
            }
46
            $res = file_exists($settings['result_file']);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
47
            // Отложенное удаление файла.
48
            $rm_file = basename(dirname($settings['result_file'])) === 'tmp' ? $settings['result_file'] : dirname(
49
                $settings['result_file']
50
            );
51
            Util::mwExecBg(
52
                '/etc/rc/shell_functions.sh killprocesses ' . $rm_file . ' -TERM 0;rm -rf ' . $rm_file,
53
                '/dev/null',
54
                120
55
            );
56
        }
57
    }
58
59
}
60
61
62
// Start worker process
63
$workerClassname = WorkerMergeUploadedFile::class;
64
if (isset($argv) && count($argv) > 1) {
65
    cli_set_process_title($workerClassname);
66
    try {
67
        $worker = new $workerClassname();
68
        $worker->start($argv);
69
    } catch (\Exception $e) {
70
        global $errorLogger;
71
        $errorLogger->captureException($e);
72
        Util::sysLogMsg("{$workerClassname}_EXCEPTION", $e->getMessage());
73
    }
74
}
75