MigrateResetCommand::reset()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 13

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 12
nop 1
1
<?php
2
3
namespace Consigliere\Components\Commands;
4
5
use Illuminate\Console\Command as ComponentCommand;
6
use Consigliere\Components\Migrations\Migrator;
7
use Consigliere\Components\Traits\MigrationLoaderTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11 View Code Duplication
class MigrateResetCommand extends ComponentCommand
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 = 'component:migrate-reset';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Reset the components migrations.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return mixed
33
     */
34
    public function fire()
35
    {
36
        $component = $this->argument('component');
37
38
        if (!empty($component)) {
39
            $this->reset($component);
40
41
            return;
42
        }
43
44
        foreach (array_reverse($this->laravel['components']->all()) as $component) {
45
            $this->line('Running for component: <info>' . $component->getName() . '</info>');
46
47
            $this->reset($component);
48
        }
49
    }
50
51
    /**
52
     * Rollback migration from the specified component.
53
     *
54
     * @param $component
55
     */
56
    public function reset($component)
57
    {
58
        if (is_string($component)) {
59
            $component = $this->laravel['components']->findOrFail($component);
60
        }
61
62
        $migrator = new Migrator($component);
63
64
        $database = $this->option('database');
65
66
        if (!empty($database)) {
67
            $migrator->setDatabase($database);
68
        }
69
70
        $migrated = $migrator->reset();
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
    protected function getArguments()
89
    {
90
        return [
91
            ['component', InputArgument::OPTIONAL, 'The name of component will be used.'],
92
        ];
93
    }
94
95
    /**
96
     * Get the console command options.
97
     *
98
     * @return array
99
     */
100
    protected function getOptions()
101
    {
102
        return [
103
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
104
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
105
            ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
106
        ];
107
    }
108
}
109