Completed
Push — master ( d43b57...61b043 )
by Randall
07:54 queued 04:32
created

MigrateCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
c 1
b 0
f 0
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A migrate() 0 17 3
A handle() 0 17 3
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Other;
4
5
use Illuminate\Console\Command;
6
use Rawilk\LaravelModules\Migrations\Migrator;
7
use Rawilk\LaravelModules\Module;
8
9
class MigrateCommand extends Command
10
{
11
    /** @var string */
12
    protected $signature = 'module:migrate
13
                            {module? : The name of the module to migrate}
14
                            {--d|direction=asc : The direction to fetch modules in (only applies when migrating all modules)}
15
                            {--database= : The database connection to use}
16
                            {--pretend : Dump the SQL queries that would be run}
17
                            {--force : Force the operation to run when in production}
18
                            {--seed : Indicates if the seed task should be re-run}
19
                            {--subpath= : Indicate a subpath to run your migrations from}';
20
21
    /** @var string */
22
    protected $description = 'Run the migrations from a specific module or all modules.';
23
24
    /** @var \Rawilk\LaravelModules\Contracts\Repository */
25
    protected $module;
26
27
    public function handle(): void
28
    {
29
        $this->module = $this->laravel['modules'];
30
31
        if ($name = $this->argument('module')) {
32
            $module = $this->module->findOrFail($name);
33
34
            $this->migrate($module);
35
36
            return;
37
        }
38
39
        /** @var \Rawilk\LaravelModules\Module $module */
40
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
41
            $this->line("Running migrations for module: <info>{$module->getName()}</info>");
42
43
            $this->migrate($module);
44
        }
45
    }
46
47
    private function migrate(Module $module): void
48
    {
49
        $path = str_replace(base_path(), '', (new Migrator($module, $this->getLaravel()))->getPath());
50
51
        if ($this->option('subpath')) {
52
            $path .= '/' . $this->option('subpath');
53
        }
54
55
        $this->call('migrate', [
56
            '--path'     => $path,
57
            '--database' => $this->option('database'),
58
            '--pretend'  => $this->option('pretend'),
59
            '--force'    => $this->option('force')
60
        ]);
61
62
        if ($this->option('seed')) {
63
            $this->call('module:seed', ['module' => $module->getName()]);
64
        }
65
    }
66
}
67