Passed
Push — master ( e5b7b4...ec2be8 )
by Julien
14:08
created

Security::getAcl()   C

Complexity

Conditions 13
Paths 225

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 13
eloc 30
c 4
b 0
f 0
nc 225
nop 3
dl 0
loc 55
ccs 0
cts 31
cp 0
crap 182
rs 5.4708

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit;
13
14
use Phalcon\Acl\Adapter\Memory;
15
use Phalcon\Acl\Component;
16
use Phalcon\Acl\Role;
17
use Zemit\Bootstrap\Config;
18
19
/**
20
 * {@inheritDoc}
21
 */
22
class Security extends \Phalcon\Security
23
{
24
    protected ?array $permissions;
25
    
26
    public function getConfig(): Config
27
    {
28
        return $this->getDI()->get('config');
29
    }
30
    
31
    /**
32
     * Return an ACL for the specified components name
33
     * @todo cache the ACL
34
     * @todo move to its own ACL class, shouldn't be in the Phalcon\Security
35
     *
36
     * @param array $componentsName
37
     * @param array|null $permissions
38
     * @param string $inherit
39
     * @return Memory
40
     */
41
    public function getAcl(array $componentsName = ['components'], ?array $permissions = null, string $inherit = 'inherit'): Memory
42
    {
43
        $acl = new Memory();
44
        $aclRoleList = [];
45
        
46
        $this->permissions ??= $this->getConfig()->permissions->toArray();
47
        
48
        $featureList = $this->permissions['features'] ?? [];
49
        $roleList = $this->permissions['roles'] ?? [];
50
        
51
        foreach ($roleList as $role => $rolePermission) {
52
            
53
            $role = $role === '*' ? 'everyone' : $role;
54
            $aclRole = new Role($role);
55
            $aclRoleList[$role] = $aclRole;
56
            $acl->addRole($aclRole);
57
            
58
            if (isset($rolePermission['features'])) {
59
                foreach ($rolePermission['features'] as $feature) {
60
                    $rolePermission = array_merge_recursive($rolePermission, $featureList[$feature] ?? []);
61
                    // @todo remove duplicates
62
                }
63
            }
64
            
65
            foreach ($componentsName as $componentName) {
66
                $components = $rolePermission[$componentName] ?? [];
67
                $components = is_array($components) ? $components : [$components];
68
                
69
                foreach ($components as $component => $accessList) {
70
                    if (empty($component)) {
71
                        $component = $accessList;
72
                        $accessList = '*';
73
                    }
74
                    
75
                    if ($component !== '*') {
76
                        $aclComponent = new Component($component);
77
                        $acl->addComponent($aclComponent, $accessList);
78
                        $acl->allow($aclRole, $aclComponent, $accessList);
79
                    }
80
                }
81
            }
82
        }
83
        
84
        /**
85
         * Add inheritance (role extends)
86
         */
87
        foreach ($aclRoleList as $role => $aclRole) {
88
            $inheritList = $permissions[$role][$inherit] ?? [];
89
            $inheritList = is_array($inheritList) ? $inheritList : [$inheritList];
90
            foreach ($inheritList as $inheritRole) {
91
                $acl->addInherit($aclRole, $aclRoleList[$inheritRole]);
92
            }
93
        }
94
        
95
        return $acl;
96
    }
97
}
98