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

DebugService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 22
c 0
b 0
f 0
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadLogs() 0 9 2
A __construct() 0 3 1
A downloadFiles() 0 22 2
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