HasPermission::cannot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Yajra\Acl\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Yajra\Acl\Models\Permission;
7
8
trait HasPermission
9
{
10
    use InteractsWithPermission;
11
12
    /**
13
     * Checks if the role has the given permission.
14
     *
15
     * @param  array|string  $permission
16
     * @return bool
17
     */
18
    public function can($permission): bool
19
    {
20
        $permissions = $this->getPermissions();
21
22
        if (is_array($permission)) {
23
            $permissionCount = count($permission);
24
            $intersection = array_intersect($permissions, $permission);
25
            $intersectionCount = count($intersection);
26
27
            return $permissionCount == $intersectionCount;
28
        }
29
30
        return in_array($permission, $permissions);
31
    }
32
33
    /**
34
     * Checks if the role does not have the given permission.
35
     *
36
     * @param  array|string  $permission
37
     * @return bool
38
     */
39
    public function cannot($permission): bool
40
    {
41
        return ! $this->can($permission);
42
    }
43
44
    /**
45
     * Check if the role has at least one of the given permissions.
46
     *
47
     * @param  string|array  $permission
48
     * @return bool
49
     */
50
    public function canAtLeast($permission): bool
51
    {
52
        $permissions = $this->getPermissions();
53
54
        $intersection = array_intersect($permissions, (array) $permission);
55
        $intersectionCount = count($intersection);
56
57
        return $intersectionCount > 0;
58
    }
59
}
60