Completed
Pull Request — master (#1032)
by
unknown
05:37
created

MigrateRefreshCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 8.51 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 13.79%

Importance

Changes 0
Metric Value
dl 8
loc 94
ccs 4
cts 29
cp 0.1379
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 29 4
A getArguments() 0 6 1
A getOptions() 8 8 1
A getModuleName() 0 16 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
            return E_ERROR;
38
        }
39
40
        $this->call('module:migrate-reset', [
41
            'module' => $this->getModuleName(),
42
            '--database' => $this->option('database'),
43
            '--force' => $this->option('force'),
44
        ]);
45
46
        $this->call('module:migrate', [
47
            'module' => $this->getModuleName(),
48
            '--database' => $this->option('database'),
49
            '--force' => $this->option('force'),
50
        ]);
51
52
        if ($this->option('seed')) {
53
            $this->call('module:seed', [
54
                'module' => $this->getModuleName(),
55
            ]);
56
        }
57
58
        return 0;
59
    }
60
61
    /**
62
     * Get the console command arguments.
63
     *
64
     * @return array
65
     */
66 128
    protected function getArguments()
67
    {
68
        return [
69 128
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
70
        ];
71
    }
72
73
    /**
74
     * Get the console command options.
75
     *
76
     * @return array
77
     */
78 128 View Code Duplication
    protected function getOptions()
79
    {
80
        return [
81 128
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
82
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
83
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
84
        ];
85
    }
86
87
    public function getModuleName()
88
    {
89
        $module = $this->argument('module');
90
91
        if (!$module) {
92
            return null;
93
        }
94
95
        $module = app('modules')->find($module);
96
97
        if (!$module) {
98
            return null;
99
        }
100
101
        return $module->getStudlyName();
102
    }
103
}
104