Completed
Push — master ( 7a93ab...e5b266 )
by Michael
03:59
created

MigrateRollbackCommand::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
namespace Schnittstabil\Dartisan\Commands;
4
5
use Garden\Cli\Cli;
6
use Garden\Cli\Args;
7
use Illuminate\Database\Migrations\Migrator;
8
9
class MigrateRollbackCommand extends Command
10
{
11
    use DatabaseAwareCommandTrait;
12
    use MigrationAwareCommandTrait;
13
14
    public static $name = 'migrate:rollback';
15
    protected $args;
16
    protected $migrator;
17
    protected $migrationsPath;
18
19 View Code Duplication
    public function __construct(callable $outputFormatter, Args $args, Migrator $migrator, $migrationsPath)
20
    {
21
        parent::__construct($outputFormatter);
22
        $this->args = $args;
23
        $this->migrator = $migrator;
24
        $this->migrationsPath = $migrationsPath;
25
    }
26
27
    public function run()
28
    {
29
        if (!$this->migrator->repositoryExists()) {
30
            $this->echoError('No migration table found.');
31
            return 1;
32
        }
33
34
        $path = $this->args->getOpt('path', $this->migrationsPath);
35
        $this->migrator->rollback($path, [
36
            'pretend' => $this->args->getOpt('pretend', null),
37
            'step' => $this->args->getOpt('step', null),
38
        ]);
39
        $this->echoNotes($this->migrator->getNotes());
40
41
        return 0;
42
    }
43
44
    public static function register(Cli $cli)
45
    {
46
        $cli = static::registerDatabaseOpts($cli);
47
        $cli = static::registerMigrationOpts($cli);
48
49
        return $cli
50
            ->command(static::$name)
51
            ->description('Rollback the last database migration.')
52
            ->opt('pretend', 'Dump the SQL queries that would be run.');
53
    }
54
}
55