PermissionManager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 86
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 12 3
A clean() 0 12 3
A getState() 0 12 3
A permissionsAreAllFalse() 0 14 2
1
<?php namespace Modules\User\Permissions;
2
3
use Pingpong\Modules\Module;
4
5
class PermissionManager
6
{
7
    /**
8
     * @var Module
9
     */
10
    private $module;
11
12
    /**
13
     */
14
    public function __construct()
15
    {
16
        $this->module = app('modules');
17
    }
18
19
    /**
20
     * Get the permissions from all the enabled modules
21
     * @return array
22
     */
23
    public function all()
24
    {
25
        $permissions = [];
26
        foreach ($this->module->enabled() as $enabledModule) {
0 ignored issues
show
Bug introduced by
The expression $this->module->enabled() of type boolean is not traversable.
Loading history...
27
            $configuration = config(strtolower('asgard.' . $enabledModule->getName()) . '.permissions');
28
            if ($configuration) {
29
                $permissions[$enabledModule->getName()] = $configuration;
30
            }
31
        }
32
33
        return $permissions;
34
    }
35
36
    /**
37
     * Return a correctly type casted permissions array
38
     * @param $permissions
39
     * @return array
40
     */
41
    public function clean($permissions)
42
    {
43
        if (!$permissions) {
44
            return [];
45
        }
46
        $cleanedPermissions = [];
47
        foreach ($permissions as $permissionName => $checkedPermission) {
48
            $cleanedPermissions[$permissionName] = $this->getState($checkedPermission);
49
        }
50
51
        return $cleanedPermissions;
52
    }
53
54
    /**
55
     * @param $checkedPermission
56
     * @return bool
57
     */
58
    protected function getState($checkedPermission)
59
    {
60
        if ($checkedPermission == 'true') {
61
            return true;
62
        }
63
64
        if ($checkedPermission == 'false') {
65
            return false;
66
        }
67
68
        return (bool)$checkedPermission;
69
    }
70
71
    /**
72
     * Are all of the permissions passed of false value?
73
     * @param array $permissions    Permissions array
74
     * @return bool
75
     */
76
    public function permissionsAreAllFalse(array $permissions)
77
    {
78
        $uniquePermissions = array_unique($permissions);
79
80
        if (count($uniquePermissions) > 1) {
81
            return false;
82
        }
83
84
        $uniquePermission = reset($uniquePermissions);
85
86
        $cleanedPermission = $this->getState($uniquePermission);
87
88
        return $cleanedPermission === false;
89
    }
90
}
91