Completed
Push — master ( d42b43...5ac77a )
by ARCANEDEV
8s
created

PermissionsGroup::hasPermission()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract;
5
use Arcanesoft\Contracts\Auth\Models\PermissionsGroup as PermissionsGroupContract;
6
use Illuminate\Database\Eloquent\Model as Eloquent;
7
use Illuminate\Support\Str;
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
     |  Properties
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * The attributes that are mass assignable.
31
     *
32
     * @var array
33
     */
34
    protected $fillable = ['name', 'slug', 'description'];
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Constructor
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Create a new Eloquent model instance.
42
     *
43
     * @param  array  $attributes
44
     */
45
    public function __construct(array $attributes = [])
46
    {
47
        $this->setTable(
48
            config('laravel-auth.permissions-groups.table', 'permissions_groups')
49
        );
50
51 528
        parent::__construct($attributes);
52
    }
53 528
54
    /* ------------------------------------------------------------------------------------------------
55 528
     |  Relationships
56 528
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Permissions Groups has many permissions.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
62
     */
63
    public function permissions()
64
    {
65
        return $this->hasMany(
66
            config('laravel-auth.permissions.model', Permission::class),
67 104
            'group_id'
68
        );
69 104
    }
70
71
    /* ------------------------------------------------------------------------------------------------
72
     |  Getters & Setters
73
     | ------------------------------------------------------------------------------------------------
74
     */
75
    /**
76
     * Set the name attribute.
77
     *
78
     * @param  string  $name
79
     */
80
    public function setNameAttribute($name)
81 104
    {
82
        $this->attributes['name'] = $name;
83 104
        $this->setSlugAttribute($name);
84 104
    }
85 104
86
    /**
87
     * Set the slug attribute.
88
     *
89
     * @param  string  $slug
90
     */
91
    public function setSlugAttribute($slug)
92 104
    {
93
        $this->attributes['slug'] = $this->slugify($slug);
94 104
    }
95 104
96
    /* ------------------------------------------------------------------------------------------------
97
     |  CRUD Functions
98
     | ------------------------------------------------------------------------------------------------
99
     */
100
    /**
101
     * Create and attach a permission.
102
     *
103
     * @param  array  $attributes
104
     * @param  bool   $reload
105
     */
106
    public function createPermission(array $attributes, $reload = true)
107 16
    {
108
        $this->permissions()->create($attributes);
109 16
110
        $this->loadPermissions($reload);
111 16
    }
112 16
113
    /**
114
     * Attach the permission to a group.
115
     *
116
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
117
     * @param  bool                                          $reload
118
     */
119
    public function attachPermission(&$permission, $reload = true)
120 48
    {
121
        if ($this->hasPermission($permission))
122 48
            return;
123 8
124
        $permission = $this->permissions()->save($permission);
125
126 48
        $this->loadPermissions($reload);
127
    }
128 48
129 48
    /**
130
     * Attach the permission by id to a group.
131
     *
132
     * @param  int   $id
133
     * @param  bool  $reload
134
     *
135
     * @return \Arcanesoft\Contracts\Auth\Models\Permission
136
     */
137
    public function attachPermissionById($id, $reload = true)
138
    {
139 8
        $permission = $this->getPermissionById($id);
140
141 8
        if ( ! is_null($permission))
142
            $this->attachPermission($permission, $reload);
143 8
144 8
        return $permission;
145 6
    }
146
147 8
    /**
148
     * Attach a collection of permissions to the group.
149
     *
150
     * @param  \Illuminate\Database\Eloquent\Collection|array  $permissions
151
     * @param  bool                                            $reload
152
     *
153
     * @return \Illuminate\Database\Eloquent\Collection|array
154
     */
155
    public function attachPermissions($permissions, $reload = true)
156
    {
157
        $permissions = $this->permissions()->saveMany($permissions);
158 24
159
        $this->loadPermissions($reload);
160 24
161
        return $permissions;
162 24
    }
163
164 24
    /**
165
     * Attach the permission from a group.
166
     *
167
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
168
     * @param  bool                                          $reload
169
     */
