Passed
Push — master ( 1995be...2b4444 )
by Dmitriy
07:56 queued 04:33
created

BashCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 42
ccs 0
cts 23
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTitle() 0 3 1
A run() 0 24 3
A getDescription() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Inspector\Command;
6
7
use Symfony\Component\Process\Process;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Yii\Debug\Api\Inspector\CommandInterface;
10
use Yiisoft\Yii\Debug\Api\Inspector\CommandResponse;
11
12
final class BashCommand implements CommandInterface
13
{
14
    public function __construct(
15
        private Aliases $aliases,
16
        private array $command,
17
    ) {
18
    }
19
20
    public static function getTitle(): string
21
    {
22
        return 'Bash';
23
    }
24
25
    public static function getDescription(): string
26
    {
27
        return 'Runs any commands from the project root.';
28
    }
29
30
    public function run(): CommandResponse
31
    {
32
        $projectDirectory = $this->aliases->get('@root');
33
34
        $process = new Process($this->command);
35
36
        $process
37
            ->setWorkingDirectory($projectDirectory)
38
            ->setTimeout(null)
39
            ->run();
40
41
        $processOutput = $process->getOutput();
42
43
        if (!$process->getExitCode() > 1) {
44
            return new CommandResponse(
45
                status: CommandResponse::STATUS_FAIL,
46
                result: null,
47
                errors: array_filter([$processOutput, $process->getErrorOutput()]),
48
            );
49
        }
50
51
        return new CommandResponse(
52
            status: $process->isSuccessful() ? CommandResponse::STATUS_OK : CommandResponse::STATUS_ERROR,
53
            result: $processOutput
54
        );
55
    }
56
}
57