Completed
Pull Request — master (#10)
by
unknown
03:47
created

AbstractInitializeCommand::handle()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.6276

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 20
cts 27
cp 0.7407
rs 8.425
c 0
b 0
f 0
cc 6
nc 8
nop 1
crap 6.6276

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 204
    public function handle(Container $container)
19
    {
20 204
        $initializerInstance = null;
21
22
        try {
23 204
            $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 204
        $config = $container->make('config');
33 204
        $env = $config->get($config->get('initializer.env_config_key'));
34
35 204
        $this->alert($this->title().' started');
36
37 204
        if (is_array($env)) {
38
39
            $this->error('Config not found in cache.');
40
            $this->error('$ php artisan config:cache');
41
42
            return 1;
43
        }
44
45
        $result = $initializerInstance
46 204
            ->{$this->option('root') ? $env.'Root' : $env}
47 204
            ($container->makeWith(Run::class, ['artisanCommand' => $this]));
48
49 192
        $this->output->newLine();
50
51 192
        if ($result->errorMessages()) {
52 12
            $this->line('<fg=red>'.$this->title().' done with errors:</>');
53
54 12
            $this->output->newLine();
55
56 12
            foreach ($result->errorMessages() as $message) {
57 12
                $this->error($message);
58 12
                $this->output->newLine();
59
            }
60
61 12
            $this->line('<fg=red>You could run command with <fg=cyan>-v</> flag to see more details</>');
62
63 12
            return 1;
64
        }
65
66 180
        $this->info($this->title().' done!');
67
68 180
        return 0;
69
    }
70
71
    /**
72
     * Returns initializer instance for current command.
73
     *
74
     * @return object
75
     */
76
    abstract protected function getInitializerInstance(Container $container);
77
78
    abstract protected function title(): string;
79
}
80