ExecCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2.0078
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