Passed
Push — master ( f218e6...f51c1b )
by Arthur
04:59
created

CreateAuthorizationTables::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 47
nc 1
nop 0
dl 0
loc 68
rs 9.1563
c 1
b 0
f 0

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
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Database\Migrations\Migration;
6
7
class CreateAuthorizationTables extends Migration
8
{
9
    /**
10
     * Run the migrations.
11
     *
12
     * @return void
13
     */
14
    public function up()
15
    {
16
        $tableNames = config('permission.table_names');
17
        $columnNames = config('permission.column_names');
18
19
        Schema::create($tableNames['permissions'], function (Blueprint $table) {
20
            $table->increments('id');
21
            $table->string('name');
22
            $table->string('guard_name');
23
            $table->timestamps();
24
        });
25
26
        Schema::create($tableNames['roles'], function (Blueprint $table) {
27
            $table->increments('id');
28
            $table->string('name');
29
            $table->string('guard_name');
30
            $table->timestamps();
31
        });
32
33
        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
34
            $table->unsignedInteger('permission_id');
35
36
            $table->string('model_type');
37
            $table->unsignedBigInteger($columnNames['model_morph_key']);
38
            $table->index([$columnNames['model_morph_key'], 'model_type', ]);
39
40
            $table->foreign('permission_id')
41
                ->references('id')
42
                ->on($tableNames['permissions'])
43
                ->onDelete('cascade');
44
45
            $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
46
                    'model_has_permissions_permission_model_type_primary');
47
        });
48
49
        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
50
            $table->unsignedInteger('role_id');
51
52
            $table->string('model_type');
53
            $table->unsignedBigInteger($columnNames['model_morph_key']);
54
            $table->index([$columnNames['model_morph_key'], 'model_type', ]);
55
56
            $table->foreign('role_id')
57
                ->references('id')
58
                ->on($tableNames['roles'])
59
                ->onDelete('cascade');
60
61
            $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
62
                    'model_has_roles_role_model_type_primary');
63
        });
64
65
        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
66
            $table->unsignedInteger('permission_id');
67
            $table->unsignedInteger('role_id');
68
69
            $table->foreign('permission_id')
70
                ->references('id')
71
                ->on($tableNames['permissions'])
72
                ->onDelete('cascade');
73
74
            $table->foreign('role_id')
75
                ->references('id')
76
                ->on($tableNames['roles'])
77
                ->onDelete('cascade');
78
79
            $table->primary(['permission_id', 'role_id']);
80
81
            app('cache')->forget('spatie.permission.cache');
82
        });
83
    }
84
85
    /**
86
     * Reverse the migrations.
87
     *
88
     * @return void
89
     */
90
    public function down()
91
    {
92
        $tableNames = config('permission.table_names');
93
94
        Schema::drop($tableNames['role_has_permissions']);
95
        Schema::drop($tableNames['model_has_roles']);
96
        Schema::drop($tableNames['model_has_permissions']);
97
        Schema::drop($tableNames['roles']);
98
        Schema::drop($tableNames['permissions']);
99
    }
100
}
101