Passed
Push — develop ( e523f5...8ba359 )
by Портнов
05:00
created

WorkerMakeLogFilesArchive::progress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 1
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\Workers;
21
22
require_once 'Globals.php';
23
24
use MikoPBX\Core\System\Processes;
25
use MikoPBX\Core\System\Storage;
26
use MikoPBX\Core\System\System;
27
use MikoPBX\Core\Workers\WorkerBase;
28
use MikoPBX\Core\System\Util;
29
use ZipArchive;
30
use MikoPBX\PBXCoreREST\Lib\SysinfoManagementProcessor;
31
32
33
/**
34
 * The WorkerMakeLogFilesArchive class is responsible for archiving log files.
35
 *
36
 * @package MikoPBX\PBXCoreREST\Workers
37
 */
38
class WorkerMakeLogFilesArchive extends WorkerBase
39
{
40
    private string $progress_file = '';
41
42
    /**
43
     * Starts the log files archiving worker process.
44
     *
45
     * @param array $argv The command-line arguments passed to the worker.
46
     * @return void
47
     */
48
    public function start(array $argv): void
49
    {
50
        $settings_file = $argv[2] ?? '';
51
52
        // Check if the settings file exists
53
        if ( ! file_exists($settings_file)) {
54
            Util::sysLogMsg("WorkerMakeLogFilesArchive", 'File with settings not found', LOG_ERR);
55
56
            return;
57
        }
58
        $file_data = json_decode(file_get_contents($settings_file), true);
59
60
        // Check if the 'result_file' key is present in the settings file
61
        if ( ! isset($file_data['result_file'])) {
62
            Util::sysLogMsg("WorkerMakeLogFilesArchive", 'Wrong settings', LOG_ERR);
63
64
            return;
65
        }
66
        $tcpdump_only  = $file_data['tcpdump_only'] ?? true;
67
        $resultFile    = $file_data['result_file'];
68
        $this->progress_file = "{$resultFile}.progress";
69
        file_put_contents($this->progress_file, '1');
70
71
        $rmPath   = Util::which('rm');
72
        $findPath = Util::which('find');
73
74
        // Remove the result file if it already exists
75
        if (file_exists($resultFile)) {
76
            Processes::mwExec("{$rmPath} -rf {$resultFile}");
77
        }
78
        $logDir         = System::getLogDir();
79
        $systemInfoFile = "{$logDir}/system-information.log";
80
        if ($tcpdump_only) {
81
            $command = "{$findPath} {$logDir}/tcpDump -type f ";
82
        } else {
83
            // Collect system info
84
            file_put_contents($systemInfoFile, SysinfoManagementProcessor::prepareSysyinfoContent());
85
            $command = "{$findPath} {$logDir} -type f ";
86
        }
87
        Processes::mwExec($command, $out);
88
        $zip     = new ZipArchive();
89
90
        $storageDir = '';
91
        Storage::isStorageDiskMounted('',$storageDir);
92
        if($zip->open($resultFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true){
93
            foreach ($out as $filename) {
94
                if ( !file_exists($filename)) {
95
                    continue;
96
                }
97
                $zip->addFile($filename, str_replace("$storageDir/mikopbx/", '', $filename));
98
            }
99
            if(version_compare(PHP_VERSION, '8.0.0') >= 0){
100
                $zip->registerProgressCallback(0.05, [$this, "progress"]);
0 ignored issues
show
Bug introduced by
The method registerProgressCallback() does not exist on ZipArchive. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
                $zip->/** @scrutinizer ignore-call */ 
101
                      registerProgressCallback(0.05, [$this, "progress"]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            }
102
            $zip->close();
103
        }
104
        file_put_contents($this->progress_file, '100');
105
        if ($tcpdump_only === true) {
106
            // Delete TCP dump
107
            Processes::mwExec("{$rmPath} -rf {$logDir}/tcpDump");
108
        }
109
        Processes::mwExec("{$rmPath} -rf $systemInfoFile $settings_file");
110
    }
111
112
    public function progress($rate):void
113
    {
114
        $progress = round($rate * 100);
115
        if ($progress % 5 === 0) {
116
            file_put_contents($this->progress_file, $progress);
117
        }
118
    }
119
}
120
121
// Start the log files archiving worker process
122
WorkerMakeLogFilesArchive::startWorker($argv ?? []);
123