Completed
Push — develop ( 92b215...469605 )
by Nicolas
03:17
created

PermissionManager::permissionsAreAllFalse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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