DatabaseRefreshCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 30.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 23
loc 75
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 16 16 3
B doRefresh() 7 23 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 DatabaseRefreshCommand 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:refresh
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 migrate to clean, and migrate 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->doRefresh($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
     * Execute clean and upgrade.
57
     *
58
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
59
     */
60
    protected function doRefresh(Migrator $migrator)
61
    {
62
        // retreive installed versions
63
        $installed_migrations = $migrator->installedMigrationsByDesc();
64
65
        // downgrade
66 View Code Duplication
        foreach ($installed_migrations as $group => $migrations) {
67
            foreach ($migrations as $data) {
68
                $this->infoDowngrade($group, $data->version, $data->class);
69
70
                $migrator->doDowngrade($group, $data->version);
71
            }
72
        }
73
74
        // upgrade
75
        foreach ($migrator->migrationGroups() as $group) {
76
            foreach ($migrator->migrationVersions($group) as $version => $class) {
77
                $this->infoUpgrade($group, $version, $class);
78
79
                $migrator->doUpgrade($group, $version, $class);
80
            }
81
        }
82
    }
83
}
84