Completed
Pull Request — master (#1)
by ARCANEDEV
04:08
created

PermissionsGroup::detachPermissionById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
5
/**
6
 * Class     PermissionsGroup
7
 *
8
 * @package  Arcanedev\LaravelAuth\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int                                       id
12
 * @property  string                                    name
13
 * @property  string                                    slug
14
 * @property  string                                    description
15
 * @property  \Carbon\Carbon                            created_at
16
 * @property  \Carbon\Carbon                            updated_at
17
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
18
 */
19
class PermissionsGroup extends Model
20
{
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Properties
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * The attributes that are mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = ['name', 'slug', 'description'];
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Constructor
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Create a new Eloquent model instance.
38
     *
39
     * @param  array  $attributes
40
     */
41 48
    public function __construct(array $attributes = [])
42
    {
43 48
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
44
45 48
        parent::__construct($attributes);
46 48
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Relationships
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Permissions Groups has many permissions.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
56
     */
57 40
    public function permissions()
58
    {
59 40
        return $this->hasMany(Permission::class, 'group_id');
60
    }
61
62
    /* ------------------------------------------------------------------------------------------------
63
     |  CRUD Functions
64
     | ------------------------------------------------------------------------------------------------
65
     */
66
    /**
67
     * Create and attach a permission.
68
     *
69
     * @param  array  $attributes
70
     * @param  bool   $reload
71
     */
72 8
    public function createPermission(array $attributes, $reload = true)
73
    {
74 8
        $this->permissions()->create($attributes);
75
76 8
        if ($reload) {
77 8
            $this->load('permissions');
78 6
        }
79 8
    }
80
81
    /**
82
     * Attach the permission to a group.
83
     *
84
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
85
     * @param  bool                                      $reload
86
     */
87 20
    public function attachPermission(&$permission, $reload = true)
88
    {
89 20
        if ($this->hasPermission($permission)) {
90 4
            return;
91
        }
92
93 20
        $permission = $this->permissions()->save($permission);
94
95 20
        if ($reload) {
96 20
            $this->load('permissions');
97 15
        }
98 20
    }
99
100
    /**
101
     * Attach a collection of permissions to the group.
102
     *
103
     * @param  \Illuminate\Database\Eloquent\Collection|array  $permissions
104
     * @param  bool                                            $reload
105
     *
106
     * @return \Illuminate\Database\Eloquent\Collection|array
107
     */
108 4
    public function attachPermissions($permissions, $reload = true)
109
    {
110 4
        $permissions = $this->permissions()->saveMany($permissions);
111
112 4
        if ($reload) {
113 4
            $this->load('permissions');
114 3
        }
115
116 4
        return $permissions;
117
    }
118
119
    /**
120
     * Attach the permission by id to a group.
121
     *
122
     * @param  int   $id
123
     * @param  bool  $reload
124
     *
125
     * @return \Arcanedev\LaravelAuth\Models\Permission
126
     */
127 4
    public function attachPermissionById($id, $reload = true)
128
    {
129 4
        $permission = $this->getPermissionById($id);
130
131 4
        $this->attachPermission($permission, $reload);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by $this->getPermissionById($id) on line 129 can be null; however, Arcanedev\LaravelAuth\Mo...oup::attachPermission() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
132
133 4
        return $permission;
134
    }
135
136
    /**
137
     * Attach the permission from a group.
138
     *
139
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
140
     * @param  bool                                      $reload
141
     */
142 12
    public function detachPermission(&$permission, $reload = true)
143
    {
144 12
        if ( ! $this->hasPermission($permission)) {
145 8
            return;
146
        }
147
148 12
        $permission = $this->getPermissionFromGroup($permission);
149
150 12
        $permission->update([
151 12
            'group_id' => 0,
152 9
        ]);
153
154 12
        if ($reload) {
155 12
            $this->load('permissions');
156 9
        }
157 12
    }
158
159
    /**
160
     * Attach the permission by id to a group.
161
     *
162
     * @param  int   $id
163
     * @param  bool  $reload
164
     *
165
     * @return \Arcanedev\LaravelAuth\Models\Permission
166
     */
167 4
    public function detachPermissionById($id, $reload = true)
168
    {
169 4
        $permission = $this->getPermissionById($id);
170
171 4
        $this->detachPermission($permission, $reload);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by $this->getPermissionById($id) on line 169 can be null; however, Arcanedev\LaravelAuth\Mo...oup::detachPermission() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
172
173 4
        return $permission;
174
    }
175
176
    /* ------------------------------------------------------------------------------------------------
177
     |  Check Functions
178
     | ------------------------------------------------------------------------------------------------
179
     */
180
    /**
181
     * Check if role has the given permission (Permission Model or Id).
182
     *
183
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
184
     *
185
     * @return bool
186
     */
187 24
    public function hasPermission($id)
188
    {
189 24
        if ($id instanceof Permission) {
190 24
            $id = $id->getKey();
191 18
        }
192
193 24
        return ! is_null($this->getPermissionFromGroup($id));
194
    }
195
196
    /**
197
     * Get a permission from the group.
198
     *
199
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
200
     *
201
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
202
     */
203 24
    private function getPermissionFromGroup($id)
204
    {
205 24
        if ($id instanceof Permission) {
206 12
            $id = $id->getKey();
207 9
        }
208
209 24
        $this->load('permissions');
210
211 24
        return $this->permissions->filter(function (Permission $permission) use ($id) {
212 24
            return $permission->id == $id;
213 24
        })->first();
214
    }
215
216
    /**
217
     * Get a permission by id.
218
     *
219
     * @param  int  $id
220
     *
221
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
222
     */
223 8
    private function getPermissionById($id)
224
    {
225 8
        return $this->permissions()
226 8
            ->getRelated()
227 8
            ->where('id', $id)
228 8
            ->first();
229
    }
230
}
231