Completed
Push — master ( d0bf09...15e02a )
by Nicolas
04:05
created

MigrateCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 107
ccs 4
cts 4
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 16 3
A migrate() 0 15 2
A getPath() 0 6 1
A getArguments() 0 6 1
A getOptions() 0 10 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class MigrateCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'module:migrate';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Migrate the migrations from the specified module or from all modules.';
24
25
    /**
26
     * @var \Nwidart\Modules\Repository
27
     */
28
    protected $module;
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return mixed
34
     */
35
    public function fire()
36
    {
37
        $this->module = $this->laravel['modules'];
38
39
        $name = $this->argument('module');
40
41
        if ($name) {
42
            return $this->migrate($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->argument('module') on line 39 can also be of type array; however, Nwidart\Modules\Commands\MigrateCommand::migrate() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
        }
44
45
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
46
            $this->line('Running for module: <info>'.$module->getName().'</info>');
47
48
            $this->migrate($module);
49
        }
50
    }
51
52
    /**
53
     * Run the migration from the specified module.
54
     *
55
     * @param string $name
56
     *
57
     * @return mixed
58
     */
59
    protected function migrate($name)
60
    {
61
        $module = $this->module->findOrFail($name);
62
63
        $this->call('migrate', [
64
            '--path' => $this->getPath($module),
65
            '--database' => $this->option('database'),
66
            '--pretend' => $this->option('pretend'),
67
            '--force' => $this->option('force'),
68
        ]);
69
70
        if ($this->option('seed')) {
71
            $this->call('module:seed', ['module' => $name]);
72
        }
73
    }
74
75
    /**
76
     * Get migration path for specific module.
77
     *
78
     * @param  \Nwidart\Modules\Module $module
79
     * @return string
80
     */
81
    protected function getPath($module)
82
    {
83
        $path = $module->getExtraPath(config('modules.paths.generator.migration'));
84
85
        return str_replace(base_path(), '', $path);
86
    }
87
88
    /**
89
     * Get the console command arguments.
90
     *
91
     * @return array
92
     */
93 3
    protected function getArguments()
94
    {
95
        return array(
96 3
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
97
        );
98
    }
99
100
    /**
101
     * Get the console command options.
102
     *
103
     * @return array
104
     */
105 3
    protected function getOptions()
106
    {
107
        return array(
108 3
            array('direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'),
109
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),
110
            array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'),
111
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
112
            array('seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'),
113
        );
114
    }
115
}
116