Completed
Push — dev ( ade326...e1b211 )
by
unknown
03:23
created

AdminGroupConfiguration   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 115
Duplicated Lines 20.87 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 24
loc 115
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A getGroups() 0 4 1
A getGroupLogins() 8 8 2
A getGroupPermissions() 8 8 2
A getGroupLabel() 8 8 2
A getLoginGroupName() 0 4 2
A hasPermission() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace eXpansion\Framework\AdminGroups\Services;
4
5
/**
6
 * Class AdminGroupConfiguration
7
 *
8
 * @package eXpansion\Bundle\AdminGroupConfiguration\Services;
9
 * @author oliver de Cramer <[email protected]>
10
 */
11
class AdminGroupConfiguration
12
{
13
    protected $config;
14
15
    protected $loginGroups = [];
16
17
    /**
18
     * AdminGroupConfiguration constructor.
19
     * @param $config
20
     */
21 13
    public function __construct($config)
22
    {
23 13
        $this->config = $config;
24
25 13
        foreach ($this->config as $groupName => $groupData) {
26 13
            if (!empty($groupData['logins'])) {
27 13
                foreach ($groupData['logins'] as $login) {
28 13
                    $this->loginGroups[$login] = $groupName;
29
                }
30
            }
31
        }
32 13
    }
33
34
    /**
35
     * Get the list of all admin groups.
36
     *
37
     * @return string[]
38
     */
39 4
    public function getGroups()
40
    {
41 4
        return array_keys($this->config);
42
    }
43
44
    /**
45
     * Get list of all users in a group (not just connected).
46
     *
47
     * @param $groupName
48
     *
49
     * @return string[]|null
50
     */
51 3 View Code Duplication
    public function getGroupLogins($groupName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53 3
        if (!isset($this->config[$groupName])) {
54 3
            return null;
55
        }
56
57 2
        return $this->config[$groupName]['logins'];
58
    }
59
60
    /**
61
     * Get list of all permissions given to a group.
62
     *
63
     * @param string $groupName
64
     *
65
     * @return string[]
66
     */
67 5 View Code Duplication
    public function getGroupPermissions($groupName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69 5
        if (!isset($this->config[$groupName])) {
70 1
            return [];
71
        }
72
73 5
        return $this->config[$groupName]['permissions'];
74
    }
75
76
    /**
77
     * Get admin group label
78
     *
79
     * @param string $groupName
80
     * @return string
81
     */
82 View Code Duplication
    public function getGroupLabel($groupName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (!isset($this->config[$groupName])) {
85
            return "";
86
        }
87
88
        return $this->config[$groupName]['label'];
89
    }
90
91
92
    /**
93
     * @param string $login
94
     *
95
     * @return string|null
96
     */
97 7
    public function getLoginGroupName($login)
98
    {
99 7
        return isset($this->loginGroups[$login]) ? $this->loginGroups[$login] : null;
100
    }
101
102
    /**
103
     * @param string $login
104
     * @param string $permission
105
     *
106
     * @return bool
107
     */
108 4
    public function hasPermission($login, $permission)
109
    {
110 4
        $groupName = $this->getLoginGroupName($login);
111
112
        // if login has no groups, no permission
113 4
        if ($groupName === null) {
114 3
            return false;
115
        }
116
        // master admin has all permissions
117 4
        if ($groupName == 'master_admin') {
118 4
            return true;
119
        }
120
121 4
        $permissions = $this->getGroupPermissions($groupName);
122
123 4
        return in_array($permission, $permissions);
124
    }
125
}
126