Completed
Push — master ( 898087...9eb657 )
by Nicolas
15s queued 11s
created

MigrateRefreshCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 8.79 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 14.81%

Importance

Changes 0
Metric Value
dl 8
loc 91
ccs 4
cts 27
cp 0.1481
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 30 4
A getArguments() 0 6 1
A getOptions() 8 8 1
A getModuleName() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Traits\ModuleCommandTrait;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class MigrateRefreshCommand extends Command
11
{
12
    use ModuleCommandTrait;
13
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'module:migrate-refresh';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Rollback & re-migrate the modules migrations.';
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle() : int
32
    {
33
        $module = $this->argument('module');
34
35
        if ($module && !$this->getModuleName()) {
36
            $this->error("Module [$module] does not exists.");
37
38
            return E_ERROR;
39
        }
40
41
        $this->call('module:migrate-reset', [
42
            'module' => $this->getModuleName(),
43
            '--database' => $this->option('database'),
44
            '--force' => $this->option('force'),
45
        ]);
46
47
        $this->call('module:migrate', [
48
            'module' => $this->getModuleName(),
49
            '--database' => $this->option('database'),
50
            '--force' => $this->option('force'),
51
        ]);
52
53
        if ($this->option('seed')) {
54
            $this->call('module:seed', [
55
                'module' => $this->getModuleName(),
56
            ]);
57
        }
58
59
        return 0;
60
    }
61
62
    /**
63
     * Get the console command arguments.
64
     *
65
     * @return array
66
     */
67 128
    protected function getArguments()
68
    {
69
        return [
70 128
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
71
        ];
72
    }
73
74
    /**
75
     * Get the console command options.
76
     *
77
     * @return array
78
     */
79 128 View Code Duplication
    protected function getOptions()
80
    {
81
        return [
82 128
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
83
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
84
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
85
        ];
86
    }
87
88
    public function getModuleName()
89
    {
90
        $module = $this->argument('module');
91
92
        if (!$module) {
93
            return null;
94
        }
95
96
        $module = app('modules')->find($module);
97
98
        return $module ? $module->getStudlyName() : null;
99
    }
100
}
101