Passed
Push — master ( 522ba7...dac3f8 )
by Andrey
13:44
created

BaseMigration::dropTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Helldar\Roles\Support\Database;
4
5
use Closure;
6
use Helldar\Roles\Facades\Config;
7
use Illuminate\Database\Migrations\Migration;
8
use Illuminate\Database\Schema\Blueprint;
9
use Illuminate\Database\Schema\Builder;
10
use Illuminate\Support\Facades\Schema;
11
12
abstract class BaseMigration extends Migration
13
{
14
    protected $permissions = 'permissions';
15
16
    protected $role_permission = 'role_permission';
17
18
    protected $role_permissions = 'role_permissions';
19
20
    protected $roles = 'roles';
21
22
    protected $user_permission = 'user_permission';
23
24
    protected $user_role = 'user_role';
25
26
    protected $user_roles = 'user_roles';
27
28
    protected $users = 'users';
29
30
    abstract public function up();
31 42
32
    abstract public function down();
33 42
34 42
    protected function schema(): Builder
35
    {
36
        return Schema::connection(
37
            Config::connection()
38 42
        );
39
    }
40 42
41 42
    protected function create(string $table, Closure $callback)
42
    {
43
        $this->schema()->create($table, $callback);
44
    }
45
46
    protected function drop(string $table)
47
    {
48 42
        $this->schema()->dropIfExists($table);
49
    }
50 42
51 42
    protected function table(string $table, Closure $callback)
52
    {
53 42
        $this->schema()->table($table, $callback);
54
    }
55 42
56 42
    protected function rename(string $from, string $to)
57
    {
58
        $this->schema()->rename($from, $to);
59
    }
60
61
    protected function dropTables(...$tables)
62
    {
63
        foreach ($tables as $table) {
64
            $this->drop($table);
65
        }
66
    }
67
68
    protected function createPivot(string $table, string $first_table, string $second_table, string $first_key, string $second_key)
69
    {
70
        $this->create($table, function (Blueprint $table) use ($first_table, $second_table, $first_key, $second_key) {
71
            $table->unsignedBigInteger($first_key);
72
            $table->unsignedBigInteger($second_key);
73
74
            $table->foreign($first_key)->references('id')->on($first_table)->onDelete('cascade');
75
            $table->foreign($second_key)->references('id')->on($second_table)->onDelete('cascade');
76
77
            $table->primary([$first_key, $second_key]);
78
        });
79
    }
80
}
81