Completed
Push — master ( f78699...2a9ef0 )
by Basenko
04:26
created

Run::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    private $errorMessages = [];
20
21
    private $doneWithErrors = false;
22
23 210
    public function __construct(Command $artisanCommand)
24
    {
25 210
        $this->artisanCommand = $artisanCommand;
26 210
    }
27
28 18
    public function errorMessages(): array
29
    {
30 18
        return $this->errorMessages;
31
    }
32
33 210
    private function run(Action $action)
34
    {
35 210
        $action();
36
37 198
        if ($action->failed()) {
38 18
            if (! $this->doneWithErrors) {
39 18
                $this->doneWithErrors = true;
40
            }
41
42 18
            if ($message = $action->errorMessage()) {
43 12
                $this->errorMessages[] = $message;
44
            }
45
        }
46
47 198
        return $this;
48
    }
49
50 198
    public function doneWithErrors(): bool
51
    {
52 198
        return $this->doneWithErrors;
53
    }
54
55 24
    public function artisan(string $command, array $arguments = []): RunnerContract
56
    {
57 24
        return $this->run(new Artisan($this->artisanCommand, $command, $arguments));
58
    }
59
60 72
    public function publish($providers, bool $force = false): RunnerContract
61
    {
62 72
        return $this->run(new Publish($this->artisanCommand, $providers, $force));
63
    }
64
65 6
    public function publishForce($providers): RunnerContract
66
    {
67 6
        return $this->publish($providers, true);
68
    }
69
70 24
    public function publishTag($tag, bool $force = false): RunnerContract
71
    {
72 24
        return $this->run(new PublishTag($this->artisanCommand, $tag, $force));
73
    }
74
75 12
    public function publishTagForce($tag): RunnerContract
76
    {
77 12
        return $this->publishTag($tag, true);
78
    }
79
80 18
    public function external(string $command, ...$arguments): RunnerContract
81
    {
82 18
        return $this->run(new External($this->artisanCommand, $command, $arguments));
83
    }
84
85 12
    public function callable(callable $function, ...$arguments): RunnerContract
86
    {
87 12
        return $this->run(new Callback($this->artisanCommand, $function, $arguments));
88
    }
89
90 48
    public function dispatch($job): RunnerContract
91
    {
92 48
        return $this->run(new Dispatch($this->artisanCommand, $job));
93
    }
94
95 18
    public function dispatchNow($job): RunnerContract
96
    {
97 18
        return $this->run(new Dispatch($this->artisanCommand, $job, true));
98
    }
99
}
100