Passed
Push — master ( 24bd5b...fabfb6 )
by Nils
10:48
created

Runner::fileExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Exec;
4
5
use Spatie\Ssh\Ssh;
6
use Symfony\Component\Process\Process;
7
8
class Runner
9
{
10
    private bool $timeout = false;
11
12
    private static ?Runner $instance = null;
13
14
    private bool $remoteOn = false;
15
    private string $remoteDsn;
16
    private Ssh $sshConnection;
17
18
    public function setRemote($dsn)
19
    {
20
        $this->remoteOn = true;
21
        $this->remoteDsn = $dsn;
22
23
        $sshCredentials = explode('@', $this->remoteDsn);
24
        $this->sshConnection = Ssh::create($sshCredentials[0], $sshCredentials[1]);
25
    }
26
27
    static public function getInstance(): Runner
28
    {
29
        if (self::$instance === null) {
30
            self::$instance = new self();
31
        }
32
33
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return Startwind\Inventorio\Exec\Runner. Consider adding an additional type-check to rule them out.
Loading history...
34
    }
35
36
    public function __construct()
37
    {
38
        $process = $this->run('timeout --version');
39
        if ($process->isSuccessful()) {
40
            $this->timeout = true;
41
        } else {
42
            $this->timeout = false;
43
        }
44
    }
45
46
    public function run($command, $killAfterSeconds = 5): Process
47
    {
48
49
        if ($this->timeout) {
50
            $shellCommandLine = "timeout --kill-after=" . $killAfterSeconds . "s 1m " . $command;
51
        } else {
52
            $shellCommandLine = $command;
53
        }
54
55
        // var_dump($command);
56
57
        if ($this->remoteOn) {
58
            // echo "DEBUG: " . $command . "\n";
59
            $process = $this->sshConnection->execute($command);
60
        } else {
61
            $process = Process::fromShellCommandline($shellCommandLine);
62
            $process->run();
63
        }
64
65
        return $process;
66
    }
67
68
    public function commandExists(string $command): bool
69
    {
70
        $which = $this->run('which ' . $command)->getOutput();
71
        if (empty($which)) {
72
            return false;
73
        }
74
75
        return true;
76
    }
77
78
    public static function outputToArray(string $output): array
79
    {
80
        return explode("\n", trim($output));
81
    }
82
83
    public function getFileContents(string $path, bool $asArray = false): string|false|array
84
    {
85
        return File::getInstance()->getContents($path, $asArray);
86
    }
87
88
    public function fileExists(string $path): bool
89
    {
90
        return File::getInstance()->fileExists($path);
91
    }
92
}