AclService::acl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace Staticus\Acl;
3
4
use Staticus\Config\ConfigInterface;
5
use Zend\Permissions\Acl\AclInterface;
6
use Zend\Permissions\Acl\Acl;
7
use Zend\Permissions\Acl\Resource\ResourceInterface;
8
use Zend\Permissions\Acl\Role\RoleInterface;
9
10
class AclService implements AclServiceInterface
11
{
12
    const ROLES = 'roles';
13
    const RESOURCES = 'resources';
14
    const PRIVILEGES = 'privileges';
15
    const INHERIT = 'inherit';
16
17
    protected $config;
18
19
    /**
20
     * @var AclInterface|Acl
21
     */
22
    protected $acl;
23
24
    public function __construct(ConfigInterface $config, AclInterface $acl)
25
    {
26
        $this->config = $config->get('acl');
27
        $this->acl = $acl;
28
        $this->fillRoles($this->config[self::ROLES]);
29
        $this->fillResources($this->config[self::RESOURCES]);
30
    }
31
32
    public function fillRoles(array $rolesConfig)
33
    {
34
        foreach ($rolesConfig as $role => $options) {
35
            $inherit = $this->getOption($options, self::INHERIT);
36
            if (null !== $inherit
37
                && !is_string($inherit)
38
                && !is_array($inherit)
39
                && !$inherit instanceof RoleInterface
40
            ) {
41
                throw new Exceptions\RuntimeException(
42
                    'Inherit option must be a string, an array or implement RoleInterface for roles');
43
            }
44
            $this->acl->addRole($role, $inherit);
45
        }
46
    }
47
48
    public function fillResources(array $resourcesConfig)
49
    {
50
        foreach ($resourcesConfig as $resource => $options) {
51
            $inherit = $this->getOption($options, self::INHERIT);
52
            if (null !== $inherit
53
                && !is_string($inherit)
54
                && !$inherit instanceof ResourceInterface
55
            ) {
56
                throw new Exceptions\RuntimeException(
57
                    'Inherit option must be a string or implement ResourceInterface for resources');
58
            }
59
            $this->acl->addResource($resource, $inherit);
60
            $privileges = $this->getOption($options, self::PRIVILEGES, []);
61
            foreach ($privileges as $role => $actions) {
62
                $this->acl->allow([$role], [$resource], $actions);
63
            }
64
        }
65
    }
66
67
    /**
68
     * @param array $options
69
     * @param string $option
70
     * @param mixed $default
71
     * @return array
72
     */
73
    protected function getOption(array $options, $option, $default = null)
74
    {
75
        $inherit = array_key_exists($option, $options)
76
            ? $options[$option]
77
            : $default;
78
79
        return $inherit;
80
    }
81
82
    public function acl()
83
    {
84
        return $this->acl;
85
    }
86
}
87