Completed
Push — master ( 879f46...16ff0f )
by Andrii
02:19
created

AbstractIniter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 39
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A reinit() 0 4 1
A init() 0 14 5
1
<?php
2
/**
3
 * RBAC implementation for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-rbac
6
 * @package   hipanel-rbac
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac;
12
13
/**
14
 * Initer for AuthManager.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
abstract class AbstractIniter implements RbacIniterInterface
19
{
20
    /**
21
     * Provides a tree of permissions to be set in AuthManager.
22
     *
23
     * @return array where:
24
     * - key: role name
25
     * - value: role items
26
     */
27
    abstract public function getTree();
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function init(AuthManager $auth)
33
    {
34
        foreach (array_keys($this->getTree()) as $role) {
35
            $auth->setRole($role);
36
        }
37
38
        foreach ($this->getTree() as $role => $items) {
39
            foreach ($items as $name) {
40
                $item = $auth->getItem($name);
41
                if ($item === null) {
42
                    $item = $auth->setPermission($name);
43
                    $auth->setPermission("deny:$name");
44
                }
45
                $auth->setChild($role, $item);
46
            }
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function reinit(AuthManager $auth)
54
    {
55
        $auth->removeAll();
56
        $this->init($auth);
57
    }
58
}
59