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

DatabaseAgainCommand::doAgain()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 5
nop 2
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 DatabaseAgainCommand 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:again
21
        {group : Group name}
22
        {--seed= : Indicates if the seed task should be re-run.}
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 = 'Migrations rollback latest version and upgrade';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
37
     *
38
     * @return mixed
39
     */
40 8
    public function handle(Migrator $migrator)
41
    {
42 8
        if (!$this->confirmToProceed()) {
43 1
            return;
44
        }
45
46 7
        $group = $this->argument('group');
47
48
        // check valid group
49 7
        if (!in_array($group, $migrator->migrationGroups())) {
50 2
            throw new UnexpectedValueException("Migation group '$group' is not defined.");
51
        }
52
53 5
        $migrator->makeLogTable();
54
55 5
        $this->doAgain($migrator, $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...AgainCommand::doAgain() 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...
56
57 5
        $seed = $this->option('seed');
58
59 5
        if ($seed) {
60 1
            $this->call('database:seed', ['name' => $seed, '--force' => true]);
61
        }
62 5
    }
63
64
    /**
65
     * Execute rollback and upgrade one version.
66
     *
67
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
68
     * @param string                              $group
69
     */
70 5
    protected function doAgain(Migrator $migrator, $group)
71
    {
72
        // retreive installed versions
73 5
        $installed_migrations = $migrator->installedLatestMigrations();
74
75 5
        $installed_version = data_get($installed_migrations, $group.'.version', Migrator::VERSION_NULL);
76
77 5
        $definition_versions = $migrator->migrationVersionsByDesc($group);
78
79 5
        if (!$this->checkInstalledVersion($installed_version, $definition_versions)) {
80 3
            return;
81
        }
82
83
        // remove migration log
84 2
        $definition_latest_version = array_get(array_keys($definition_versions), 0, Migrator::VERSION_NULL);
85 2
        if ($migrator->compareMigrationVersion($installed_version, $definition_latest_version) >= 0) {
86 2
            $this->line("<info>Remove log [$group/$installed_version]</info>");
87 2
            $migrator->removeMigrationLog($group, $installed_version);
88
        }
89
90
        // downgrade & upgrade
91 2
        foreach ($definition_versions as $version => $class) {
92 2
            $this->infoDowngrade($group, $version, $class);
93
94 2
            $migrator->doDowngrade($group, $version, $class);
95
96 2
            $this->infoUpgrade($group, $version, $class);
97
98 2
            $migrator->doUpgrade($group, $version, $class);
99
100 2
            break;
101
        }
102 2
    }
103
104
    /**
105
     * Check installed version.
106
     *
107
     * @param string $installed_version
108
     * @param array  $definition_versions
109
     *
110
     * @return bool
111
     */
112 5
    protected function checkInstalledVersion($installed_version, array $definition_versions)
113
    {
114 5
        if ($installed_version === null) {
115 1
            $this->error('Nothing installed version.');
116 1
            $this->line('Please run <comment>database:upgrade<comment>.');
117
118 1
            return false;
119
        }
120
121 4
        $index = array_search($installed_version, array_keys($definition_versions));
122
123 4
        if ($index === false) {
124 1
            $versions = json_encode(array_keys($definition_versions));
125 1
            $this->error("Installed version '{$installed_version}' was not found in definition {$versions}.");
126
127 1
            return false;
128
        }
129
130 3
        if ($index >= 2) {
131 1
            $this->error("Installed version '$installed_version' was older.");
132 1
            $this->line('Please run <comment>database:upgrade<comment>.');
133
134 1
            return false;
135
        }
136
137 2
        return true;
138
    }
139
}
140