Passed
Push — master ( 77392d...e0a3bd )
by Kirill
08:16 queued 11s
created

RollbackCommand::perform()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 9
nop 0
dl 0
loc 22
rs 9.2222
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Command\Migrate;
13
14
use Symfony\Component\Console\Input\InputOption;
15
16
final class RollbackCommand extends AbstractCommand
17
{
18
    protected const NAME        = 'migrate:rollback';
19
    protected const DESCRIPTION = 'Rollback one (default) or multiple migrations';
20
    protected const OPTIONS     = [
21
        ['all', 'a', InputOption::VALUE_NONE, 'Rollback all executed migrations'],
22
    ];
23
24
    /**
25
     * Perform command.
26
     */
27
    public function perform(): void
28
    {
29
        if (!$this->verifyEnvironment()) {
30
            //Making sure we can safely migrate in this environment
31
            return;
32
        }
33
34
        $this->migrator->configure();
35
36
        $found = false;
37
        $count = !$this->option('all') ? 1 : PHP_INT_MAX;
38
        while ($count > 0 && ($migration = $this->migrator->rollback())) {
39
            $found = true;
40
            $count--;
41
            $this->sprintf(
42
                "<info>Migration <comment>%s</comment> was successfully rolled back.</info>\n",
43
                $migration->getState()->getName()
44
            );
45
        }
46
47
        if (!$found) {
48
            $this->writeln('<fg=red>No executed migrations were found.</fg=red>');
49
        }
50
    }
51
}
52