Completed
Push — master ( 149996...1d6fcb )
by wen
11:13
created

RbacSetupTables::up()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 108
Code Lines 72

Duplication

Lines 17
Ratio 15.74 %

Importance

Changes 0
Metric Value
dl 17
loc 108
rs 8.2857
c 0
b 0
f 0
cc 2
eloc 72
nc 2
nop 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\Database\Schema\Blueprint;
4
use Illuminate\Database\Migrations\Migration;
5
6
class RbacSetupTables extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    private $managerTable;
9
    private $managerForeignKey;
10
11
    public function __construct()
12
    {
13
        $guard                   = config('admin.guard');
14
        $provider                = config("auth.guards.{$guard}.provider");
15
        $managerModelName        = config("auth.providers.{$provider}.model");
16
        $managerModel            = new $managerModelName();
17
        $this->managerTable      = $managerModel->getTable();
18
        $this->managerForeignKey = $managerModel->getForeignKey();
19
    }
20
21
    /**
22
     * Run the migrations.
23
     *
24
     * @return  void
25
     */
26
    public function up()
27
    {
28
        // Create table for admin user
29
        if ($this->managerTable != 'users') {
30 View Code Duplication
            Schema::create($this->managerTable, function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
                $table->engine = "InnoDB COMMENT='管理员表'";
32
                $table->increments('id');
33
34
                $table->string('name')
35
                    ->unique()
36
                    ->comment('名称');
37
38
                $table->string('email')
39
                    ->nullable()
40
                    ->unique()
41
                    ->comment('邮箱');
42
43
                $table->string('password')
44
                    ->nullable()
45
                    ->comment('密码');
46
47
                $table->rememberToken();
48
                $table->timestamps();
49
            });
50
        }
51
52
        // Create table for storing roles
53 View Code Duplication
        Schema::create('roles', function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $table->engine = "InnoDB COMMENT='角色表'";
55
            $table->increments('id');
56
57
            $table->string('name')
58
                ->unique()
59
                ->comment('名称');
60
61
            $table->string('display_name')
62
                ->nullable()
63
                ->comment('显示名');
64
65
            $table->string('description')
66
                ->nullable()
67
                ->comment('备注');
68
69
            $table->timestamps();
70
        });
71
72
        // Create table for associating roles to users (Many-to-Many)
73
        Schema::create('role_user', function (Blueprint $table) {
74
            $table->engine = "InnoDB COMMENT='角色与用户对应表'";
75
            $table->integer($this->managerForeignKey)
76
                ->unsigned()
77
                ->comment('管理员ID');
78
            $table->integer('role_id')
79
                ->unsigned()
80
                ->comment('角色ID');
81
82
            $table->primary([$this->managerForeignKey, 'role_id']);
83
        });
84
85
        // Create table for storing permissions
86
        Schema::create('permissions', function (Blueprint $table) {
87
            $table->engine = "InnoDB COMMENT='权限(即菜单)表'";
88
            $table->increments('id')->comment('主键');
89
90
            $table->integer('pid')
91
                ->comment('父ID');
92
93
            $table->string('icon', 100)
94
                ->comment('图标class')
95
                ->default('');
96
97
            $table->string('display_name', 200)
98
                ->comment('显示名称');
99
100
            $table->string('name', 100)
101
                ->comment('名称');
102
103
            $table->tinyInteger('is_menu')
104
                ->comment('是否作为菜单')
105
                ->default('1');
106
107
            $table->tinyInteger('sort')
108
                ->unsigned()
109
                ->comment('排序')
110
                ->default('0');
111
112
            $table->string('description')
113
                ->nullable()
114
                ->comment('描述');
115
116
            $table->timestamps();
117
            $table->index('pid');
118
        });
119
120
        // Create table for associating permissions to roles (Many-to-Many)
121
        Schema::create('permission_role', function (Blueprint $table) {
122
            $table->engine = "InnoDB COMMENT='权限与角色对应表'";
123
            $table->integer('permission_id')
124
                ->unsigned()
125
                ->comment('权限ID');
126
127
            $table->integer('role_id')
128
                ->unsigned()
129
                ->comment('角色ID');
130
131
            $table->primary(['permission_id', 'role_id']);
132
        });
133
    }
134
135
    /**
136
     * Reverse the migrations.
137
     *
138
     * @return  void
139
     */
140
    public function down()
141
    {
142
        Schema::drop('permission_role');
143
        Schema::drop('permissions');
144
        Schema::drop('role_user');
145
        Schema::drop('roles');
146
        if ($this->managerTable != 'users') {
147
            Schema::drop($this->managerTable);
148
        }
149
    }
150
}
151