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

PermissionsGroup::attachPermissionById()   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 2
Bugs 0 Features 0
Metric Value
c 2
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
use Arcanedev\LaravelAuth\Traits\Slugable;
5
6
/**
7
 * Class     PermissionsGroup
8
 *
9
 * @package  Arcanedev\LaravelAuth\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @property  int                                       id
13
 * @property  string                                    name
14
 * @property  string                                    slug
15
 * @property  string                                    description
16
 * @property  \Carbon\Carbon                            created_at
17
 * @property  \Carbon\Carbon                            updated_at
18
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
19
 */
20
class PermissionsGroup extends Model
21
{
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Traits
24
     | ------------------------------------------------------------------------------------------------
25
     */
26
    use Slugable;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Properties
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * The attributes that are mass assignable.
34
     *
35
     * @var array
36
     */
37
    protected $fillable = ['name', 'slug', 'description'];
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Constructor
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Create a new Eloquent model instance.
45
     *
46
     * @param  array  $attributes
47
     */
48 252
    public function __construct(array $attributes = [])
49
    {
50 252
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
51
52 252
        parent::__construct($attributes);
53 252
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Relationships
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Permissions Groups has many permissions.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
63
     */
64 44
    public function permissions()
65
    {
66 44
        return $this->hasMany(Permission::class, 'group_id');
67
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Getters & Setters
71
     | ------------------------------------------------------------------------------------------------
72
     */
73 44
    public function setNameAttribute($value)
74
    {
75 44
        $this->attributes['name'] = $value;
76 44
        $this->setSlugAttribute($value);
77
78 44
    }
79
80 44
    public function setSlugAttribute($value)
81
    {
82 44
        $this->attributes['slug'] = $this->slugify($value);
83 44
    }
84
85
    /* ------------------------------------------------------------------------------------------------
86
     |  CRUD Functions
87
     | ------------------------------------------------------------------------------------------------
88
     */
89
    /**
90
     * Create and attach a permission.
91
     *
92
     * @param  array  $attributes
93
     * @param  bool   $reload
94
     */
95 8
    public function createPermission(array $attributes, $reload = true)
96
    {
97 8
        $this->permissions()->create($attributes);
98
99 8
        if ($reload) {
100 8
            $this->load('permissions');
101 6
        }
102 8
    }
103
104
    /**
105
     * Attach the permission to a group.
106
     *
107
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
108
     * @param  bool                                      $reload
109
     */
110 24
    public function attachPermission(&$permission, $reload = true)
111
    {
112 24
        if ($this->hasPermission($permission)) {
113 4
            return;
114
        }
115
116 24
        $permission = $this->permissions()->save($permission);
117
118 24
        if ($reload) {
119 24
            $this->load('permissions');
120 18
        }
121 24
    }
122
123
    /**
124
     * Attach a collection of permissions to the group.
125
     *
126
     * @param  \Illuminate\Database\Eloquent\Collection|array  $permissions
127
     * @param  bool                                            $reload
128
     *
129
     * @return \Illuminate\Database\Eloquent\Collection|array
130
     */
131 4
    public function attachPermissions($permissions, $reload = true)
132
    {
133 4
        $permissions = $this->permissions()->saveMany($permissions);
134
135 4
        if ($reload) {
136 4
            $this->load('permissions');
137 3
        }
138
139 4
        return $permissions;
140
    }
141
142
    /**
143
     * Attach the permission by id to a group.
144
     *
145
     * @param  int   $id
146
     * @param  bool  $reload
147
     *
148
     * @return \Arcanedev\LaravelAuth\Models\Permission
149
     */
150 4
    public function attachPermissionById($id, $reload = true)
151
    {
152 4
        $permission = $this->getPermissionById($id);
153
154 4
        $this->attachPermission($permission, $reload);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by $this->getPermissionById($id) on line 152 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...
155
156 4
        return $permission;
157
    }
158
159
    /**
160
     * Attach the permission from a group.
161
     *
162
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
163
     * @param  bool                                      $reload
164
     */
165 12
    public function detachPermission(&$permission, $reload = true)
166
    {
167 12
        if ( ! $this->hasPermission($permission)) {
168 8
            return;
169
        }
170
171 12
        $permission = $this->getPermissionFromGroup($permission);
172
173 12
        $permission->update([
174 12
            'group_id' => 0,
175 9
        ]);
176
177 12
        if ($reload) {
178 12
            $this->load('permissions');
179 9
        }
180 12
    }
181
182
    /**
183
     * Attach the permission by id to a group.
184
     *
185
     * @param  int   $id
186
     * @param  bool  $reload
187
     *
188
     * @return \Arcanedev\LaravelAuth\Models\Permission
189
     */
190 4
    public function detachPermissionById($id, $reload = true)
191
    {
192 4
        $permission = $this->getPermissionById($id);
193
194 4
        $this->detachPermission($permission, $reload);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by $this->getPermissionById($id) on line 192 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...
195
196 4
        return $permission;
197
    }
198
199
    /* ------------------------------------------------------------------------------------------------
200
     |  Check Functions
201
     | ------------------------------------------------------------------------------------------------
202
     */
203
    /**
204
     * Check if role has the given permission (Permission Model or Id).
205
     *
206
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
207
     *
208
     * @return bool
209
     */
210 28
    public function hasPermission($id)
211
    {
212 28
        if ($id instanceof Permission) {
213 28
            $id = $id->getKey();
214 21
        }
215
216 28
        return ! is_null($this->getPermissionFromGroup($id));
217
    }
218
219
    /**
220
     * Get a permission from the group.
221
     *
222
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
223
     *
224
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
225
     */
226 28
    private function getPermissionFromGroup($id)
227
    {
228 28
        if ($id instanceof Permission) {
229 12
            $id = $id->getKey();
230 9
        }
231
232 28
        $this->load('permissions');
233
234 28
        return $this->permissions->filter(function (Permission $permission) use ($id) {
235 28
            return $permission->id == $id;
236 28
        })->first();
237
    }
238
239
    /**
240
     * Get a permission by id.
241
     *
242
     * @param  int  $id
243
     *
244
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
245
     */
246 8
    private function getPermissionById($id)
247
    {
248 8
        return $this->permissions()
249 8
            ->getRelated()
250 8
            ->where('id', $id)
251 8
            ->first();
252
    }
253
}
254