Completed
Push — master ( a9d673...d0cde4 )
by Arjay
06:58
created

HasPermission::canAtLeast()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Yajra\Acl\Traits;
4
5
use Yajra\Acl\Models\Permission;
6
7
trait HasPermission
8
{
9
    /**
10
     * Assigns the given permission to the role.
11
     *
12
     * @param  int $permissionId
13
     * @return bool
14
     */
15
    public function assignPermission($permissionId = null)
16
    {
17
        $permissions = $this->permissions;
0 ignored issues
show
Bug introduced by
The property permissions 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...
18
19
        if (! $permissions->contains($permissionId)) {
20
            $this->permissions()->attach($permissionId);
21
22
            return true;
23
        }
24
25
        return false;
26
    }
27
28
    /**
29
     * Get related permissions.
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
32
     */
33
    public function permissions()
34
    {
35
        return $this->belongsToMany(config('acl.permission', Permission::class))->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...
36
    }
37
38
    /**
39
     * Revokes the given permission from the role.
40
     *
41
     * @param  int|null $permissionId
42
     * @return bool
43
     */
44
    public function revokePermission($permissionId = null)
45
    {
46
        return $this->permissions()->detach($permissionId);
47
    }
48
49
    /**
50
     * Syncs the given permission(s) with the role.
51
     *
52
     * @param  array $permissionIds
53
     * @return array|bool
54
     */
55
    public function syncPermissions(array $permissionIds = [])
56
    {
57
        return $this->permissions()->sync($permissionIds);
58
    }
59
60
    /**
61
     * Revokes all permissions from the role.
62
     *
63
     * @return bool
64
     */
65
    public function revokeAllPermissions()
66
    {
67
        return $this->permissions()->detach();
68
    }
69
70
    /**
71
     * Checks if the role has the given permission.
72
     *
73
     * @param  string $permission
74
     * @return bool
75
     */
76
    public function can($permission)
77
    {
78
        $permissions = $this->getPermissions();
79
80
        if (is_array($permission)) {
81
            $permissionCount   = count($permission);
82
            $intersection      = array_intersect($permissions, $permission);
83
            $intersectionCount = count($intersection);
84
85
            return ($permissionCount == $intersectionCount) ? true : false;
86
        } else {
87
            return in_array($permission, $permissions);
88
        }
89
    }
90
91
    /**
92
     * Get list of permissions slug.
93
     *
94
     * @return array
95
     */
96
    public function getPermissions()
97
    {
98
        return $this->permissions->pluck('slug')->toArray();
99
    }
100
101
    /**
102
     * Check if the role has at least one of the given permissions.
103
     *
104
     * @param  array $permission
105
     * @return bool
106
     */
107
    public function canAtLeast(array $permission = [])
108
    {
109
        $permissions = $this->getPermissions();
110
111
        $intersection      = array_intersect($permissions, $permission);
112
        $intersectionCount = count($intersection);
113
114
        return ($intersectionCount > 0) ? true : false;
115
    }
116
}
117