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

BashCommand::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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