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

PermissionsGroup::setSlugAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 256
    public function __construct(array $attributes = [])
49
    {
50 256
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
51
52 256
        parent::__construct($attributes);
53 256
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Relationships
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Permissions Groups has many permissions.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
63
     */
64 48
    public function permissions()
65
    {
66 48
        return $this->hasMany(Permission::class, 'group_id');
67
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Getters & Setters
71
     | ------------------------------------------------------------------------------------------------
72
     */
73 48
    public function setNameAttribute($value)
74
    {
75 48
        $this->attributes['name'] = $value;
76 48
        $this->setSlugAttribute($value);
77
78 48
    }
79
80 48
    public function setSlugAttribute($value)
81
    {
82 48
        $this->attributes['slug'] = $this->slugify($value);
83 48
    }
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 8
    public function attachPermissions($permissions, $reload = true)
132
    {
133 8
        $permissions = $this->permissions()->saveMany($permissions);
134
135 8
        if ($reload) {
136 8
            $this->load('permissions');
137 6
        }
138
139 8
        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
     * Detach all permissions from the group.
201
     *
202
     * @param  bool  $reload
203
     *
204
     * @return int
205
     */
206 8
    public function detachAllPermissions($reload = true)
207
    {
208 8
        $detached = $this->permissions()->update([
209
            'group_id' => 0
210 8
        ]);
211
212 8
        if ($reload) {
213 4
            $this->load('permissions');
214 3
        }
215
216 8
        return $detached;
217
    }
218
219
    /* ------------------------------------------------------------------------------------------------
220
     |  Check Functions
221
     | ------------------------------------------------------------------------------------------------
222
     */
223
    /**
224
     * Check if role has the given permission (Permission Model or Id).
225
     *
226
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
227
     *
228
     * @return bool
229
     */
230 32
    public function hasPermission($id)
231
    {
232 32
        if ($id instanceof Permission) {
233 32
            $id = $id->getKey();
234 24
        }
235
236 32
        return ! is_null($this->getPermissionFromGroup($id));
237
    }
238
239
    /**
240
     * Get a permission from the group.
241
     *
242
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
243
     *
244
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
245
     */
246 32
    private function getPermissionFromGroup($id)
247
    {
248 32
        if ($id instanceof Permission) {
249 12
            $id = $id->getKey();
250 9
        }
251
252 32
        $this->load('permissions');
253
254 32
        return $this->permissions->filter(function (Permission $permission) use ($id) {
255 32
            return $permission->id == $id;
256 32
        })->first();
257
    }
258
259
    /**
260
     * Get a permission by id.
261
     *
262
     * @param  int  $id
263
     *
264
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
265
     */
266 8
    private function getPermissionById($id)
267
    {
268 8
        return $this->permissions()
269 8
            ->getRelated()
270 8
            ->where('id', $id)
271 8
            ->first();
272
    }
273
}
274