AbstractInitializeCommand::handle()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.1429

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 24
cts 28
cp 0.8571
rs 8.1138
c 0
b 0
f 0
cc 7
nc 7
nop 1
crap 7.1429

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 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 210
        $runner = $container->makeWith(Run::class, ['artisanCommand' => $this]);
38
39 198
        $initializerInstance->{$this->option('root') ? $env.'Root' : $env}($runner);
40
41 198
        $this->output->newLine();
42
43 198
        if ($runner->doneWithErrors()) {
44 18
            $errorMessages = $runner->errorMessages();
45
46 18
            $this->line(
47 18
                '<fg=red>'.$this->title().' done with errors'.
48 18
                (! empty($errorMessages) ? ':' : '.').
49 18
                '</>'
50
            );
51
52 18
            if (! empty($errorMessages)) {
53 12
                $this->output->newLine();
54
55 12
                foreach ($runner->errorMessages() as $message) {
56 12
                    $this->error($message);
57 12
                    $this->output->newLine();
58
                }
59
            }
60
61 18
            $this->line('<fg=red>You could run command with <fg=cyan>-v</> flag to see more details</>');
62
63 18
            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