Completed
Push — master ( 5acf75...dcb410 )
by Stephen
01:54
created

UserOnly::cacheGetNegatedPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace z1haze\Acl\Traits;
4
5
use z1haze\Acl\Models\Permission;
6
7
trait UserOnly
8
{
9
    /** USER
10
     * A User can have permissions explicitly assigned to it
11
     *
12
     * @return mixed
13
     */
14 13
    public function permissions()
15
    {
16 13
        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...
17
    }
18
    
19
    /**
20
     * Negate a permission from a User
21
     *
22
     * @param $permission
23
     */
24 1
    public function negatePermission($permission)
25
    {
26 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...
27 1
    }
28
29
    /**
30
     * Negate an array of permissions from a User
31
     *
32
     * @param $permissions
33
     */
34 1
    public function negatePermissions(array $permissions)
35
    {
36 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...
37 1
    }
38
39
    /**
40
     * First try the cache to return the collection,
41
     * then fetch it from the database.
42
     *
43
     * See @cacheGetNegatedPermissions()
44
     *
45
     * @return mixed
46
     */
47 3
    public function getNegatedPermissions()
48
    {
49 3
        return \Cache::remember(
50 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...
51 3
            config('laravel-acl.cacheMinutes'),
52 3
            function () {
53 3
                return $this->cacheGetNegatedPermissions();
54 3
            }
55
        );
56
    }
57
58
    /**
59
     * Return a collection of permissions that
60
     * are negated for a User
61
     *
62
     * @return mixed
63
     */
64 3
    protected function cacheGetNegatedPermissions()
65
    {
66 3
        return $this->permissions()->wherePivot('negated', true)->get();
67
    }
68
}