RollbackCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B execute() 0 22 6
1
<?php
2
namespace Wandu\Migrator\Commands;
3
4
use Wandu\Console\Command;
5
use Wandu\Migrator\Migrator;
6
7
class RollbackCommand 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\Migrator\Migrator */
18
    protected $manager;
19
20
    /**
21
     * @param \Wandu\Migrator\Migrator $manager
22
     */
23
    public function __construct(Migrator $manager)
24
    {
25
        $this->manager = $manager;
26
    }
27
    
28
    public function execute()
29
    {
30
        $untilMigrationId = $this->input->getArgument('until');
31
        
32
        /** @var \Wandu\Migrator\FileMigrationInformation[] $migrations */
33
        $migrations = array_reverse($this->manager->getMigrationInformations());
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 (!$this->manager->isApplied($migration)) {
41
                continue;
42
            }
43
            $this->manager->down($migration->getId());
44
            $this->output->writeln(sprintf("<info>down</info> %s", $migration->getId()));
45
            if ($migration->getId() === $untilMigrationId || !$untilMigrationId) {
46
                break;
47
            }
48
        }
49
    }
50
}
51