External   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 65
ccs 24
cts 32
cp 0.75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A title() 0 6 1
B run() 0 33 8
A createProcess() 0 8 2
1
<?php
2
3
namespace MadWeb\Initializer\Actions;
4
5
use Illuminate\Console\Command;
6
use RuntimeException;
7
use Symfony\Component\Process\Process;
8
9
class External extends Action
10
{
11
    private $command;
12
13
    private $arguments;
14
15 18
    public function __construct(Command $artisanCommand, string $command, array $arguments = [])
16
    {
17 18
        parent::__construct($artisanCommand);
18
19 18
        $this->artisanCommand = $artisanCommand;
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
20 18
        $this->command = $command;
21 18
        $this->arguments = $arguments;
22 18
    }
23
24 18
    public function title(): string
25
    {
26 18
        $argString = implode(' ', $this->arguments);
27
28 18
        return "<comment>Run external command:</comment> $this->command $argString";
29
    }
30
31 18
    public function run(): bool
32
    {
33 18
        $Process = $this->createProcess();
34 18
        $Process->setTimeout(null);
35
36 18
        $isVerbose = $this->artisanCommand->getOutput()->isVerbose();
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
37
38 18
        if ($isVerbose) {
39
            $this->artisanCommand->getOutput()->newLine();
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
40
            if (Process::isTtySupported()) {
41
                $Process->setTty(true);
42
            } elseif (Process::isPtySupported()) {
43
                $Process->setPty(true);
44
            }
45
        }
46
47
        $Process->run($isVerbose ? function ($type, $buffer) {
48
            if (Process::ERR === $type) {
49
                $this->artisanCommand->error($buffer);
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
            } else {
51
                $this->artisanCommand->line($buffer);
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
            }
53 18
        } : null);
54
55 18
        $error = $Process->getErrorOutput();
56 18
        $exitCode = $Process->getExitCode();
57
58 18
        if ($error and $exitCode > 0) {
59 6
            throw new RuntimeException(trim($error));
60
        }
61
62 12
        return ! $exitCode;
63
    }
64
65 18
    private function createProcess(): Process
66
    {
67 18
        if (empty($this->arguments)) {
68 12
            return Process::fromShellCommandline($this->command);
69
        }
70
71 6
        return new Process(array_merge([$this->command], $this->arguments));
72
    }
73
}
74