Completed
Push — master ( aed5e6...e7b04e )
by Fumio
01:47
created

DatabaseRollbackCommand::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Jumilla\Versionia\Laravel\Commands;
4
5
use UnexpectedValueException;
6
use Illuminate\Console\Command;
7
use Illuminate\Console\ConfirmableTrait;
8
use Jumilla\Versionia\Laravel\Migrator;
9
10
class DatabaseRollbackCommand extends Command
11
{
12
    use DatabaseCommandTrait;
13
    use ConfirmableTrait;
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'database:rollback
21
        {group : The name of the migration group.}
22
        {--all : Includes all version.}
23
        {--force : Force the operation to run when in production.}
24
    ';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Database migrate to back to one or specifed version';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
37
     *
38
     * @return mixed
39
     */
40 7
    public function handle(Migrator $migrator)
41
    {
42 7
        if (!$this->confirmToProceed()) {
43 1
            return;
44
        }
45
46 6
        $group = $this->argument('group');
47
48
        // check valid group
49 6
        if (!in_array($group, $migrator->migrationGroups())) {
50 2
            throw new UnexpectedValueException("Migation group '$group' is not defined.");
51
        }
52
53 4
        $version = $this->option('all') ? Migrator::VERSION_NULL : $migrator->migrationLatestVersion($group);
0 ignored issues
show
Bug introduced by
It seems like $group defined by $this->argument('group') on line 46 can also be of type array; however, Jumilla\Versionia\Larave...igrationLatestVersion() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
55 4
        $migrator->makeLogTable();
56
57 4
        $this->doRollback($migrator, $group, $version);
0 ignored issues
show
Bug introduced by
It seems like $group defined by $this->argument('group') on line 46 can also be of type array; however, Jumilla\Versionia\Larave...ckCommand::doRollback() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58 4
    }
59
60
    /**
61
     * Execute rollback.
62
     *
63
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
64
     * @param string                              $target_group
65
     * @param string                              $target_version
66
     */
67 4
    protected function doRollback(Migrator $migrator, $target_group, $target_version)
68
    {
69 4
        $installed_migrations = $migrator->installedMigrationsByDesc();
70
71 4
        if (!isset($installed_migrations[$target_group])) {
72 1
            $this->info("Nothing migrations for group '$target_group'.");
73
74 1
            return;
75
        }
76
77 3
        foreach ($installed_migrations[$target_group] as $data) {
78
            // check version
79 3
            if ($migrator->compareMigrationVersion($data->version, $target_version) < 0) {
80 1
                continue;
81
            }
82
83 3
            $this->infoDowngrade($target_group, $data->version, $data->class);
84
85 3
            $migrator->doDowngrade($target_group, $data->version);
86
        }
87 3
    }
88
}
89