Completed
Push — master ( cf4870...574113 )
by wen
03:00
created

Permission::deleteMenu()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 0
cts 0
cp 0
rs 8.7972
cc 4
eloc 14
nc 8
nop 1
crap 20
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
/**
13
 * Sco\Admin\Models\Permission
14
 *
15
 * @property int $id 主键
16
 * @property int $pid 父ID
17
 * @property string $icon 图标class
18
 * @property string $display_name 显示名称
19
 * @property string $name 名称
20
 * @property bool $is_menu 是否作为菜单
21
 * @property bool $sort 排序
22
 * @property string $description 描述
23
 * @property \Carbon\Carbon $created_at
24
 * @property \Carbon\Carbon $updated_at
25
 * @property-read \Illuminate\Database\Eloquent\Collection|\Sco\Admin\Models\Role[] $roles
26
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereCreatedAt($value)
27
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereDescription($value)
28
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereDisplayName($value)
29
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereIcon($value)
30
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereId($value)
31
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereIsMenu($value)
32
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereName($value)
33
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission wherePid($value)
34
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereSort($value)
35
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereUpdatedAt($value)
36
 * @mixin \Eloquent
37
 */
38
class Permission extends EntrustPermission
39
{
40
    use TreeTrait;
41
42
    protected $hidden = ['created_at', 'updated_at'];
43
44
    protected $guarded = ['created_at', 'updated_at'];
45
46
    protected $treeNodeParentIdName = 'pid';
47
48
    private $allRoutes = null;
49
50
    private $validList = null;
0 ignored issues
show
Unused Code introduced by
The property $validList is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
51
52
    private $permList = null;
0 ignored issues
show
Unused Code introduced by
The property $permList is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
53
54
    private $menuList = null;
55
56
57
    /**
58
     * @return \Illuminate\Support\Collection
59
     */
60
    public function getMenuTreeList()
61
    {
62
        $routes = $this->getDescendants(0);
63
        //dd($routes);
64
        return $routes;
65
    }
66
67
    private function getAll()
68
    {
69
        if ($this->allRoutes) {
70
            return $this->allRoutes;
71
        }
72
73
        $this->allRoutes = Cache::rememberForever('permission_all',
74
            function () {
75
                return $this->orderBy('sort')->get();
76
            });
77
        return $this->allRoutes;
78
    }
79
80
    /**
81
     * Tree Trait 获取所有节点
82
     *
83
     * @return mixed|null
84
     */
85
    protected function getTreeAllNodes()
86
    {
87
        return $this->getAll();
88
    }
89
90
    /**
91
     * 获取有效的路由列表
92
     *
93
     * @return \Illuminate\Support\Collection
94
     */
95
    /*public function getValidRouteList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
96
    {
97
        if ($this->validList) {
98
            return $this->validList;
99
        }
100
101
        $all = $this->getAll();
102
103
        $this->validList = collect([]);
104
        foreach ($all as $route) {
105
            if (!empty($route->uri) && $route->uri != '#') {
106
                $this->validList->push($route);
107
            }
108
        }
109
        return $this->validList;
110
    }*/
111
112
    /**
113
     * 获取权限列表
114
     *
115
     * @return \Illuminate\Support\Collection|null
116
     */
117
    /*public function getPermRouteList()
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
118
    {
119
        if ($this->permList) {
120
            return $this->permList;
121
        }
122
123
        return $this->permList = $this->getLayerOfDescendants(0);
124
    }*/
125
126
    public function getMenuList()
127
    {
128
        if ($this->menuList) {
129
            return $this->menuList;
130
        }
131
        $all = $this->getAll();
132
133
        $routes = collect([]);
134
        foreach ($all as $route) {
135
            if ($route->is_menu) {
136
                $routes->push($route);
137
            }
138
        }
139
140
        $this->setAllNodes($routes);
141
        return $this->menuList = $this->getLayerOfDescendants(0);
142
    }
143
144
    /*public function getInfoById($id)
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
145
    {
146
        return $this->getSelf($id);
147
    }
148
149
    public function getInfoByName($name)
150
    {
151
        $all = $this->getAll();
152
        $key = $all->search(function ($item) use ($name) {
153
            return $item->name == $name;
154
        });
155
        return $key === false ? false : $all->get($key);
156
    }*/
157
158
    /*public function getParentTree($id)
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
159
    {
160
        return $this->getAncestors($id);
161
    }
162
163
    public function getParentTreeAndSelfById($id)
164
    {
165
        $self = $this->getInfoById($id);
166
        if ($self) {
167
            $parent = $this->getParentTree($self->id);
168
            $parent->push($self);
169
            return $parent;
170
        }
171
        return false;
172
    }
173
174
    public function getParentTreeAndSelfByName($name)
175
    {
176
        $self = $this->getInfoByName($name);
177
        if ($self) {
178
            $parent = $this->getParentTree($self->id);
179
            $parent->push($self);
180
            return $parent;
181
        }
182
        return false;
183
    }*/
184
185
    public function saveMenu(Request $request)
186
    {
187
        if (empty($request->input('id'))) {
188
            $model = $this;
189
        } else {
190
            $model = $this->findOrFail($request->input('id'));
191
        }
192
193
        $model->pid          = $request->input('pid');
194
        $model->display_name = $request->input('display_name');
195
        $model->name         = $request->input('name');
196
        $model->icon         = $request->input('icon') ?: '';
197
        $model->is_menu      = $request->input('is_menu', 0);
198
        $model->sort         = $request->input('sort', 255);
199
        $model->description  = $request->input('description') ?: '';
200
201
        $model->save();
202
203
        $this->clearCache();
204
        return true;
205
    }
206
207
    /**
208
     * 删除菜单
209
     *
210
     * @param int|array $ids 菜单ID
211
     *
212
     * @return bool
213
     */
214
    public function deleteMenu($ids)
215
    {
216
        if (!is_array($ids)) {
217
            $ids = [intval($ids)];
218
        }
219
        $items = collect();
220
        foreach ($ids as $id) {
221
            $items->push($id);
222
            $items = $items->merge($this->getDescendants($id)->keys());
223
        }
224
        $items = $items->unique();
225
        if ($items->isEmpty()) {
226
            throw new AdminHttpException('菜单不存在');
227
        }
228
229
        DB::transaction(function () use ($items) {
230
            $this->destroy($items->toArray());
231
        });
232
233
        $this->clearCache();
234
235
        return true;
236
    }
237
238
    private function clearCache()
239
    {
240
        Cache::forget('permission_all');
241
    }
242
}
243