Completed
Push — master ( 7c6d04...ef0efe )
by ARCANEDEV
07:38
created

PermissionsGroup::loadPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
use Arcanedev\LaravelAuth\Traits\Slugable;
5
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract;
6
use Arcanesoft\Contracts\Auth\Models\PermissionsGroup as PermissionsGroupContract;
7
use Illuminate\Database\Eloquent\Model as Eloquent;
8
9
/**
10
 * Class     PermissionsGroup
11
 *
12
 * @package  Arcanedev\LaravelAuth\Models
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @property  int                                       id
16
 * @property  string                                    name
17
 * @property  string                                    slug
18
 * @property  string                                    description
19
 * @property  \Carbon\Carbon                            created_at
20
 * @property  \Carbon\Carbon                            updated_at
21
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
22
 */
23
class PermissionsGroup extends Model implements PermissionsGroupContract
24
{
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Traits
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    use Slugable;
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Properties
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * The attributes that are mass assignable.
37
     *
38
     * @var array
39
     */
40
    protected $fillable = ['name', 'slug', 'description'];
41
42
    /* ------------------------------------------------------------------------------------------------
43
     |  Constructor
44
     | ------------------------------------------------------------------------------------------------
45
     */
46
    /**
47
     * Create a new Eloquent model instance.
48
     *
49
     * @param  array  $attributes
50
     */
51 528
    public function __construct(array $attributes = [])
52
    {
53 528
        $this->setTable(config('laravel-auth.permissions-groups.table', 'permissions_groups'));
54
55 528
        parent::__construct($attributes);
56 528
    }
57
58
    /* ------------------------------------------------------------------------------------------------
59
     |  Relationships
60
     | ------------------------------------------------------------------------------------------------
61
     */
62
    /**
63
     * Permissions Groups has many permissions.
64
     *
65
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
66
     */
67 104
    public function permissions()
68
    {
69 104
        return $this->hasMany(config('laravel-auth.permissions.model', Permission::class), 'group_id');
70
    }
71
72
    /* ------------------------------------------------------------------------------------------------
73
     |  Getters & Setters
74
     | ------------------------------------------------------------------------------------------------
75
     */
76
    /**
77
     * Set the name attribute.
78
     *
79
     * @param  string  $name
80
     */
81 104
    public function setNameAttribute($name)
82
    {
83 104
        $this->attributes['name'] = $name;
84 104
        $this->setSlugAttribute($name);
85 104
    }
86
87
    /**
88
     * Set the slug attribute.
89
     *
90
     * @param  string  $slug
91
     */
92 104
    public function setSlugAttribute($slug)
93
    {
94 104
        $this->attributes['slug'] = $this->slugify($slug);
95 104
    }
96
97
    /* ------------------------------------------------------------------------------------------------
98
     |  CRUD Functions
99
     | ------------------------------------------------------------------------------------------------
100
     */
101
    /**
102
     * Create and attach a permission.
103
     *
104
     * @param  array  $attributes
105
     * @param  bool   $reload
106
     */
107 16
    public function createPermission(array $attributes, $reload = true)
108
    {
109 16
        $this->permissions()->create($attributes);
110
111 16
        $this->loadPermissions($reload);
112 16
    }
113
114
    /**
115
     * Attach the permission to a group.
116
     *
117
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
118
     * @param  bool                                          $reload
119
     */
120 48
    public function attachPermission(&$permission, $reload = true)
121
    {
122 48
        if ($this->hasPermission($permission)) {
123 8
            return;
124
        }
125
126 48
        $permission = $this->permissions()->save($permission);
127
128 48
        $this->loadPermissions($reload);
129 48
    }
130
131
    /**
132
     * Attach the permission by id to a group.
133
     *
134
     * @param  int   $id
135
     * @param  bool  $reload
136
     *
137
     * @return \Arcanesoft\Contracts\Auth\Models\Permission
138
     */
139 8
    public function attachPermissionById($id, $reload = true)
140
    {
141 8
        $permission = $this->getPermissionById($id);
142
143 8
        if ( ! is_null($permission)) {
144 8
            $this->attachPermission($permission, $reload);
145 6
        }
146
147 8
        return $permission;
148
    }
149
150
    /**
151
     * Attach a collection of permissions to the group.
152
     *
153
     * @param  \Illuminate\Database\Eloquent\Collection|array  $permissions
154
     * @param  bool                                            $reload
155
     *
156
     * @return \Illuminate\Database\Eloquent\Collection|array
157
     */
158 24
    public function attachPermissions($permissions, $reload = true)
159
    {
160 24
        $permissions = $this->permissions()->saveMany($permissions);
161
162 24
        $this->loadPermissions($reload);
163
164 24
        return $permissions;
165
    }
166
167
    /**
168
     * Attach the permission from a group.
169
     *
170
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
171
     * @param  bool                                          $reload
172
     */
173 24
    public function detachPermission(&$permission, $reload = true)
174
    {
175 24
        if ( ! $this->hasPermission($permission)) {
176 16
            return;
177
        }
178
179 24
        $permission = $this->getPermissionFromGroup($permission);
180 24
        $permission->update([
0 ignored issues
show
Bug introduced by
The method update() does not seem to exist on object<Arcanesoft\Contra...Auth\Models\Permission>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181 24
            'group_id' => 0,
182 18
        ]);
183
184 24
        $this->loadPermissions($reload);
185 24
    }
186
187
    /**
188
     * Attach the permission by id to a group.
189
     *
190
     * @param  int   $id
191
     * @param  bool  $reload
192
     *
193
     * @return \Arcanesoft\Contracts\Auth\Models\Permission
194
     */
195 8
    public function detachPermissionById($id, $reload = true)
196
    {
197 8
        $permission = $this->getPermissionById($id);
198
199 8
        if ( ! is_null($permission)) {
200 8
            $this->detachPermission($permission, $reload);
201 6
        }
202
203 8
        return $permission;
204
    }
205
206
    /**
207
     * Detach multiple permissions by ids.
208
     *
209
     * @param  array  $ids
210
     * @param  bool   $reload
211
     *
212
     * @return int
213
     */
214 8
    public function detachPermissions(array $ids, $reload = true)
215
    {
216 8
        $detached = $this->permissions()->whereIn('id', $ids)->update([
217
            'group_id' => 0
218 8
        ]);
219
220 8
        $this->loadPermissions($reload);
221
222 8
        return $detached;
223
    }
224
225
    /**
226
     * Detach all permissions from the group.
227
     *
228
     * @param  bool  $reload
229
     *
230
     * @return int
231
     */
232 16
    public function detachAllPermissions($reload = true)
233
    {
234 16
        $detached = $this->permissions()->update([
235
            'group_id' => 0
236 16
        ]);
237
238 16
        $this->loadPermissions($reload);
239
240 16
        return $detached;
241
    }
242
243
    /* ------------------------------------------------------------------------------------------------
244
     |  Check Functions
245
     | ------------------------------------------------------------------------------------------------
246
     */
247
    /**
248
     * Check if role has the given permission (Permission Model or Id).
249
     *
250
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $id
251
     *
252
     * @return bool
253
     */
254 64
    public function hasPermission($id)
255
    {
256 64
        if ($id instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
257 64
            $id = $id->getKey();
258 48
        }
259
260 64
        return ! is_null($this->getPermissionFromGroup($id));
261
    }
262
263
    /* ------------------------------------------------------------------------------------------------
264
     |  Other Functions
265
     | ------------------------------------------------------------------------------------------------
266
     */
267
    /**
268
     * Get a permission from the group.
269
     *
270
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $id
271
     *
272
     * @return \Arcanesoft\Contracts\Auth\Models\Permission|null
273
     */
274 64
    private function getPermissionFromGroup($id)
275
    {
276 64
        if ($id instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
277 24
            $id = $id->getKey();
278 18
        }
279
280 64
        $this->loadPermissions();
281
282 64
        return $this->permissions->filter(function (PermissionContract $permission) use ($id) {
283 64
            return $permission->id == $id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\Permission suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
284 64
        })->first();
285
    }
286
287
    /**
288
     * Get a permission by id.
289
     *
290
     * @param  int  $id
291
     *
292
     * @return \Arcanesoft\Contracts\Auth\Models\Permission|null
293
     */
294 16
    private function getPermissionById($id)
295
    {
296 16
        return $this->permissions()
297 16
            ->getRelated()
298 16
            ->where('id', $id)
299 16
            ->first();
300
    }
301
302
    /**
303
     * Load the permissions.
304
     *
305
     * @param  bool  $load
306
     *
307
     * @return self
308
     */
309 88
    protected function loadPermissions($load = true)
310
    {
311 88
        return $load ? $this->load('permissions') : $this;
312
    }
313
}
314