Completed
Push — master ( 60538d...52baeb )
by wen
02:30
created

RbacSetupTables   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 99
Duplicated Lines 17.17 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 17
loc 99
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A up() 17 63 2
A down() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 $userTable;
9
    private $userForeignKey;
10
11
    public function __construct()
12
    {
13
        $provider = config('auth.guards.' . config('admin.guard') . '.provider');
14
        $userModelName = config("auth.providers.{$provider}.model");
15
        $userModel = new $userModelName();
16
        $this->userTable = $userModel->getTable();
17
        $this->userForeignKey = $userModel->getForeignKey();
18
    }
19
20
    /**
21
     * Run the migrations.
22
     *
23
     * @return  void
24
     */
25
    public function up()
26
    {
27
        // Create table for admin user
28
        if ($this->userTable != 'users') {
29 View Code Duplication
            Schema::create($this->userTable, 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...
30
                $table->engine = "InnoDB COMMENT='管理员表'";
31
                $table->increments('id');
32
                $table->string('name')->unique()->comment('名称');
33
                $table->string('email')->nullable()->unique()->comment('邮箱');
34
                $table->string('password')->nullable()->comment('密码');
35
                $table->rememberToken();
36
                $table->timestamps();
37
            });
38
        }
39
40
        // Create table for storing roles
41 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...
42
            $table->engine = "InnoDB COMMENT='角色表'";
43
            $table->increments('id');
44
            $table->string('name')->unique()->comment('名称');
45
            $table->string('display_name')->nullable()->comment('显示名');
46
            $table->string('description')->nullable()->comment('备注');
47
            $table->timestamps();
48
        });
49
50
        // Create table for associating roles to users (Many-to-Many)
51
        Schema::create('role_user', function (Blueprint $table) {
52
            $table->engine = "InnoDB COMMENT='角色与用户对应表'";
53
            $table->integer($this->userForeignKey)->unsigned()->comment('管理员ID');
54
            $table->integer('role_id')->unsigned()->comment('角色ID');
55
56
            /*$table->foreign('uid')->references('id')->on('users')
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
                ->onUpdate('cascade')->onDelete('cascade');
58
            $table->foreign('role_id')->references('id')->on('roles')
59
                ->onUpdate('cascade')->onDelete('cascade');*/
60
61
            $table->primary([$this->userForeignKey, 'role_id']);
62
        });
63
64
        // Create table for storing permissions
65
        Schema::create('permissions', function (Blueprint $table) {
66
            $table->engine = "InnoDB COMMENT='权限(即菜单)表'";
67
            $table->increments('id')->comment('主键');
68
            $table->integer('pid')->comment('父ID');
69
            $table->string('icon', 100)->comment('图标class');
70
            $table->string('display_name', 200)->comment('显示名称');
71
            $table->string('name', 100)->comment('名称');
72
            $table->tinyInteger('is_menu')->comment('是否作为菜单')->default('1');
73
            $table->tinyInteger('sort')->unsigned()->comment('排序');
74
            $table->string('description')->nullable()->comment('描述');
75
            $table->timestamps();
76
            $table->index('pid');
77
        });
78
79
        // Create table for associating permissions to roles (Many-to-Many)
80
        Schema::create('permission_role', function (Blueprint $table) {
81
            $table->engine = "InnoDB COMMENT='权限与角色对应表'";
82
            $table->integer('permission_id')->unsigned()->comment('权限ID');
83
            $table->integer('role_id')->unsigned()->comment('角色ID');
84
85
            $table->primary(['permission_id', 'role_id']);
86
        });
87
    }
88
89
    /**
90
     * Reverse the migrations.
91
     *
92
     * @return  void
93
     */
94
    public function down()
95
    {
96
        Schema::drop('permission_role');
97
        Schema::drop('permissions');
98
        Schema::drop('role_user');
99
        Schema::drop('roles');
100
        if ($this->userTable != 'users') {
101
            Schema::drop($this->userTable);
102
        }
103
    }
104
}
105