DatabaseStatusCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Jumilla\Versionia\Laravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Migrations\Migration;
7
use Jumilla\Versionia\Laravel\Migrator;
8
9
class DatabaseStatusCommand extends Command
10
{
11
    use DatabaseCommandTrait;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'database:status';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Display migration status';
26
27
   /**
28
    * Execute the console command.
29
    *
30
    * @param \Jumilla\Versionia\Laravel\Migrator $migrator
31
    *
32
    * @return mixed
33
    */
34
   public function handle(Migrator $migrator)
35
   {
36
       $migrator->makeLogTable();
37
38
       $this->showMigrations($migrator);
39
40
       $this->showSeeds($migrator);
41
   }
42
43
    /**
44
     * Show migration infomation.
45
     *
46
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
47
     */
48
    protected function showMigrations(Migrator $migrator)
49
    {
50
        $this->line('<comment>Migrations</comment>');
51
52
        $groups = $migrator->migrationGroups();
53
54
        if (count($groups) > 0) {
55
            // get [$group => $items]
56
            $installed_migrations = $migrator->installedMigrationsByDesc();
57
58
            foreach ($groups as $group) {
59
                // [$items] to [$installed_versions]
60
                $installed_versions = $installed_migrations->get($group, collect())->pluck('version');
61
62
                // [$versions] to $latest_version
63
                $latest_installed_version = $installed_versions->get(0, Migrator::VERSION_NULL);
64
65
                // enum definition
66
                foreach ($migrator->migrationVersionsByDesc($group) as $version => $class) {
67
                    $installed = $installed_versions->contains($version);
68
69
                    if ($version === $latest_installed_version) {
70
                        $mark = '*';
71
                    } else {
72
                        $mark = $installed ? '-' : ' ';
73
                    }
74
75
                    if (!class_exists($class)) {
76
                        $this->line("{$mark} <info>[{$group}/{$version}]</info> <error>{$class}</error>");
77
                        $this->line('');
78
                        $this->error('Error: Class not found.');
79
                        continue;
80
                    }
81
82
                    $migration = new $class();
83
84
                    if (!$migration instanceof Migration) {
85
                        $this->line("{$mark} <info>[{$group}/{$version}]</info> <error>{$class}</error>");
86
                        $this->line('');
87
                        $this->error('Error: Must inherit from class "Illuminate\Database\Migrations\Migration".');
88
                        continue;
89
                    }
90
91
                    $this->line("{$mark} <info>[{$group}/{$version}]</info> {$class}");
92
                }
93
94
                $this->line('');
95
            }
96
        } else {
97
            $this->info('Nothing.');
98
            $this->line('');
99
        }
100
    }
101
102
    /**
103
     * Show seed infomation.
104
     *
105
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
106
     */
107
    protected function showSeeds(Migrator $migrator)
108
    {
109
        $this->line('<comment>Seeds</comment>');
110
111
        $seeds = $migrator->seedNames();
112
113
        if (count($seeds) > 0) {
114
            $default_seed = $migrator->defaultSeed();
115
116
            foreach ($seeds as $seed) {
117
                $class = $migrator->seedClass($seed);
118
119
                $status_mark = ' ';
120
                $default_mark = $seed == $default_seed ? '(default)' : '';
121
122
                if (!class_exists($class)) {
123
                    $this->line("{$status_mark} <info>[{$seed}]</info> <error>{$class}</error>");
124
                    $this->line('');
125
                    $this->error('Error: Class not found.');
126
                    continue;
127
                }
128
129
                $this->line("{$status_mark} <comment>{$default_mark}</comment><info>[{$seed}]</info> {$class}");
130
            }
131
132
            if ($default_seed && !in_array($default_seed, $seeds)) {
133
                $this->line('');
134
                $this->error("Error: default seed '{$default_seed}' is not defined.");
135
            }
136
        } else {
137
            $this->info('Nothing.');
138
        }
139
140
        $this->line('');
141
    }
142
}
143