Passed
Push — develop ( 330c0c...a751d7 )
by Septianata
12:33
created

CreatePermissionTables::up()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 79
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 53
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 79
rs 9.0254

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
    /**
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
        if (empty($tableNames)) {
20
            throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
21
        }
22
23
        Schema::create($tableNames['permissions'], function (Blueprint $table) {
24
            $table->bigIncrements('id');
25
            $table->string('name');       // For MySQL 8.0 use string('name', 125);
26
            $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
27
            $table->timestamps();
28
29
            $table->unique(['name', 'guard_name']);
30
        });
31
32
        Schema::create($tableNames['roles'], function (Blueprint $table) {
33
            $table->bigIncrements('id');
34
            $table->string('name');       // For MySQL 8.0 use string('name', 125);
35
            $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
36
            $table->timestamps();
37
38
            $table->unique(['name', 'guard_name']);
39
        });
40
41
        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
42
            $table->unsignedBigInteger('permission_id');
43
44
            $table->string('model_type');
45
            $table->unsignedBigInteger($columnNames['model_morph_key']);
46
            $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
47
48
            $table->foreign('permission_id')
49
                ->references('id')
50
                ->on($tableNames['permissions'])
51
                ->onDelete('cascade');
52
53
            $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
54
                    'model_has_permissions_permission_model_type_primary');
55
        });
56
57
        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
58
            $table->unsignedBigInteger('role_id');
59
60
            $table->string('model_type');
61
            $table->unsignedBigInteger($columnNames['model_morph_key']);
62
            $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
63
64
            $table->foreign('role_id')
65
                ->references('id')
66
                ->on($tableNames['roles'])
67
                ->onDelete('cascade');
68
69
            $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
70
                    'model_has_roles_role_model_type_primary');
71
        });
72
73
        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
74
            $table->unsignedBigInteger('permission_id');
75
            $table->unsignedBigInteger('role_id');
76
77
            $table->foreign('permission_id')
78
                ->references('id')
79
                ->on($tableNames['permissions'])
80
                ->onDelete('cascade');
81
82
            $table->foreign('role_id')
83
                ->references('id')
84
                ->on($tableNames['roles'])
85
                ->onDelete('cascade');
86
87
            $table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
88
        });
89
90
        app('cache')
91
            ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
92
            ->forget(config('permission.cache.key'));
93
    }
94
95
    /**
96
     * Reverse the migrations.
97
     *
98
     * @return void
99
     */
100
    public function down()
101
    {
102
        $tableNames = config('permission.table_names');
103
104
        if (empty($tableNames)) {
105
            throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
106
        }
107
108
        Schema::drop($tableNames['role_has_permissions']);
109
        Schema::drop($tableNames['model_has_roles']);
110
        Schema::drop($tableNames['model_has_permissions']);
111
        Schema::drop($tableNames['roles']);
112
        Schema::drop($tableNames['permissions']);
113
    }
114
}
115