Completed
Push — master ( 1db263...be4043 )
by Michael
02:39
created

MigrateStatusCommand::echoMigrationTable()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
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 $migrator;
17
    protected $migrationsPath;
18
19
    public function __construct(
20
        Args $args,
21
        callable $outputFormatter,
22
        Migrator $migrator,
23
        $migrationsPath
24
    ) {
25
        parent::__construct($args, $outputFormatter);
26
        $this->migrator = $migrator;
27
        $this->migrationsPath = $migrationsPath;
28
    }
29
30
    public function run()
31
    {
32
        if (!$this->migrator->repositoryExists()) {
33
            $this->echoError('No migration table found.');
34
35
            return 1;
36
        }
37
38
        $path = $this->args->getOpt('path', $this->migrationsPath);
39
        $ran = $this->migrator->getRepository()->getRan();
40
        $migrationFiles = $this->migrator->getMigrationFiles($path);
41
        $this->echoMigrationTable($ran, $migrationFiles);
42
43
        return 0;
44
    }
45
46
    protected function echoMigrationTable($ran, $migrationFiles)
47
    {
48
        if (count($migrationFiles) === 0) {
49
            $this->echoInfo('No migration files found.');
50
51
            return;
52
        }
53
54
        $table = new Table();
55
        $table
56
            ->row()
57
            ->bold('Ran')
58
            ->bold('Migration');
59
60
        foreach ($migrationFiles as $migration) {
61
            $migrationName = $this->migrator->getMigrationName($migration);
62
            $table->row();
63
            in_array($migrationName, $ran) ? $table->green('Y') : $table->red('N');
64
            $table->cell($migrationName);
65
        }
66
67
        $table->write();
68
    }
69
70
    public static function register(Cli $cli)
71
    {
72
        $cli = static::registerDatabaseOpts($cli);
73
        $cli = static::registerMigrationOpts($cli);
74
75
        return $cli
76
            ->command(static::$name)
77
            ->description('Show the status of each migration.')
78
            ->opt('path', 'The path of migrations files to be executed.');
79
    }
80
}
81