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

MigrateResetCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Traits\MigrationLoaderTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class MigrateResetCommand extends Command
12
{
13
    use MigrationLoaderTrait;
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'module:migrate-reset';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Reset 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(): void
38
    {
39
        $this->module = $this->laravel['modules'];
40
41
        $name = $this->argument('module');
42
43
        if (!empty($name)) {
44
            $this->resetOne($name);
45
46
            return;
47
        }
48
49
        $this->resetMany($this->module->getOrdered($this->option('direction')));
50
    }
51
52
    /**
53
     * Rollback migration from the specified module.
54
     *
55
     * @param $module
56
     */
57
    public function resetOne($module): void
58
    {
59
        if (is_string($module)) {
60
            $module = $this->module->findOrFail($module);
61
        }
62
63
        $path = str_replace(base_path(), '', (new Migrator($module))->getPath());
64
65
        $this->call('migrate:reset', [
66
            '--path' => $path,
67
            '--database' => $this->option('database'),
68
            '--pretend' => $this->option('pretend'),
69
            '--force' => $this->option('force'),
70
        ]);
71
    }
72
73
    /**
74
     * Rollback migration from the all the specified modules.
75
     *
76
     * @param array $modules
77
     */
78
    private function resetMany(array $modules): void
79
    {
80
        $paths = [];
81
        foreach ($modules as $module) {
82
            $path = str_replace(base_path(), '', (new Migrator($module))->getPath());
83
            $paths[] = $path;
84
        }
85
86
        $this->call('migrate:reset', [
87
            '--path' => $paths,
88
            '--database' => $this->option('database'),
89
            '--pretend' => $this->option('pretend'),
90
            '--force' => $this->option('force'),
91
        ]);
92
    }
93
94
    /**
95
     * Get the console command arguments.
96
     *
97
     * @return array
98
     */
99 99
    protected function getArguments()
100
    {
101
        return [
102 99
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
103
        ];
104
    }
105
106
    /**
107
     * Get the console command options.
108
     *
109
     * @return array
110
     */
111 99
    protected function getOptions()
112
    {
113
        return [
114 99
            ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'],
115
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
116
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
117
            ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
118
        ];
119
    }
120
}
121