Completed
Push — master ( 57ad4c...31fe35 )
by Freek
03:26 queued 01:40
created

MigrateFresh::getTableDropper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Spatie\MigrateFresh\Commands;
4
5
use DB;
6
use Illuminate\Console\Command;
7
use Illuminate\Console\ConfirmableTrait;
8
use Spatie\MigrateFresh\TableDroppers\TableDropper;
9
use Spatie\MigrateFresh\Exceptions\CannotDropTables;
10
11
class MigrateFresh extends Command
12
{
13
    use ConfirmableTrait;
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'migrate:fresh {--seed} {--force}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Drop all tables from db and rebuild it using migrations.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return mixed
33
     */
34
    public function handle()
35
    {
36
        if (! $this->confirmToProceed()) {
37
            return;
38
        }
39
40
        $this->info('Dropping all tables...');
41
        $this->getTableDropper()->dropAllTables();
42
43
        $this->info('Running migrations...');
44
        $this->call('migrate', ['--force' => true]);
45
46
        if ($this->option('seed')) {
47
            $this->info('Running seeders...');
48
            $this->call('db:seed', ['--force' => true]);
49
        }
50
51
        $this->comment('All done!');
52
    }
53
54
    public function getTableDropper(): TableDropper
55
    {
56
        $driverName = DB::getDriverName();
57
58
        $dropperClass = '\\Spatie\\MigrateFresh\\TableDroppers\\'.ucfirst($driverName);
59
60
        if (! class_exists($dropperClass)) {
61
            throw CannotDropTables::unsupportedDbDriver($driverName);
62
        }
63
64
        return new $dropperClass;
65
    }
66
}
67