DatabaseUpgradeCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 20.78 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 16
loc 77
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 16 16 3
B migrateToLatest() 0 25 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Jumilla\Versionia\Laravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Jumilla\Versionia\Laravel\Migrator;
8
9
class DatabaseUpgradeCommand extends Command
10
{
11
    use DatabaseCommandTrait;
12
    use ConfirmableTrait;
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'database:upgrade
20
        {--seed= : Indicates if the seed task should be re-run.}
21
        {--force : Force the operation to run when in production.}
22
    ';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Database migration to latest version';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
35
     *
36
     * @return mixed
37
     */
38 View Code Duplication
    public function handle(Migrator $migrator)
39
    {
40
        if (!$this->confirmToProceed()) {
41
            return;
42
        }
43
44
        $migrator->makeLogTable();
45
46
        $this->migrateToLatest($migrator);
47
48
        $seed = $this->option('seed');
49
50
        if ($seed) {
51
            $this->call('database:seed', ['name' => $seed, '--force' => true]);
52
        }
53
    }
54
55
    /**
56
     * Migrate dataase to latest version.
57
     *
58
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
59
     */
60
    protected function migrateToLatest(Migrator $migrator)
61
    {
62
        $installed_migrations = $migrator->installedLatestMigrations();
63
64
        $migration_count = 0;
65
66
        foreach ($migrator->migrationGroups() as $group) {
67
            // [$group => ['version'=>$version, 'class'=>$class]] to $version
68
            $latest_installed_version = data_get($installed_migrations, $group.'.version', Migrator::VERSION_NULL);
69
70
            foreach ($migrator->migrationVersions($group) as $version => $class) {
71
                if ($migrator->compareMigrationVersion($version, $latest_installed_version) > 0) {
72
                    $this->line("<info>Up [$group/$version]</info> Run class <comment>$class</comment>");
73
74
                    $migrator->doUpgrade($group, $version, $class);
75
76
                    ++$migration_count;
77
                }
78
            }
79
        }
80
81
        if ($migration_count == 0) {
82
            $this->line('<info>Nothing to migrate.</info>');
83
        }
84
    }
85
}
86