Completed
Pull Request — master (#46)
by John
03:51
created

MigrateCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 17 3
A migrate() 0 14 2
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 Nwidart\Modules\Migrations\Migrator;
7
use Nwidart\Modules\Module;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class MigrateCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module: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.';
26
27
    /**
28
     * @var \Nwidart\Modules\Repository
29
     */
30
    protected $module;
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return mixed
36
     */
37
    public function fire()
38
    {
39
        $this->module = $this->laravel['modules'];
40
41
        $name = $this->argument('module');
42
43
        if ($name) {
44
            $module = $this->module->findOrFail($name);
45
            return $this->migrate($module);
46
        }
47
48
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
49
            $this->line('Running for module: <info>' . $module->getName() . '</info>');
50
51
            $this->migrate($module);
52
        }
53
    }
54
55
    /**
56
     * Run the migration from the specified module.
57
     *
58
     * @param Module $module
59
     *
60
     * @return mixed
61
     */
62
    protected function migrate(Module $module)
63
    {
64
        $path = str_replace(base_path(), '', (new Migrator($module))->getPath());
65
        $this->call('migrate', [
66
            '--path' => $path,
67
            '--database' => $this->option('database'),
68
            '--pretend' => $this->option('pretend'),
69
            '--force' => $this->option('force'),
70
        ]);
71
72
        if ($this->option('seed')) {
73
            $this->call('module:seed', ['module' => $module->getName()]);
74
        }
75
    }
76
77
    /**
78
     * Get the console command arguments.
79
     *
80
     * @return array
81
     */
82 38
    protected function getArguments()
83
    {
84
        return array(
85 38
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
86
        );
87
    }
88
89
    /**
90
     * Get the console command options.
91
     *
92
     * @return array
93
     */
94 38
    protected function getOptions()
95
    {
96
        return array(
97 38
            array('direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'),
98
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),
99
            array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'),
100
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
101
            array('seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'),
102
        );
103
    }
104
}
105