Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

MigrateRollbackCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B execute() 0 21 6
1
<?php
2
namespace Wandu\Database\Migrator\Commands;
3
4
use Wandu\Console\Command;
5
use Wandu\Database\Migrator\MigrateManager;
6
7
class MigrateRollbackCommand extends Command
8
{
9
    /** @var string */
10
    protected $description = 'Run rollback.';
11
12
    /** @var array */
13
    protected $arguments = [
14
        'until?' => 'the migrate id for the rollback',
15
    ];
16
17
    /** @var \Wandu\Database\Migrator\MigrateManager */
18
    protected $manager;
19
20
    /**
21
     * @param \Wandu\Database\Migrator\MigrateManager $manager
22
     */
23
    public function __construct(MigrateManager $manager)
24
    {
25
        $this->manager = $manager;
26
    }
27
    
28
    public function execute()
29
    {
30
        $untilMigrationId = $this->input->getArgument('until');
31
        
32
        /** @var \Wandu\Database\Migrator\MigrationContainer[] $migrations */
33
        $migrations = array_reverse($this->manager->getMigrations());
34
35
        if (!count($migrations)) {
36
            $this->output->writeln("<comment>there is no migration to rollback.</comment>");
37
            return 2;
38
        }
39
        foreach ($migrations as $migration) {
40
            if (!$migration->isApplied()) {
41
                continue;
42
            }
43
            $this->manager->down($migration->getId());
44
            if ($migration->getId() === $untilMigrationId || !$untilMigrationId) {
45
                break;
46
            }
47
        }
48
    }
49
}
50