MigrateRollbackCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 98
loc 98
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 16 16 3
B rollback() 26 26 5
A getArguments() 6 6 1
A getOptions() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 MigrateRollbackCommand 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-rollback';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Rollback 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->rollback($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->rollback($component);
48
        }
49
    }
50
51
    /**
52
     * Rollback migration from the specified component.
53
     *
54
     * @param $component
55
     */
56
    public function rollback($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->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
    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