Action::getArtisanCommnad()   A
last analyzed

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\Actions;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
8
abstract class Action
9
{
10
    protected const LOADING_TEXT = 'running';
11
12
    /** @var \Illuminate\Console\Command */
13
    private $artisanCommnad;
14
15
    private $failed = false;
16
17
    protected $errorMessage = null;
18
19 210
    public function __construct(Command $artisanCommnad)
20
    {
21 210
        $this->artisanCommnad = $artisanCommnad;
22 210
    }
23
24 198
    public function __invoke(): bool
25
    {
26
        $failed = ! $this->getArtisanCommnad()->task($this->title(), function () {
27
            try {
28 198
                return $this->run();
29 12
            } catch (Exception $e) {
30 12
                $this->errorMessage = get_class($e).': '.$e->getMessage();
31
32 12
                return false;
33
            }
34 198
        }, static::LOADING_TEXT.'...');
35
36 198
        $this->failed = $failed;
37
38 198
        return ! $failed;
39
    }
40
41 198
    public function failed(): bool
42
    {
43 198
        return $this->failed;
44
    }
45
46 18
    public function errorMessage(): ?string
47
    {
48 18
        return $this->errorMessage;
49
    }
50
51
    abstract public function title(): string;
52
53
    abstract public function run(): bool;
54
55 198
    public function getArtisanCommnad(): Command
56
    {
57 198
        return $this->artisanCommnad;
58
    }
59
}
60