Completed
Pull Request — master (#11)
by
unknown
02:46
created

Run::errorMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MadWeb\Initializer;
4
5
use Illuminate\Console\Command;
6
use MadWeb\Initializer\Actions\Action;
7
use MadWeb\Initializer\Actions\Artisan;
8
use MadWeb\Initializer\Actions\Callback;
9
use MadWeb\Initializer\Actions\Dispatch;
10
use MadWeb\Initializer\Actions\External;
11
use MadWeb\Initializer\Actions\Publish;
12
use MadWeb\Initializer\Actions\PublishTag;
13
use MadWeb\Initializer\Contracts\Runner as RunnerContract;
14
15
class Run implements RunnerContract
16
{
17
    protected $artisanCommand;
18
19
    protected $options;
20
21
    private $errorMessages = [];
22
23
    public function __construct(Command $artisanCommand)
24
    {
25
        $this->artisanCommand = $artisanCommand;
26
        this->options = $artisanCommand->option('options');
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_OBJECT_OPERATOR
Loading history...
27
    }
28
29
    public function errorMessages(): array
30
    {
31
        return $this->errorMessages;
32
    }
33
34
    private function run(Action $action)
35
    {
36
        $action();
37
38
        if ($action->failed()) {
39
            $this->errorMessages[] = $action->errorMessage();
40
        }
41
42
        return $this;
43
    }
44
45
    public function doneWithErrors(): bool
46
    {
47
        return ! empty($this->errorMessages);
48
    }
49
50
    public function artisan(string $command, array $arguments = []): RunnerContract
51
    {
52
        return $this->run(new Artisan($this->artisanCommand, $command, $arguments));
53
    }
54
55
    public function publish($providers, bool $force = false): RunnerContract
56
    {
57
        return $this->run(new Publish($this->artisanCommand, $providers, $force));
58
    }
59
60
    public function publishForce($providers): RunnerContract
61
    {
62
        return $this->publish($providers, true);
63
    }
64
65
    public function publishTag($tag, bool $force = false): RunnerContract
66
    {
67
        return $this->run(new PublishTag($this->artisanCommand, $tag, $force));
68
    }
69
70
    public function publishTagForce($tag): RunnerContract
71
    {
72
        return $this->publishTag($tag, true);
73
    }
74
75
    public function external(string $command, ...$arguments): RunnerContract
76
    {
77
        return $this->run(new External($this->artisanCommand, $command, $arguments));
78
    }
79
80
    public function callable(callable $function, ...$arguments): RunnerContract
81
    {
82
        return $this->run(new Callback($this->artisanCommand, $function, $arguments));
83
    }
84
85
    public function dispatch($job): RunnerContract
86
    {
87
        return $this->run(new Dispatch($this->artisanCommand, $job));
88
    }
89
90
    public function dispatchNow($job): RunnerContract
91
    {
92
        return $this->run(new Dispatch($this->artisanCommand, $job, true));
93
    }
94
95
    public function getOption(string $option): bool
96
    {
97
        return in_array($option, $this->options);
98
    }
99
}
100