Passed
Push — master ( 03796d...557337 )
by Nikita
23:00 queued 11:26
created

DebugService::downloadLogs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Gameap\Services\Daemon;
4
5
use Gameap\Helpers\OsHelper;
6
use Gameap\Models\DedicatedServer;
7
use Knik\Gameap\GdaemonFiles;
8
use ZipArchive;
9
10
class DebugService
11
{
12
    const LINUX_LOG_PATH = '/var/log/gameap-daemon';
13
    const WINDOWS_LOG_PATH = 'C:/gameap/daemon/logs';
14
15
    /** @var GdaemonFiles */
16
    private $gdaemonFiles;
17
18
    public function __construct(GdaemonFiles $gdaemonFiles)
19
    {
20
        $this->gdaemonFiles = $gdaemonFiles;
21
    }
22
23
    public function downloadLogs(DedicatedServer $dedicatedServer): string
24
    {
25
        if ($dedicatedServer->isLinux()) {
26
            $logPath = self::LINUX_LOG_PATH;
27
        } else {
28
            $logPath = self::WINDOWS_LOG_PATH;
29
        }
30
31
        return $this->downloadFiles($dedicatedServer, $logPath);
32
    }
33
34
    private function downloadFiles(DedicatedServer $dedicatedServer, $path): string
35
    {
36
        $this->gdaemonFiles->setConfig($dedicatedServer->gdaemonSettings());
37
38
        $tmpDir = OsHelper::makeTempDirectory();
39
40
        $zipFilePath = OsHelper::tempFile();
41
        $zip = new ZipArchive();
42
        $zip->open($zipFilePath, ZipArchive::CREATE);
43
44
        foreach ($this->gdaemonFiles->listFiles($path) as $fileName) {
45
            $fullPath = $path . '/' . $fileName;
46
            $destination = $tmpDir . '/' . $fileName;
47
48
            $this->gdaemonFiles->get($fullPath, $destination);
49
50
            $zip->addFile($destination, "daemon_logs/". $fileName);
51
        }
52
53
        $zip->close();
54
55
        return $zipFilePath;
56
    }
57
}
58