Completed
Push — master ( e5a61d...342745 )
by wen
04:30
created

Permission   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 190
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 28
c 2
b 0
f 0
lcom 1
cbo 3
dl 20
loc 190
ccs 0
cts 96
cp 0
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getMenuTreeList() 0 6 1
A getAll() 0 12 2
A getTreeAllNodes() 0 4 1
B getValidRouteList() 0 16 5
A getPermRouteList() 0 8 2
A getMenuList() 0 17 4
A getInfoById() 0 4 1
A getInfoByName() 0 8 2
A getParentTree() 0 4 1
A getParentTreeAndSelfById() 10 10 2
A getParentTreeAndSelfByName() 10 10 2
A saveMenu() 0 21 2
A deleteMenu() 0 17 2
A clearCache() 0 4 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 Sco\Admin\Models;
4
5
use DB;
6
use Cache;
7
use Illuminate\Http\Request;
8
use Sco\Admin\Exceptions\AdminHttpException;
9
use Sco\Tree\Traits\TreeTrait;
10
use Zizaco\Entrust\EntrustPermission;
11
12
class Permission extends EntrustPermission
13
{
14
    use TreeTrait;
15
16
    protected $guarded = ['created_at', 'updated_at'];
17
18
    protected $treeNodeParentIdName = 'pid';
19
20
    private $allRoutes = null;
21
22
    private $validList = null;
23
24
    private $permList = null;
25
26
    private $menuList = null;
27
28
29
    /**
30
     * @return \Illuminate\Support\Collection
31
     */
32
    public function getMenuTreeList()
33
    {
34
        $routes = $this->getDescendants(0);
35
        //dd($routes);
36
        return $routes;
37
    }
38
39
    private function getAll()
40
    {
41
        if ($this->allRoutes) {
42
            return $this->allRoutes;
43
        }
44
45
        $this->allRoutes = Cache::rememberForever('permission_all',
46
            function () {
47
                return $this->orderBy('sort')->get();
48
            });
49
        return $this->allRoutes;
50
    }
51
52
    /**
53
     * Tree Trait 获取所有节点
54
     *
55
     * @return mixed|null
56
     */
57
    protected function getTreeAllNodes()
58
    {
59
        return $this->getAll();
60
    }
61
62
    /**
63
     * 获取有效的路由列表
64
     *
65
     * @return \Illuminate\Support\Collection
66
     */
67
    public function getValidRouteList()
68
    {
69
        if ($this->validList) {
70
            return $this->validList;
71
        }
72
73
        $all = $this->getAll();
74
75
        $this->validList = collect([]);
76
        foreach ($all as $route) {
77
            if (!empty($route->uri) && $route->uri != '#') {
78
                $this->validList->push($route);
79
            }
80
        }
81
        return $this->validList;
82
    }
83
84
    /**
85
     * 获取权限列表
86
     *
87
     * @return \Illuminate\Support\Collection|null
88
     */
89
    public function getPermRouteList()
90
    {
91
        if ($this->permList) {
92
            return $this->permList;
93
        }
94
95
        return $this->permList = $this->getLayerOfDescendants(0);
96
    }
97
98
    public function getMenuList()
99
    {
100
        if ($this->menuList) {
101
            return $this->menuList;
102
        }
103
        $all = $this->getAll();
104
105
        $routes = collect([]);
106
        foreach ($all as $route) {
107
            if ($route->is_menu) {
108
                $routes->push($route);
109
            }
110
        }
111
112
        $this->setAllNodes($routes);
113
        return $this->menuList = $this->getLayerOfDescendants(0);
114
    }
115
116
    public function getInfoById($id)
117
    {
118
        return $this->getSelf($id);
119
    }
120
121
    public function getInfoByName($name)
122
    {
123
        $all = $this->getAll();
124
        $key = $all->search(function ($item) use ($name) {
125
            return $item->name == $name;
126
        });
127
        return $key === false ? false : $all->get($key);
128
    }
129
130
    public function getParentTree($id)
131
    {
132
        return $this->getAncestors($id);
133
    }
134
135 View Code Duplication
    public function getParentTreeAndSelfById($id)
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...
136
    {
137
        $self = $this->getInfoById($id);
138
        if ($self) {
139
            $parent = $this->getParentTree($self->id);
140
            $parent->push($self);
141
            return $parent;
142
        }
143
        return false;
144
    }
145
146 View Code Duplication
    public function getParentTreeAndSelfByName($name)
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...
147
    {
148
        $self = $this->getInfoByName($name);
149
        if ($self) {
150
            $parent = $this->getParentTree($self->id);
151
            $parent->push($self);
152
            return $parent;
153
        }
154
        return false;
155
    }
156
157
    public function saveMenu(Request $request)
158
    {
159
        if ($request->exists('id')) {
160
            $model = $this->findOrFail($request->input('id'));
161
162
            $model->pid          = $request->input('pid');
163
            $model->display_name = $request->input('display_name');
164
            $model->name         = $request->input('name');
165
            $model->icon         = $request->input('icon');
166
            $model->is_menu      = $request->input('is_menu');
167
            $model->sort         = $request->input('sort');
168
            $model->description  = $request->input('description');
169
170
            $model->save();
171
        } else {
172
            $this->create($request->input());
173
        }
174
175
        $this->clearCache();
176
        return true;
177
    }
178
179
    public function deleteMenu($id)
180
    {
181
        $info = $this->getInfoById($id);
182
        if (is_null($info)) {
183
            throw new AdminHttpException(404, '菜单不存在');
184
        }
185
186
        $childs = $this->getDescendants($id)->keys();
187
        $childs->push($id);
188
        DB::transaction(function () use ($childs) {
189
            $this->destroy($childs->toArray());
190
        });
191
192
        $this->clearCache();
193
194
        return true;
195
    }
196
197
    private function clearCache()
198
    {
199
        Cache::forget('permission_all');
200
    }
201
}
202