Completed
Pull Request — master (#322)
by Mathieu
03:43
created

MigrateCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 6 1
A getOptions() 0 11 1
A handle() 0 18 3
A migrate() 0 19 3
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 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 Migrator($module))->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()]);
78
        }
79
    }
80
81
    /**
82
     * Get the console command arguments.
83
     *
84
     * @return array
85
     */
86 56
    protected function getArguments()
87
    {
88
        return array(
89 56
            array('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 56
    protected function getOptions()
99
    {
100
        return array(
101 56
            array('direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'),
102
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),
103
            array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'),
104
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
105
            array('seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'),
106
            array('subpath', null, InputOption::VALUE_OPTIONAL, 'Indicate a subpath to run your migrations from'),
107
        );
108
    }
109
}
110