Completed
Push — master ( 80411a...1281a7 )
by wen
03:35
created

Permission::destroyMenu()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
ccs 0
cts 18
cp 0
cc 4
eloc 13
nc 8
nop 1
crap 20
1
<?php
2
3
namespace Sco\Admin\Models;
4
5
use DB;
6
use Cache;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Http\Request;
9
use Sco\Admin\Exceptions\AdminHttpException;
10
use Sco\Admin\Observers\PermissionObserver;
11
use Sco\Admin\Traits\EntrustPermissionTrait;
12
use Sco\Tree\Traits\TreeTrait;
13
14
/**
15
 * Sco\Admin\Models\Permission
16
 *
17
 * @property int $id 主键
18
 * @property int $pid 父ID
19
 * @property string $icon 图标class
20
 * @property string $display_name 显示名称
21
 * @property string $name 名称
22
 * @property bool $is_menu 是否作为菜单
23
 * @property bool $sort 排序
24
 * @property string $description 描述
25
 * @property \Carbon\Carbon $created_at
26
 * @property \Carbon\Carbon $updated_at
27
 * @property-read \Illuminate\Database\Eloquent\Collection|\Sco\Admin\Models\Role[] $roles
28
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereCreatedAt($value)
29
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereDescription($value)
30
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereDisplayName($value)
31
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereIcon($value)
32
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereId($value)
33
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereIsMenu($value)
34
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereName($value)
35
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission wherePid($value)
36
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereSort($value)
37
 * @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereUpdatedAt($value)
38
 * @mixin \Eloquent
39
 */
40
class Permission extends Model
41
{
42
    use TreeTrait, EntrustPermissionTrait;
43
44
    protected $hidden = ['created_at', 'updated_at'];
45
46
    protected $guarded = ['created_at', 'updated_at'];
47
48
    protected $treeNodeParentIdName = 'pid';
49
50
    private $allRoutes = null;
51
52
    private $permList = null;
53
54
    private $menuList = null;
55
56
    protected $fillable = ['pid', 'display_name', 'name', 'icon', 'is_menu', 'sort', 'description'];
57
58
    protected $events = [
59
        'created'  => \Sco\ActionLog\Events\ModelWasCreated::class,
60
        'updated'  => \Sco\ActionLog\Events\ModelWasUpdated::class,
61
        'deleted'  => \Sco\ActionLog\Events\ModelWasDeleted::class,
62
    ];
63
64
65
    public function __construct(array $attributes = [])
66
    {
67
        parent::__construct($attributes);
68
        $this->table = config('admin.permissions_table');
69
    }
70
71
72
    /**
73
     * @return \Illuminate\Support\Collection
74
     */
75
    public function getMenuTreeList()
76
    {
77
        $routes = $this->getDescendants(0);
78
        //dd($routes);
79
        return $routes;
80
    }
81
82
    private function getAll()
83
    {
84
        if ($this->allRoutes) {
85
            return $this->allRoutes;
86
        }
87
88
        $this->allRoutes = Cache::rememberForever('permission_all',
89
            function () {
90
                return $this->orderBy('sort')->get();
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Sco\Admin\Models\Permission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
91
            });
92
        return $this->allRoutes;
93
    }
94
95
    /**
96
     * Tree Trait 获取所有节点
97
     *
98
     * @return mixed|null
99
     */
100
    protected function getTreeAllNodes()
101
    {
102
        return $this->getAll();
103
    }
104
105
    /**
106
     * 获取权限列表
107
     *
108
     * @return \Illuminate\Support\Collection|null
109
     */
110
    public function getPermRouteList()
111
    {
112
        if ($this->permList) {
113
            return $this->permList;
114
        }
115
116
        return $this->permList = $this->getLayerOfDescendants(0);
117
    }
118
119
    public function getMenuList()
120
    {
121
        if ($this->menuList) {
122
            return $this->menuList;
123
        }
124
        $all = $this->getAll();
125
126
        $routes = collect([]);
127
        foreach ($all as $route) {
128
            if ($route->is_menu) {
129
                $routes->push($route);
130
            }
131
        }
132
133
        $this->setAllNodes($routes);
134
        return $this->menuList = $this->getLayerOfDescendants(0);
135
    }
136
137
    /**
138
     * 删除菜单
139
     *
140
     * @param int|array $ids 菜单ID
141
     *
142
     * @return bool
143
     */
144
    public function destroyMenu($ids)
145
    {
146
        if (!is_array($ids)) {
147
            $ids = [intval($ids)];
148
        }
149
        $items = collect();
150
        foreach ($ids as $id) {
151
            $items->push($id);
152
            $items = $items->merge($this->getDescendants($id)->keys());
153
        }
154
        $items = $items->unique();
155
        if ($items->isEmpty()) {
156
            throw new AdminHttpException('菜单不存在');
157
        }
158
159
        DB::transaction(function () use ($items) {
160
            $this->destroy($items->toArray());
161
        });
162
163
        return true;
164
    }
165
166
    public static function boot()
167
    {
168
        parent::boot();
169
170
        static::observe(PermissionObserver::class);
171
    }
172
}
173