Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

Menu   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 10
loc 80
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 2
A roles() 0 8 1
A allNodes() 0 9 2
A withPermission() 0 4 1
A boot() 0 8 1

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
namespace Encore\Admin\Auth\Database;
4
5
use Encore\Admin\Traits\AdminBuilder;
6
use Encore\Admin\Traits\ModelTree;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Support\Facades\DB;
10
11
/**
12
 * Class Menu.
13
 *
14
 * @property int $id
15
 *
16
 * @method where($parent_id, $id)
17
 */
18
class Menu extends Model
19
{
20
    use AdminBuilder, ModelTree {
21
        ModelTree::boot as treeBoot;
22
    }
23
24
    /**
25
     * The attributes that are mass assignable.
26
     *
27
     * @var array
28
     */
29
    protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri', 'permission'];
30
31
    /**
32
     * Create a new Eloquent model instance.
33
     *
34
     * @param array $attributes
35
     */
36 View Code Duplication
    public function __construct(array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
    {
38
        $connection = config('admin.database.connection') ?: config('database.default');
39
40
        $this->setConnection($connection);
41
42
        $this->setTable(config('admin.database.menu_table'));
43
44
        parent::__construct($attributes);
45
    }
46
47
    /**
48
     * A Menu belongs to many roles.
49
     *
50
     * @return BelongsToMany
51
     */
52
    public function roles() : BelongsToMany
53
    {
54
        $pivotTable = config('admin.database.role_menu_table');
55
56
        $relatedModel = config('admin.database.roles_model');
57
58
        return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'role_id');
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function allNodes() : array
65
    {
66
        $connection = config('admin.database.connection') ?: config('database.default');
67
        $orderColumn = DB::connection($connection)->getQueryGrammar()->wrap($this->orderColumn);
68
69
        $byOrder = $orderColumn.' = 0,'.$orderColumn;
70
71
        return static::with('roles')->orderByRaw($byOrder)->get()->toArray();
72
    }
73
74
    /**
75
     * determine if enable menu bind permission.
76
     *
77
     * @return bool
78
     */
79
    public function withPermission()
80
    {
81
        return (bool) config('admin.menu_bind_permission');
82
    }
83
84
    /**
85
     * Detach models from the relationship.
86
     *
87
     * @return void
88
     */
89
    protected static function boot()
90
    {
91
        static::treeBoot();
92
93
        static::deleting(function ($model) {
94
            $model->roles()->detach();
95
        });
96
    }
97
}
98