| 1 | <?php |
||
| 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 | public function __construct(Command $artisanCommnad) |
||
| 20 | { |
||
| 21 | $this->artisanCommnad = $artisanCommnad; |
||
| 22 | } |
||
| 23 | |||
| 24 | public function __invoke(): bool |
||
| 25 | { |
||
| 26 | $failed = ! $this->getArtisanCommnad()->task($this->title(), function () { |
||
| 27 | try { |
||
| 28 | return $this->run(); |
||
| 29 | } catch (Exception $e) { |
||
| 30 | $this->errorMessage = get_class($e).': '.$e->getMessage(); |
||
| 31 | |||
| 32 | return false; |
||
| 33 | } |
||
| 34 | }, static::LOADING_TEXT.'...'); |
||
| 35 | |||
| 36 | $this->failed = $failed; |
||
| 37 | |||
| 38 | return ! $failed; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function failed(): bool |
||
| 42 | { |
||
| 43 | return $this->failed; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function errorMessage(): string |
||
| 47 | { |
||
| 48 | return $this->errorMessage; |
||
| 49 | } |
||
| 50 | |||
| 51 | abstract public function title(): string; |
||
| 52 | |||
| 53 | abstract public function run(): bool; |
||
| 54 | |||
| 55 | public function getArtisanCommnad(): Command |
||
| 56 | { |
||
| 57 | return $this->artisanCommnad; |
||
| 58 | } |
||
| 59 | } |
||
| 60 |