Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

CreatePermissionTables::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 40
nc 1
nop 0
dl 0
loc 59
rs 9.28
c 0
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 CreatePermissionTables extends Migration
8
{
9
    public function up()
10
    {
11
        $tableNames = config('permission.table_names');
12
13
        Schema::create($tableNames['permissions'], function (Blueprint $table) {
14
            $table->increments('id');
15
            $table->string('name');
16
            $table->string('guard_name');
17
            $table->timestamps();
18
        });
19
20
        Schema::create($tableNames['roles'], function (Blueprint $table) {
21
            $table->increments('id');
22
            $table->string('name');
23
            $table->string('guard_name');
24
            $table->timestamps();
25
        });
26
27
        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames) {
28
            $table->unsignedInteger('permission_id');
29
            $table->morphs('model');
30
31
            $table->foreign('permission_id')
32
                ->references('id')
33
                ->on($tableNames['permissions'])
34
                ->onDelete('cascade');
35
36
            $table->primary(['permission_id', 'model_id', 'model_type']);
37
        });
38
39
        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames) {
40
            $table->unsignedInteger('role_id');
41
            $table->morphs('model');
42
43
            $table->foreign('role_id')
44
                ->references('id')
45
                ->on($tableNames['roles'])
46
                ->onDelete('cascade');
47
48
            $table->primary(['role_id', 'model_id', 'model_type']);
49
        });
50
51
        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
52
            $table->unsignedInteger('permission_id');
53
            $table->unsignedInteger('role_id');
54
55
            $table->foreign('permission_id')
56
                ->references('id')
57
                ->on($tableNames['permissions'])
58
                ->onDelete('cascade');
59
60
            $table->foreign('role_id')
61
                ->references('id')
62
                ->on($tableNames['roles'])
63
                ->onDelete('cascade');
64
65
            $table->primary(['permission_id', 'role_id']);
66
67
            app('cache')->forget('spatie.permission.cache');
68
        });
69
    }
70
71
    public function down()
72
    {
73
        $tableNames = config('permission.table_names');
74
75
        Schema::drop($tableNames['role_has_permissions']);
76
        Schema::drop($tableNames['model_has_roles']);
77
        Schema::drop($tableNames['model_has_permissions']);
78
        Schema::drop($tableNames['roles']);
79
        Schema::drop($tableNames['permissions']);
80
    }
81
}
82