MigrateRollbackCommand::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Other;
4
5
use Illuminate\Console\Command;
6
use Rawilk\LaravelModules\Migrations\Migrator;
7
8
class MigrateRollbackCommand extends Command
9
{
10
    /** @var string */
11
    protected $signature = 'module:migrate-rollback
12
                            {module? : The name of the module to rollback migrations for}
13
                            {--d|direction=desc : The direction to sort modules}
14
                            {--force : Force the operation to run when in production}
15
                            {--pretend : Dump the SQL queries that would be run}';
16
17
    /** @var string */
18
    protected $description = 'Rollback migrations for a module.';
19
20
    /** @var \Rawilk\LaravelModules\Contracts\Repository */
21
    protected $module;
22
23
    public function handle(): void
24
    {
25
        $this->module = $this->laravel['modules'];
26
27
        if ($name = $this->argument('module')) {
28
            $this->rollback($name);
29
30
            return;
31
        }
32
33
        /** @var \Rawilk\LaravelModules\Module $module */
34
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
35
            $this->line("Rolling back migrations for module: <info>{$module->getName()}</info>");
36
37
            $this->rollback($module);
38
        }
39
    }
40
41
    private function rollback($module): void
42
    {
43
        if (is_string($module)) {
44
            $module = $this->module->findOrFail($module);
45
        }
46
47
        $migrator = new Migrator($module, $this->getLaravel());
48
49
        if ($database = $this->option('database')) {
50
            $migrator->setDatabase($database);
51
        }
52
53
        $migrated = $migrator->rollback();
54
55
        if (count($migrated) > 0) {
56
            foreach ($migrated as $migration) {
57
                $this->line("Migration rolled back: <info>{$migration}</info>");
58
            }
59
60
            return;
61
        }
62
63
        $this->comment('Nothing to rollback.');
64
    }
65
}
66