Completed
Push — master ( 9a1463...3718bd )
by Basenko
04:21
created

Action::title()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 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
    /** @var \Illuminate\Console\Command */
11
    private $artisanCommnad;
12
13
    private $failed = false;
14
15
    protected $errorMessage = null;
16
17 174
    public function __construct(Command $artisanCommnad)
18
    {
19 174
        $this->artisanCommnad = $artisanCommnad;
20 174
    }
21
22 168
    public function __invoke(): bool
23
    {
24
        $failed = ! $this->getArtisanCommnad()->task($this->title(), function () {
25
            try {
26 168
                return $this->run();
27 12
            } catch (Exception $e) {
28 12
                $this->errorMessage = get_class($e).': '.$e->getMessage();
29
30 12
                return false;
31
            }
32 168
        });
33
34 168
        $this->failed = $failed;
35
36 168
        return ! $failed;
37
    }
38
39 168
    public function failed(): bool
40
    {
41 168
        return $this->failed;
42
    }
43
44 12
    public function errorMessage(): string
45
    {
46 12
        return $this->errorMessage;
47
    }
48
49
    abstract public function title(): string;
50
51
    abstract public function run(): bool;
52
53 168
    public function getArtisanCommnad(): Command
54
    {
55 168
        return $this->artisanCommnad;
56
    }
57
}
58