Passed
Pull Request — master (#77)
by Ron
41:51 queued 13:39
created

CreateUserRolePermissionsTable::up()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 77
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 65
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 77
rs 8.7636

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\Database\Migrations\Migration;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Support\Facades\Schema;
6
use Carbon\Carbon;
7
8
class CreateUserRolePermissionsTable extends Migration
9
{
10
    /**
11
     * Run the migrations.
12
     *
13
     * @return void
14
     */
15
    public function up()
16
    {
17
        Schema::create('user_role_permissions', function (Blueprint $table) {
18
            $table->increments('id');
19
            $table->integer('role_id')->unsigned();
20
            $table->integer('perm_type_id')->unsigned();
21
            $table->boolean('allow')->default(0);
22
            $table->timestamps();
23
            $table->foreign('role_id')->references('role_id')->on('user_role_types')->onUpdate('cascade');
24
            $table->foreign('perm_type_id')->references('perm_type_id')->on('user_role_permission_types')->onUpdate('cascade');
25
        });
26
27
        $defaults = [
28
            1 => [
29
                1  => 1,
30
                2  => 1,
31
                3  => 1,
32
                4  => 1,
33
                5  => 1,
34
                6  => 1,
35
                7  => 1,
36
                8  => 1,
37
                9  => 1,
38
                10 => 1,
39
                11 => 1,
40
            ],
41
            2 => [
42
                1  => 1,
43
                2  => 1,
44
                3  => 1,
45
                4  => 1,
46
                5  => 1,
47
                6  => 1,
48
                7  => 1,
49
                8  => 1,
50
                9  => 1,
51
                10 => 1,
52
                11 => 1,
53
            ],
54
            3 => [
55
                1  => 0,
56
                2  => 0,
57
                3  => 1,
58
                4  => 1,
59
                5  => 0,
60
                6  => 0,
61
                7  => 1,
62
                8  => 1,
63
                9  => 0,
64
                10 => 0,
65
                11 => 0,
66
            ],
67
            4 => [
68
                1  => 0,
69
                2  => 0,
70
                3  => 0,
71
                4  => 1,
72
                5  => 0,
73
                6  => 0,
74
                7  => 1,
75
                8  => 1,
76
                9  => 0,
77
                10 => 0,
78
                11 => 0,
79
            ],
80
        ];
81
82
        foreach($defaults as $role_id => $perms)
83
        {
84
            foreach($perms as $perm_type_id => $allow)
85
            {
86
                DB::table('user_role_permissions')->insert([
87
                    'role_id'      => $role_id,
88
                    'perm_type_id' => $perm_type_id,
89
                    'allow'        => $allow,
90
                    'created_at'   => Carbon::now(),
91
                    'updated_at'   => Carbon::now(),
92
                ]);
93
            }
94
        }
95
    }
96
97
    /**
98
     * Reverse the migrations.
99
     *
100
     * @return void
101
     */
102
    public function down()
103
    {
104
        Schema::dropIfExists('user_role_permissions');
105
    }
106
}
107