Completed
Push — master ( cafb47...1e1424 )
by Nicolas
16s queued 11s
created

MigrateCommand::migrate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 19
ccs 0
cts 12
cp 0
crap 12
rs 9.6333
c 0
b 0
f 0
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\Contracts\RepositoryInterface
29
     */
30
    protected $module;
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return mixed
36
     */
37 View Code Duplication
    public function handle() : int
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
            $this->migrate($module);
47
48
            return 0;
49
        }
50
51
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
52
            $this->line('Running for module: <info>' . $module->getName() . '</info>');
53
54
            $this->migrate($module);
55
        }
56
57
        return 0;
58
    }
59
60
    /**
61
     * Run the migration from the specified module.
62
     *
63
     * @param Module $module
64
     */
65
    protected function migrate(Module $module)
66
    {
67
        $path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath());
68
69
        if ($this->option('subpath')) {
70
            $path = $path . "/" . $this->option("subpath");
71
        }
72
73
        $this->call('migrate', [
74
            '--path' => $path,
75
            '--database' => $this->option('database'),
76
            '--pretend' => $this->option('pretend'),
77
            '--force' => $this->option('force'),
78
        ]);
79
80
        if ($this->option('seed')) {
81
            $this->call('module:seed', ['module' => $module->getName()]);
82
        }
83
    }
84
85
    /**
86
     * Get the console command arguments.
87
     *
88
     * @return array
89
     */
90 128
    protected function getArguments()
91
    {
92
        return [
93 128
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
94
        ];
95
    }
96
97
    /**
98
     * Get the console command options.
99
     *
100
     * @return array
101
     */
102 128
    protected function getOptions()
103
    {
104
        return [
105 128
            ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'],
106
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
107
            ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
108
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
109
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
110
            ['subpath', null, InputOption::VALUE_OPTIONAL, 'Indicate a subpath to run your migrations from'],
111
        ];
112
    }
113
}
114