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

MigrateStatusCommand::run()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 5
nop 0
1
<?php
2
3
namespace Schnittstabil\Dartisan\Commands;
4
5
use Garden\Cli\Cli;
6
use Garden\Cli\Args;
7
use Garden\Cli\Table;
8
use Illuminate\Database\Migrations\Migrator;
9
10
class MigrateStatusCommand extends Command
11
{
12
    use DatabaseAwareCommandTrait;
13
    use MigrationAwareCommandTrait;
14
15
    public static $name = 'migrate:status';
16
    protected $args;
17
    protected $migrator;
18
    protected $migrationsPath;
19
20 View Code Duplication
    public function __construct(callable $outputFormatter, Args $args, Migrator $migrator, $migrationsPath)
21
    {
22
        parent::__construct($outputFormatter);
23
        $this->args = $args;
24
        $this->migrator = $migrator;
25
        $this->migrationsPath = $migrationsPath;
26
    }
27
28
    public function run()
29
    {
30
        if (!$this->migrator->repositoryExists()) {
31
            $this->echoError('No migration table found.');
32
            return 1;
33
        }
34
35
        $path = $this->args->getOpt('path', $this->migrationsPath);
36
        $ran = $this->migrator->getRepository()->getRan();
37
        $migrationFiles = $this->migrator->getMigrationFiles($path);
38
39
        if (count($migrationFiles) === 0) {
40
            $this->echoInfo('No migration files found.');
41
            return 0;
42
        }
43
44
        $table = new Table();
45
        $table
46
            ->row()
47
            ->bold('Ran')
48
            ->bold('Migration');
49
50
        foreach ($migrationFiles as $migration) {
51
            $migrationName = $this->migrator->getMigrationName($migration);
52
            $table->row();
53
            in_array($migrationName, $ran) ? $table->green('Y') : $table->red('N');
54
            $table->cell($migrationName);
55
        }
56
57
        $table->write();
58
59
        return 0;
60
    }
61
62
    public static function register(Cli $cli)
63
    {
64
        $cli = static::registerDatabaseOpts($cli);
65
        $cli = static::registerMigrationOpts($cli);
66
67
        return $cli
68
            ->command(static::$name)
69
            ->description('Show the status of each migration.')
70
            ->opt('path', 'The path of migrations files to be executed.');
71
    }
72
}
73