ExecCommand::getToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Commands;
4
5
final class ExecCommand implements Command
6
{
7 1
    public function getToken(): string
8
    {
9 1
        return 'exec';
10
    }
11
12 3
    public function execute(CommandParams $params, CommandExecutionContext $executionContext): string
13
    {
14 3
        $fileName = $params->getFirstArgument();
15
16 3
        if (!$executionContext->hasFileInWorkingDirectory($fileName)) {
17
            throw new ExecutionFailedException("File '$fileName' does not exist.");
18
        }
19
20 3
        $path = $executionContext->getPathOfFileInWorkingDirectory($fileName);
21
22 3
        $cmd = sprintf('php %s 2>&1', escapeshellarg($path));
23 3
        $output = shell_exec($cmd);
24
25 3
        return $this->wrapOutput($output);
26
    }
27
    
28 3
    private function wrapOutput(string $output): string
29
    {
30 3
        $lines = explode("\n", $output);
31
32 3
        $indented = array_map(function ($e) {
33 3
            return '    '.$e;
34 3
        }, $lines);
35
36 3
        return implode("\n", $indented);
37
    }
38
}
39