ResolverInterface   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllowed() 0 20 2
A check() 0 25 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(),
26
            $rules,
27
            $role,
28
            $resource,
29
            $privilege
30
        )
31
            &&
32
            $this->check(
33
                PermissionTypeEnum::ALLOW(),
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
58
        if (isset($item[$role][$type][$resource][$privilege])) {
59
            return true;
60
        }
61
62
        foreach ($item[$role]['parents'] ?? [] as $parent => $value) {
63
            if ($this->check($type, $rules, $parent, $resource, $privilege)) {
64
                return true;
65
            }
66
        }
67
68
        if (isset($item[$role][$type][$resource]) &&
69
            !count($item[$role][$type][$resource])) {
70
            return true;
71
        }
72
73
        return false;
74
    }
75
}
76