Completed
Push — master ( 890702...fa33d5 )
by Nicolas
06:41
created

MigrateRollbackCommand::rollback()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 13

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 26
loc 26
ccs 0
cts 14
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 12
nop 1
crap 30
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 View Code Duplication
class MigrateRollbackCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
     * Execute the console command.
31
     *
32
     * @return mixed
33
     */
34
    public function fire()
35
    {
36
        $module = $this->argument('module');
37
38
        if (!empty($module)) {
39
            $this->rollback($module);
40
41
            return;
42
        }
43
44
        foreach (array_reverse($this->laravel['modules']->all()) as $module) {
45
            $this->line('Running for module: <info>' . $module->getName() . '</info>');
46
47
            $this->rollback($module);
48
        }
49
    }
50
51
    /**
52
     * Rollback migration from the specified module.
53
     *
54
     * @param $module
55
     */
56
    public function rollback($module)
57
    {
58
        if (is_string($module)) {
59
            $module = $this->laravel['modules']->findOrFail($module);
60
        }
61
62
        $migrator = new Migrator($module);
63
64
        $database = $this->option('database');
65
66
        if (!empty($database)) {
67
            $migrator->setDatabase($database);
68
        }
69
70
        $migrated = $migrator->rollback();
71
72
        if (count($migrated)) {
73
            foreach ($migrated as $migration) {
74
                $this->line("Rollback: <info>{$migration}</info>");
75
            }
76
77
            return;
78
        }
79
80
        $this->comment('Nothing to rollback.');
81
    }
82
83
    /**
84
     * Get the console command arguments.
85
     *
86
     * @return array
87
     */
88 43
    protected function getArguments()
89
    {
90
        return array(
91 43
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
92
        );
93
    }
94
95
    /**
96
     * Get the console command options.
97
     *
98
     * @return array
99
     */
100 43
    protected function getOptions()
101
    {
102
        return array(
103 43
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),
104
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
105
            array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'),
106
        );
107
    }
108
}
109