Completed
Push — master ( aed5e6...e7b04e )
by Fumio
01:47
created

DatabaseStatusCommand::showMigrations()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 29
cts 29
cp 1
rs 7.1199
c 0
b 0
f 0
cc 8
eloc 30
nc 12
nop 1
crap 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 4
   public function handle(Migrator $migrator)
35
   {
36 4
       $migrator->makeLogTable();
37
38 4
       $this->showMigrations($migrator);
39
40 4
       $this->showSeeds($migrator);
41 4
   }
42
43
    /**
44
     * Show migration infomation.
45
     *
46
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
47
     */
48 4
    protected function showMigrations(Migrator $migrator)
49
    {
50 4
        $this->line('<comment>Migrations</comment>');
51
52 4
        $groups = $migrator->migrationGroups();
53
54 4
        if (count($groups) > 0) {
55
            // get [$group => $items]
56 1
            $installed_migrations = $migrator->installedMigrationsByDesc();
57
58 1
            foreach ($groups as $group) {
59
                // [$items] to [$installed_versions]
60 1
                $installed_versions = $installed_migrations->get($group, collect())->pluck('version');
61
62
                // [$versions] to $latest_version
63 1
                $latest_installed_version = $installed_versions->get(0, Migrator::VERSION_NULL);
64
65
                // enum definition
66 1
                foreach ($migrator->migrationVersionsByDesc($group) as $version => $class) {
67 1
                    $installed = $installed_versions->contains($version);
68
69 1
                    if ($version === $latest_installed_version) {
70 1
                        $mark = '*';
71
                    } else {
72 1
                        $mark = $installed ? '-' : ' ';
73
                    }
74
75 1
                    if (!class_exists($class)) {
76 1
                        $this->line("{$mark} <info>[{$group}/{$version}]</info> <error>{$class}</error>");
77 1
                        $this->line('');
78 1
                        $this->error('Error: Class not found.');
79 1
                        continue;
80
                    }
81
82 1
                    $migration = new $class();
83
84 1
                    if (!$migration instanceof Migration) {
85 1
                        $this->line("{$mark} <info>[{$group}/{$version}]</info> <error>{$class}</error>");
86 1
                        $this->line('');
87 1
                        $this->error('Error: Must inherit from class "Illuminate\Database\Migrations\Migration".');
88 1
                        continue;
89
                    }
90
91 1
                    $this->line("{$mark} <info>[{$group}/{$version}]</info> {$class}");
92
                }
93
94 1
                $this->line('');
95
            }
96
        } else {
97 3
            $this->info('Nothing.');
98 3
            $this->line('');
99
        }
100 4
    }
101
102
    /**
103
     * Show seed infomation.
104
     *
105
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
106
     */
107 4
    protected function showSeeds(Migrator $migrator)
108
    {
109 4
        $this->line('<comment>Seeds</comment>');
110
111 4
        $seeds = $migrator->seedNames();
112
113 4
        if (count($seeds) > 0) {
114 2
            $default_seed = $migrator->defaultSeed();
115
116 2
            foreach ($seeds as $seed) {
117 2
                $class = $migrator->seedClass($seed);
118
119 2
                $status_mark = ' ';
120 2
                $default_mark = $seed == $default_seed ? '(default)' : '';
121
122 2
                if (!class_exists($class)) {
123 1
                    $this->line("{$status_mark} <info>[{$seed}]</info> <error>{$class}</error>");
124 1
                    $this->line('');
125 1
                    $this->error('Error: Class not found.');
126 1
                    continue;
127
                }
128
129 2
                $this->line("{$status_mark} <comment>{$default_mark}</comment><info>[{$seed}]</info> {$class}");
130
            }
131
132 2
            if ($default_seed && !in_array($default_seed, $seeds)) {
133 1
                $this->line('');
134 2
                $this->error("Error: default seed '{$default_seed}' is not defined.");
135
            }
136
        } else {
137 2
            $this->info('Nothing.');
138
        }
139
140 4
        $this->line('');
141 4
    }
142
}
143