PermissionRegistrar   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 90
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 8 2
A register() 0 6 1
A getPermissions() 0 4 1
A format() 0 11 3
A formatPermissions() 0 10 2
1
<?php
2
3
namespace Napp\Core\Acl;
4
5
class PermissionRegistrar
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    /** @var array */
8
    protected static $permissions = [];
9
10
    /** @var PermissionRegistrar */
11
    private static $instance;
12
13
    /**
14
     * @codeCoverageIgnore
15
     * PermissionRegistrar constructor.
16
     */
17
    private function __construct()
18
    {
19
    }
20
21
    /**
22
     * @codeCoverageIgnore
23
     * Singleton class instance.
24
     *
25
     * @return PermissionRegistrar
26
     */
27
    public static function getInstance(): self
28
    {
29
        if (! null === self::$instance) {
30
            self::$instance = new self();
31
        }
32
33
        return self::$instance;
34
    }
35
36
    /**
37
     * Registers permissions.
38
     *
39
     * @param array $permissions
40
     *
41
     * @return void
42
     */
43 52
    public static function register(array $permissions): void
44
    {
45 52
        $permissions = self::format($permissions);
46
47 52
        self::$permissions = array_merge(self::$permissions, $permissions);
48 52
    }
49
50
    /**
51
     * Fetch the collection of site permissions.
52
     *
53
     * @return array
54
     */
55 6
    public static function getPermissions(): array
56
    {
57 6
        return self::$permissions;
58
    }
59
60
    /**
61
     * @param array $permissions
62
     *
63
     * @return array
64
     */
65 60
    protected static function format(array $permissions): array
66
    {
67 60
        foreach ($permissions as $key => $permission) {
68 60
            if (\is_numeric($key)) {
69 60
                $permissions[$permission] = $permission;
70 60
                unset($permissions[$key]);
71
            }
72
        }
73
74 60
        return $permissions;
75
    }
76
77
    /**
78
     * Replace user permission with PermissionRegistrar.
79
     *
80
     * @param array $userPermissions
81
     *
82
     * @return array
83
     */
84 38
    public static function formatPermissions(array $userPermissions): array
85
    {
86 38
        $userPermissions = self::format($userPermissions);
87
88 38
        foreach ($userPermissions as $key => $permission) {
89 34
            $userPermissions[$key] = self::$permissions[$key];
90
        }
91
92 38
        return $userPermissions;
93
    }
94
}
95