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

AbstractInitializeCommand::getOptionsConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace MadWeb\Initializer\Console\Commands;
4
5
use ErrorException;
6
use Illuminate\Console\Command;
7
use Illuminate\Contracts\Container\Container;
8
use MadWeb\Initializer\Contracts\Runner as ExecutorContract;
9
use MadWeb\Initializer\Run;
10
11
abstract class AbstractInitializeCommand extends Command
12
{
13
    /**
14
     * Execute the console command.
15
     *
16
     * @return void
17
     */
18 210
    public function handle(Container $container)
19
    {
20 210
        $initializerInstance = null;
21
22
        try {
23 210
            $initializerInstance = $this->getInitializerInstance($container);
24
        } catch (ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
25
            $this->error('Publish initializer classes:');
26
            $this->error('$ php artisan vendor:publish --tag=initializers');
27
28
            return 1;
29
        }
30
31
        /** @var ExecutorContract $Executor */
32 210
        $config = $container->make('config');
33 210
        $env = $config->get($config->get('initializer.env_config_key'));
34
35 210
        $this->alert($this->title().' started');
36
37
        $result = $initializerInstance
38 210
            ->{$this->option('root') ? $env.'Root' : $env}
39 210
            ($container->makeWith(Run::class, ['artisanCommand' => $this, 'options' => $this->option('options')]));
40
41 198
        $this->output->newLine();
42
43 198
        if ($result->errorMessages()) {
44 12
            $this->line('<fg=red>'.$this->title().' done with errors:</>');
45
46 12
            $this->output->newLine();
47
48 12
            foreach ($result->errorMessages() as $message) {
49 12
                $this->error($message);
50 12
                $this->output->newLine();
51
            }
52
53 12
            $this->line('<fg=red>You could run command with <fg=cyan>-v</> flag to see more details</>');
54
55 12
            return 1;
56
        }
57
58 186
        $this->info($this->title().' done!');
59
60 186
        return 0;
61
    }
62
63
    /**
64
     * Returns allowed options.
65
     *
66
     * @return string
67
     */
68
    protected function getOptionsConfig(Container $container)
69
    {
70
        $config = $container->make('config');
71
        $options = $config->get($config->get('initializer.options'));
72
73
        $options = array_keys($options);
74
75
        if(count($options) > 0) {
76
            return '. Allowed options:[' . implode(', ', $options) . ']';
77
        }
78
79
        return;
80
    }
81
82
    /**
83
     * Returns initializer instance for current command.
84
     *
85
     * @return object
86
     */
87
    abstract protected function getInitializerInstance(Container $container);
88
89
    abstract protected function title(): string;
90
}
91