Completed
Push — master ( 1669b8...6b5da6 )
by Basenko
03:39
created

Executor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 86.84%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 67
ccs 33
cts 38
cp 0.8684
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A exec() 0 6 2
A artisan() 0 4 1
A callable() 0 7 1
A dispatch() 0 4 1
A dispatchNow() 0 4 1
A printJob() 0 8 2
A external() 0 18 5
1
<?php
2
3
namespace MadWeb\Initializer;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Container\Container;
7
use Symfony\Component\Process\Process;
8
use Illuminate\Contracts\Bus\Dispatcher;
9
use MadWeb\Initializer\Contracts\Executor as ExecutorContract;
10
11
class Executor implements ExecutorContract
12
{
13
    protected $artisanCommand;
14
15 138
    public function __construct(Command $artisanCommand)
16
    {
17 138
        $this->artisanCommand = $artisanCommand;
18 138
    }
19
20 132
    public function exec(array $commands)
21
    {
22 132
        foreach ($commands as $command) {
23 132
            $this->{$command['type']}($command['command'], $command['arguments']);
24
        }
25 132
    }
26
27 60
    public function artisan(string $command, array $arguments = [])
28
    {
29 60
        $this->artisanCommand->call($command, $arguments);
30 60
    }
31
32 12
    public function external(string $command, array $arguments = [])
33
    {
34 12
        $Process = new Process(empty($arguments) ? $command : array_merge([$command], $arguments));
0 ignored issues
show
Bug introduced by
It seems like empty($arguments) ? $com...($command), $arguments) can also be of type string; however, Symfony\Component\Process\Process::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35 12
        $Process->setTimeout(null);
36
37 12
        if (Process::isTtySupported()) {
38 12
            $Process->setTty(true);
39
        } elseif (Process::isPtySupported()) {
40
            $Process->setPty(true);
41
        }
42
        $Process->run(function ($type, $buffer) {
43
            if (Process::ERR === $type) {
44
                $this->artisanCommand->error($buffer);
45
            } else {
46
                $this->artisanCommand->line($buffer);
47
            }
48 12
        });
49 12
    }
50
51 6
    public function callable(callable $function, array $arguments = [])
52
    {
53 6
        call_user_func($function, ...$arguments);
54
55 6
        is_callable($function, false, $name);
56 6
        $this->artisanCommand->info("Callable: $name called");
57 6
    }
58
59 48
    public function dispatch($job)
60
    {
61 48
        $this->printJob($job, Container::getInstance()->make(Dispatcher::class)->dispatch($job));
62 48
    }
63
64 12
    public function dispatchNow($job)
65
    {
66 12
        $this->printJob($job, Container::getInstance()->make(Dispatcher::class)->dispatchNow($job));
67 12
    }
68
69 54
    protected function printJob($job, $result)
70
    {
71 54
        $message = 'Job "'.get_class($job).'" has been processed';
72
73 54
        $message .= is_string($result) ? '. Result: '.$result : '';
74
75 54
        $this->artisanCommand->info($message);
76 54
    }
77
}
78