Passed
Push — dev5 ( 8481ee...a622eb )
by Ron
03:52 queued 01:34
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 Carbon\Carbon;
4
use Illuminate\Support\Facades\Schema;
5
use Illuminate\Database\Schema\Blueprint;
6
use Illuminate\Database\Migrations\Migration;
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
        //  Setup the default role permissions
28
        $defaults = [
29
            1 => [
30
                1  => 1,
31
                2  => 1,
32
                3  => 1,
33
                4  => 1,
34
                5  => 1,
35
                6  => 1,
36
                7  => 1,
37
                8  => 1,
38
                9  => 1,
39
                10 => 1,
40
                11 => 1,
41
            ],
42
            2 => [
43
                1  => 1,
44
                2  => 1,
45
                3  => 1,
46
                4  => 1,
47
                5  => 1,
48
                6  => 1,
49
                7  => 1,
50
                8  => 1,
51
                9  => 1,
52
                10 => 1,
53
                11 => 1,
54
            ],
55
            3 => [
56
                1  => 0,
57
                2  => 0,
58
                3  => 1,
59
                4  => 1,
60
                5  => 0,
61
                6  => 0,
62
                7  => 1,
63
                8  => 1,
64
                9  => 0,
65
                10 => 0,
66
                11 => 0,
67
            ],
68
            4 => [
69
                1  => 0,
70
                2  => 0,
71
                3  => 0,
72
                4  => 1,
73
                5  => 0,
74
                6  => 0,
75
                7  => 1,
76
                8  => 1,
77
                9  => 0,
78
                10 => 0,
79
                11 => 0,
80
            ],
81
        ];
82
83
        foreach($defaults as $role_id => $perms)
84
        {
85
            foreach($perms as $perm_type_id => $allow)
86
            {
87
                DB::table('user_role_permissions')->insert([
88
                    'role_id'      => $role_id,
89
                    'perm_type_id' => $perm_type_id,
90
                    'allow'        => $allow,
91
                    'created_at'   => Carbon::now(),
92
                    'updated_at'   => Carbon::now(),
93
                ]);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Reverse the migrations.
100
     *
101
     * @return void
102
     */
103
    public function down()
104
    {
105
        Schema::dropIfExists('user_role_permissions');
106
    }
107
}
108