Passed
Push — main ( 45e883...c5012c )
by Teodoro
06:10 queued 03:48
created

ResolverInterface   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllowed() 0 20 2
A check() 0 24 6
1
<?php
2
3
namespace Tleckie\Acl\Permissions;
4
5
use Tleckie\Acl\Acl\PermissionTypeEnum;
6
7
/**
8
 * Class ResolverInterface
9
 *
10
 * @package Tleckie\Acl\Permissions
11
 * @author  Teodoro Leckie Westberg <[email protected]>
12
 */
13
class ResolverInterface implements PermissionResolverInterface
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function isAllowed(
19
        array &$rules,
20
        string $role,
21
        string $resource,
22
        string $privilege = null
23
    ): bool {
24
        return !$this->check(
25
            PermissionTypeEnum::DENY(),
1 ignored issue
show
Bug introduced by
The method DENY() does not exist on Tleckie\Acl\Acl\PermissionTypeEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            PermissionTypeEnum::/** @scrutinizer ignore-call */ 
26
                                DENY(),
Loading history...
26
            $rules,
27
            $role,
28
            $resource,
29
            $privilege
30
        )
31
            &&
32
            $this->check(
33
                PermissionTypeEnum::ALLOW(),
1 ignored issue
show
Bug introduced by
The method ALLOW() does not exist on Tleckie\Acl\Acl\PermissionTypeEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                PermissionTypeEnum::/** @scrutinizer ignore-call */ 
34
                                    ALLOW(),
Loading history...
34
                $rules,
35
                $role,
36
                $resource,
37
                $privilege
38
            );
39
    }
40
41
    /**
42
     * @param string      $type
43
     * @param array       $rules
44
     * @param string      $role
45
     * @param string      $resource
46
     * @param string|null $privilege
47
     * @return bool
48
     */
49
    private function check(
50
        string $type,
51
        array &$rules,
52
        string $role,
53
        string $resource,
54
        string $privilege = null
55
    ): bool {
56
        $item = &$rules['roles'];
57
        if (isset($item[$role][$type][$resource][$privilege])) {
58
            return true;
59
        }
60
61
        foreach ($item[$role]['parents'] ?? [] as $parent => $value) {
62
            if ($this->check($type, $rules, $parent, $resource, $privilege)) {
63
                return true;
64
            }
65
        }
66
67
        if (isset($item[$role][$type][$resource]) &&
68
            !count($item[$role][$type][$resource])) {
69
            return true;
70
        }
71
72
        return false;
73
    }
74
}
75