Completed
Push — master ( c436ad...d0a6e2 )
by Stephen
02:15
created

Permission::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace z1haze\Acl\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use z1haze\Acl\Exceptions\UserNotFoundException;
7
use z1haze\Acl\Traits\UserAndPermission;
8
9
class Permission extends Model
10
{
11
    use UserAndPermission;
12
13
    protected $guarded = ['id', 'created_id', 'updated_at'];
14
    protected $casts = ['id' => 'integer'];
15
    protected $table;
16
17
    /**
18
     * PERMISSION
19
     * A Permission belongs to a level
20
     *
21
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
22
     */
23 10
    public function level()
24
    {
25 10
        return $this->belongsTo(config('laravel-acl.level', Level::class));
26
    }
27
28
    /**
29
     * PERMISSION
30
     * A permission belongs to many users
31
     *
32
     * @return mixed
33
     */
34 8
    public function users()
35
    {
36 8
        return $this->belongsToMany(config('laravel-acl.user'));
37
    }
38
39
    /**
40
     * PERMISSION
41
     * Return the relationship of users
42
     *
43
     * @return mixed
44
     */
45 1
    public function getUsers()
46
    {
47 1
        return $this->users;
48
    }
49
50
    /**
51
     * PERMISSION
52
     * Assign a single user to a permission
53
     *
54
     * @param $user
55
     */
56 3
    public function addUser($user)
57
    {
58 3
        return $this->addUsers([$user]);
59
    }
60
61
    /**
62
     * PERMISSION
63
     * Assign an array of users to a permission
64
     *
65
     * @param $users
66
     */
67 6
    public function addUsers($users)
68
    {
69 6
        $userObjects = $this->buildUserArray($users);
70
71 5
        return $this->users()->attach($userObjects);
72
    }
73
74
    /**
75
     * PERMISSION
76
     * Remove a single user from a permission
77
     *
78
     * @param $user
79
     */
80 1
    public function removeUser($user)
81
    {
82 1
        return $this->removeUsers([$user]);
83
    }
84
85
    /**
86
     * PERMISSION
87
     * Remove an array of users from a permission
88
     *
89
     * @param $users
90
     */
91 2
    public function removeUsers($users)
92
    {
93 2
        $userObjects = $this->buildUserArray($users);
94
95 2
        return $this->users()->detach($userObjects);
96
    }
97
98
99
    /* ------------------------------------------------------------------------------------------------
100
     |  Other Functions
101
     | ------------------------------------------------------------------------------------------------
102
     */
103
    /**
104
     * PERMISSION
105
     * Helper function to get the user whether it is the user ID
106
     * or the user object itself.
107
     *
108
     * @param $user
109
     * @return User
110
     * @throws UserNotFoundException
111
     */
112 6
    protected function getUser($user)
113
    {
114 6
        if (is_int($user))
115 2
            $user = config('laravel-acl.user')::find($user);
116
117 6
        if (!$user)
118 1
            throw new UserNotFoundException;
119
120 5
        return $user;
121
    }
122
123
    /**
124
     * Helper function to process users and return an
125
     * array of user id's
126
     *
127
     * @param $users
128
     * @return array
129
     */
130 6
    protected function buildUserArray($users)
131
    {
132 6
        $userArray = [];
133
134 6
        foreach ($users as $user) {
135 6
            $user = $this->getUser($user);
136 5
            array_push($userArray, $user->id);
137
        }
138
139 5
        return $userArray;
140
    }
141
142
    /**
143
     * Handle model events
144
     */
145 53
    public static function boot()
146
    {
147 53
        parent::boot();
148
149 53
        static::deleting(function ($permission) {
150 1
            $permission->users()->detach();
151 1
            $permission->level()->dissociate()->save();
152 53
        });
153 53
    }
154
155 53
    public function __construct(array $attributes = [])
156
    {
157 53
        parent::__construct($attributes);
158
159 53
        $this->table = config('laravel-acl.tables.permission');
160 53
    }
161
}
162