UserOnly   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 64
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
ccs 17
cts 17
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A permissions() 0 4 1
A negatePermission() 0 4 1
A negatePermissions() 0 4 1
A getNegatedPermissions() 0 10 1
A cacheGetNegatedPermissions() 0 4 1
1
<?php
2
3
namespace z1haze\Acl\Traits;
4
5
use z1haze\Acl\Models\Permission;
6
7
trait UserOnly
8
{
9
    /**
10
     * USER
11
     * A User can have permissions explicitly assigned to it
12
     *
13
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
14
     */
15 15
    public function permissions()
16
    {
17 15
        return $this->belongsToMany(config('laravel-acl.permission', Permission::class))->withPivot('negated')->withTimestamps();
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
18
    }
19
20
    /**
21
     * USER
22
     * Negate a permission from a User
23
     *
24
     * @param $permission
25
     */
26 1
    public function negatePermission($permission)
27
    {
28 1
        $this->modifyPermissions([$permission], 'negate');
0 ignored issues
show
Bug introduced by
The method modifyPermissions() does not exist on z1haze\Acl\Traits\UserOnly. Did you maybe mean permissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
29 1
    }
30
31
    /**
32
     * Negate an array of permissions from a User
33
     *
34
     * @param $permissions
35
     */
36 1
    public function negatePermissions(array $permissions)
37
    {
38 1
        $this->modifyPermissions($permissions, 'negate');
0 ignored issues
show
Bug introduced by
The method modifyPermissions() does not exist on z1haze\Acl\Traits\UserOnly. Did you maybe mean permissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
39 1
    }
40
41
    /**
42
     * First try the cache to return the collection,
43
     * then fetch it from the database.
44
     *
45
     * See @cacheGetNegatedPermissions()
46
     *
47
     * @return \Illuminate\Support\Collection
48
     */
49 3
    public function getNegatedPermissions()
50
    {
51 3
        return \Cache::remember(
52 3
            'laravel-acl.getNegatedPermissionsForUser_' . $this->id,
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53 3
            config('laravel-acl.cacheMinutes'),
54 3
            function() {
55 3
                return $this->cacheGetNegatedPermissions();
56 3
            }
57
        );
58
    }
59
60
    /**
61
     * Return a collection of permissions that
62
     * are negated for a User
63
     *
64
     * @return \Illuminate\Support\Collection
65
     */
66 3
    protected function cacheGetNegatedPermissions()
67
    {
68 3
        return $this->permissions()->wherePivot('negated', true)->get();
69
    }
70
}