Completed
Push — master ( d69705...774ae2 )
by Dmitry
03:43
created

AbstractIniter::reinit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 1
crap 1
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-2017, 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 13
    public function init(AuthManager $auth)
33
    {
34 13
        foreach (array_keys($this->getTree()) as $role) {
35 13
            $auth->setRole($role);
36
        }
37
38 13
        foreach ($this->getTree() as $role => $items) {
39 13
            foreach ($items as $name) {
40 13
                $item = $auth->getItem($name);
41 13
                if ($item === null) {
42 13
                    $item = $auth->setPermission($name);
43
                }
44 13
                $auth->setChild($role, $item);
45
            }
46
        }
47 13
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 13
    public function reinit(AuthManager $auth)
53
    {
54 13
        $auth->removeAll();
55 13
        $this->init($auth);
56 13
    }
57
}
58