CommandRunner   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 33
rs 10
c 0
b 0
f 0
ccs 13
cts 17
cp 0.7647
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A run() 0 22 4
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
}