Completed
Pull Request — master (#911)
by Tran
02:33
created

LumenMigrateCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 99
loc 99
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 18 18 3
A migrate() 19 19 3
A getArguments() 6 6 1
A getOptions() 11 11 1

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\Migrations\LumenMigrator;
7
use Nwidart\Modules\Module;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11 View Code Duplication
class LumenMigrateCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module:lumen-migrate';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Migrate the migrations from the specified module or from all modules (For Lumen).';
26
27
    /**
28
     * @var \Nwidart\Modules\Contracts\RepositoryInterface
29
     */
30
    protected $module;
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return mixed
36
     */
37
    public function handle()
38
    {
39
        $this->module = $this->laravel['modules'];
40
41
        $name = $this->argument('module');
42
43
        if ($name) {
44
            $module = $this->module->findOrFail($name);
45
46
            return $this->migrate($module);
47
        }
48
49
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
50
            $this->line('Running for module: <info>' . $module->getName() . '</info>');
51
52
            $this->migrate($module);
53
        }
54
    }
55
56
    /**
57
     * Run the migration from the specified module.
58
     *
59
     * @param Module $module
60
     */
61
    protected function migrate(Module $module)
62
    {
63
        $path = str_replace(base_path(), '', (new LumenMigrator($module, $this->getLaravel()))->getPath());
64
65
        if ($this->option('subpath')) {
66
            $path = $path . "/" . $this->option("subpath");
67
        }
68
69
        $this->call('migrate', [
70
            '--path' => $path,
71
            '--database' => $this->option('database'),
72
            '--pretend' => $this->option('pretend'),
73
            '--force' => $this->option('force'),
74
        ]);
75
76
        if ($this->option('seed')) {
77
            $this->call('module:seed', ['module' => $module->getName()]);
0 ignored issues
show
Bug introduced by
Consider using $module->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
78
        }
79
    }
80
81
    /**
82
     * Get the console command arguments.
83
     *
84
     * @return array
85
     */
86
    protected function getArguments()
87
    {
88
        return [
89
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
90
        ];
91
    }
92
93
    /**
94
     * Get the console command options.
95
     *
96
     * @return array
97
     */
98
    protected function getOptions()
99
    {
100
        return [
101
            ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'],
102
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
103
            ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
104
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
105
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
106
            ['subpath', null, InputOption::VALUE_OPTIONAL, 'Indicate a subpath to run your migrations from'],
107
        ];
108
    }
109
}
110