Passed
Push — master ( 939a6b...8ca850 )
by Bence
03:55
created

CommandRunner::run()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.3731

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 4
dl 0
loc 22
ccs 10
cts 14
cp 0.7143
crap 4.3731
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
namespace CSFCloud\Shell;
4
5
use CSFCloud\TempFiles\TempManager;
6
use CSFCloud\TempFiles\TempFile;
7
use Exception;
8
9
class CommandRunner {
10
11
    const COMMAND_SYNC = 1;
12
    const COMMAND_ASYNC = 2;
13
14
    private $tempmrg;
15
16 3
    public function __construct() {
17 3
        $this->tempmrg = new TempManager("csf_command_runner");
18 3
    }
19
20 3
    public function run(int $type, string $dir, string $command, bool $sudo = false) : TempFile {
21 3
        $tempfile = $this->tempmrg->createFile();
22 3
        $temp_path = $tempfile->getPath();
23
24 3
        $cmd = "cd " . escapeshellarg($dir) . " && ";
25 3
        if ($sudo === true) {
26
            $cmd .= "sudo ";
27
        }
28 3
        $cmd .= $command . " ";
29 3
        $cmd .= ">> " . escapeshellarg($temp_path) . " 2>> " . escapeshellarg($temp_path) . " ";
30
31 3
        if ($type === self::COMMAND_SYNC) {
32
33
        } else if ($type === self::COMMAND_ASYNC) {
34
            $cmd = "(" . $cmd . ") &";
35
        } else {
36
            throw new Exception("Invalid command type");
37
        }
38
39 3
        shell_exec($cmd);
40
41 3
        return $tempfile;
42
    }
43
44
}