Completed
Push — master ( ec2661...46aff2 )
by wen
02:42
created

Permission::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace Sco\Admin\Models;
4
5
use DB;
6
use Cache;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
use Sco\Admin\Observers\PermissionObserver;
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 $guarded = ['created_at', 'updated_at'];
43
44
    protected $treeNodeParentIdName = 'pid';
45
46
    private $allRoutes = null;
47
48
    private $permList = null;
49
50
    private $menuList = null;
51
52
    protected $fillable = ['pid', 'display_name', 'name', 'icon', 'is_menu', 'sort', 'description'];
53
54
    protected $events = [
55
        'created'  => \Sco\ActionLog\Events\ModelWasCreated::class,
56
        'updated'  => \Sco\ActionLog\Events\ModelWasUpdated::class,
57
        'deleted'  => \Sco\ActionLog\Events\ModelWasDeleted::class,
58
    ];
59
60
    /**
61
     * @return \Illuminate\Support\Collection
62
     */
63
    public function getMenuTreeList()
64
    {
65
        $routes = $this->getDescendants(0);
66
        //dd($routes);
67
        return $routes;
68
    }
69
70
    private function getAll()
71
    {
72
        if ($this->allRoutes) {
73
            return $this->allRoutes;
74
        }
75
76
        $this->allRoutes = Cache::rememberForever(
77
            'permission_all',
78
            function () {
79
                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...
80
            }
81
        );
82
        return $this->allRoutes;
83
    }
84
85
    /**
86
     * Tree Trait 获取所有节点
87
     *
88
     * @return mixed|null
89
     */
90
    protected function getTreeAllNodes()
91
    {
92
        return $this->getAll();
93
    }
94
95
    /**
96
     * 获取权限列表
97
     *
98
     * @return \Illuminate\Support\Collection|null
99
     */
100
    public function getPermRouteList()
101
    {
102
        if ($this->permList) {
103
            return $this->permList;
104
        }
105
106
        return $this->permList = $this->getLayerOfDescendants(0);
107
    }
108
109
    public function getMenuList()
110
    {
111
        if ($this->menuList) {
112
            return $this->menuList;
113
        }
114
        $all = $this->getAll();
115
116
        $routes = collect([]);
117
        foreach ($all as $route) {
118
            if ($route->is_menu) {
119
                $routes->push($route);
120
            }
121
        }
122
123
        $this->setAllNodes($routes);
124
        return $this->menuList = $this->getLayerOfDescendants(0);
125
    }
126
127
    /**
128
     * 删除菜单
129
     *
130
     * @param int|array $ids 菜单ID
131
     *
132
     * @return bool
133
     */
134
    public function destroyMenu($ids)
135
    {
136
        if (!is_array($ids)) {
137
            $ids = [intval($ids)];
138
        }
139
        $items = collect();
140
        foreach ($ids as $id) {
141
            $items->push($id);
142
            $items = $items->merge($this->getDescendants($id)->keys());
143
        }
144
        $items = $items->unique();
145
        if ($items->isEmpty()) {
146
            throw new ModelNotFoundException('菜单不存在');
147
        }
148
149
        DB::transaction(function () use ($items) {
150
            $this->destroy($items->toArray());
151
        });
152
153
        return true;
154
    }
155
156
    public static function boot()
157
    {
158
        parent::boot();
159
160
        static::observe(PermissionObserver::class);
161
    }
162
}
163