170
    public function detachPermission(&$permission, $reload = true)
171
    {
172
        if ( ! $this->hasPermission($permission))
173 24
            return;
174
175 24
        $permission = $this->getPermissionFromGroup($permission);
176 16
        $permission->update(['group_id' => 0]);
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...
177
178
        $this->loadPermissions($reload);
179 24
    }
180 24
181 24
    /**
182 18
     * Attach the permission by id to a group.
183
     *
184 24
     * @param  int   $id
185 24
     * @param  bool  $reload
186
     *
187
     * @return \Arcanesoft\Contracts\Auth\Models\Permission
188
     */
189
    public function detachPermissionById($id, $reload = true)
190
    {
191
        $permission = $this->getPermissionById($id);
192
193
        if ( ! is_null($permission))
194
            $this->detachPermission($permission, $reload);
195 8
196
        return $permission;
197 8
    }
198
199 8
    /**
200 8
     * Detach multiple permissions by ids.
201 6
     *
202
     * @param  array  $ids
203 8
     * @param  bool   $reload
204
     *
205
     * @return int
206
     */
207
    public function detachPermissions(array $ids, $reload = true)
208
    {
209
        $detached = $this->permissions()->whereIn('id', $ids)->update(['group_id' => 0]);
210
211
        $this->loadPermissions($reload);
212
213
        return $detached;
214 8
    }
215
216 8
    /**
217
     * Detach all permissions from the group.
218 8
     *
219
     * @param  bool  $reload
220 8
     *
221
     * @return int
222 8
     */
223
    public function detachAllPermissions($reload = true)
224
    {
225
        $detached = $this->permissions()->update(['group_id' => 0]);
226
227
        $this->loadPermissions($reload);
228
229
        return $detached;
230
    }
231
232 16
    /* ------------------------------------------------------------------------------------------------
233
     |  Check Functions
234 16
     | ------------------------------------------------------------------------------------------------
235
     */
236 16
    /**
237
     * Check if role has the given permission (Permission Model or Id).
238 16
     *
239
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $id
240 16
     *
241
     * @return bool
242
     */
243
    public function hasPermission($id)
244
    {
245
        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...
246
            $id = $id->getKey();
247
248
        return ! is_null($this->getPermissionFromGroup($id));
249
    }
250
251
    /* ------------------------------------------------------------------------------------------------
252
     |  Other Functions
253
     | ------------------------------------------------------------------------------------------------
254 64
     */
255
    /**
256 64
     * Get a permission from the group.
257 64
     *
258 48
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $id
259
     *
260 64
     * @return \Arcanesoft\Contracts\Auth\Models\Permission|null
261
     */
262
    private function getPermissionFromGroup($id)
263
    {
264
        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...
265
            $id = $id->getKey();
266
267
        $this->loadPermissions();
268
269
        return $this->permissions->filter(function (PermissionContract $permission) use ($id) {
270
            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...
271
        })->first();
272
    }
273
274 64
    /**
275
     * Get a permission by id.
276 64
     *
277 24
     * @param  int  $id
278 18
     *
279
     * @return \Arcanesoft\Contracts\Auth\Models\Permission|null
280 64
     */
281
    private function getPermissionById($id)
282 64
    {
283 64
        return $this->permissions()
284 64
            ->getRelated()
285
            ->where('id', $id)
286
            ->first();
287
    }
288
289
    /**
290
     * Load the permissions.
291
     *
292
     * @param  bool  $load
293
     *
294 16
     * @return self
295
     */
296 16
    protected function loadPermissions($load = true)
297 16
    {
298 16
        return $load ? $this->load('permissions') : $this;
299 16
    }
300
301
    /**
302
     * Slugify the value.
303
     *
304
     * @param  string  $value
305
     *
306
     * @return string
307
     */
308
    protected function slugify($value)
309 88
    {
310
        return Str::slug($value, config('laravel-auth.permissions-groups.slug-separator', '-'));
311 88
    }
312
}
313