Completed
Push — master ( 5acf75...dcb410 )
by Stephen
01:54
created

Permission::users()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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