Completed
Pull Request — master (#758)
by Nicolas
03:30
created

MigrateRollbackCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 4
cts 24
cp 0.1666
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 3
A rollback() 0 15 2
A getArguments() 0 6 1
A getOptions() 0 9 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\Traits\MigrationLoaderTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class MigrateRollbackCommand extends Command
12
{
13
    use MigrationLoaderTrait;
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'module:migrate-rollback';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Rollback the modules migrations.';
28
29
    /**
30
     * @var \Nwidart\Modules\Contracts\RepositoryInterface
31
     */
32
    protected $module;
33
34
    /**
35
     * Execute the console command.
36
     */
37
    public function handle()
38
    {
39
        $this->module = $this->laravel['modules'];
40
41
        $name = $this->argument('module');
42
43
        if (!empty($name)) {
44
            $this->rollback($name);
45
46
            return;
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->rollback($module);
53
        }
54
    }
55
56
    /**
57
     * Rollback migration from the specified module.
58
     *
59
     * @param $module
60
     */
61
    public function rollback($module)
62
    {
63
        if (is_string($module)) {
64
            $module = $this->module->findOrFail($module);
65
        }
66
67
        $path = str_replace(base_path(), '', (new Migrator($module))->getPath());
68
69
        $this->call('migrate:rollback', [
70
            '--path' => $path,
71
            '--database' => $this->option('database'),
72
            '--pretend' => $this->option('pretend'),
73
            '--force' => $this->option('force'),
74
        ]);
75
    }
76
77
    /**
78
     * Get the console command arguments.
79
     *
80
     * @return array
81
     */
82 99
    protected function getArguments()
83
    {
84
        return [
85 99
            ['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 99
    protected function getOptions()
95
    {
96
        return [
97 99
            ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'],
98
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
99
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
100
            ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
101
        ];
102
    }
103
}
104