Completed
Pull Request — master (#175)
by
unknown
05:30
created

AdminGroups   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 188
Duplicated Lines 6.91 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 66.06%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 13
loc 188
ccs 37
cts 56
cp 0.6606
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getUserGroups() 0 10 2
A getLoginUserGroups() 0 9 2
A getLoginGroupLabel() 13 13 3
A getUserGroup() 0 12 2
A hasPermission() 0 16 4
A hasLoginPermission() 0 8 2
A hasGroupPermission() 0 19 4
A getGroupLabel() 0 9 2
A isAdmin() 0 4 2

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\Helpers;
4
5
use eXpansion\Framework\AdminGroups\Exceptions\UnknownGroupException;
6
use eXpansion\Framework\AdminGroups\Services\AdminGroupConfiguration;
7
use eXpansion\Framework\Core\Model\UserGroups\Group;
8
use eXpansion\Framework\Core\Plugins\UserGroups\Factory;
9
use eXpansion\Framework\Core\Storage\Data\Player;
10
11
/**
12
 * Class AdminGroupConfiguration
13
 *
14
 * @package eXpansion\Bundle\AdminGroupConfiguration\Helpers;
15
 * @author  oliver de Cramer <[email protected]>
16
 */
17
class AdminGroups
18
{
19
    /** @var  AdminGroupConfiguration */
20
    protected $adminGroupConfiguration;
21
22
    /** @var  Factory */
23
    protected $userGroupFactory;
24
25
    /**
26
     * GroupsPlugin constructor.
27
     *
28
     * @param AdminGroupConfiguration $adminGroupConfiguration
29
     * @param Factory                 $userGroupFactory
30
     */
31 6
    public function __construct(
32
        AdminGroupConfiguration $adminGroupConfiguration,
33
        Factory $userGroupFactory
34
    ) {
35 6
        $this->adminGroupConfiguration = $adminGroupConfiguration;
36 6
        $this->userGroupFactory = $userGroupFactory;
37 6
    }
38
39
    /**
40
     * Get list of all user groups.
41
     * Can be useful for creating group based GUI widgets.
42
     *
43
     * @return Group[]
44
     */
45 1
    public function getUserGroups()
46
    {
47 1
        $groups = [];
48 1
        foreach ($this->adminGroupConfiguration->getGroups() as $groupName) {
49 1
            $groups[] = $this->getUserGroup("$groupName");
50
        }
51 1
        $groups[] = $this->getUserGroup('guest');
52
53 1
        return $groups;
54
    }
55
56
    /**
57
     * Get the group in which a user is.
58
     * This is useful for gui actions.
59
     *
60
     * @param string $login
61
     *
62
     * @return Group
63
     */
64 1
    public function getLoginUserGroups($login)
65
    {
66 1
        $groupName = $this->adminGroupConfiguration->getLoginGroupName($login);
67 1
        if (empty($groupName)) {
68 1
            $groupName = 'guest';
69
        }
70
71 1
        return $this->getUserGroup("$groupName");
72
    }
73
74
    /**
75
     * Get admin group Label
76
     *
77
     * @param string $login
78
     * @return string
79
     */
80 View Code Duplication
    public function getLoginGroupLabel($login)
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...
81
    {
82
        $group = $this->getLoginUserGroups($login);
83
84
        $groupName = "Admin";
85
        if ($group) {
86
            if ($groupName) {
87
                $groupName = $this->getGroupLabel($group->getName());
88
            }
89
        }
90
91
        return $groupName;
92
    }
93
94
95
    /**
96
     * Get (or create a new) admin user group
97
     *
98
     * @param string $groupName
99
     *
100
     * @return Group
101
     */
102 2
    protected function getUserGroup($groupName)
103
    {
104 2
        $groupName = "admin:$groupName";
105
106 2
        $group = $this->userGroupFactory->getGroup($groupName);
107 2
        if (!$group) {
108 2
            $this->userGroupFactory->create($groupName);
109 2
            $group = $this->userGroupFactory->getGroup($groupName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $group is correct as $this->userGroupFactory->getGroup($groupName) (which targets eXpansion\Framework\Core...ups\Factory::getGroup()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
        }
111
112 2
        return $group;
113
    }
114
115
    /**
116
     * Checks if group, a login or a player has a certain permission or not.
117
     *
118
     * @param string|Group|Player $recipient
119
     * @param string              $permission The permission to check for.
120
     *
121
     * @return bool
122
     */
123 3
    public function hasPermission($recipient, $permission)
124
    {
125
126 3
        if ($recipient instanceof Group) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Core\Model\UserGroups\Group does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
127
            $check = true;
128
            foreach ($recipient->getLogins() as $login) {
129
                if ($this->hasLoginPermission($login, $permission) === false) {
130
                    $check = false;
131
                }
132
            }
133
134
            return $check;
135
        } else {
136 3
            return $this->hasLoginPermission($recipient, $permission);
137
        }
138
    }
139
140 3
    protected function hasLoginPermission($login, $permission)
141
    {
142 3
        if ($login instanceof Player) {
143
            $login = $login->getLogin();
144
        }
145
146 3
        return $this->adminGroupConfiguration->hasPermission($login, $permission);
147
    }
148
149
150
    /**
151
     * Check if a group has a certain permission or not.
152
     *
153
     * @param string $groupName  The name of the group to check permissions for.
154
     * @param string $permission The permission to check for.
155
     *
156
     * @return bool
157
     * @throws UnknownGroupException Thrown when group isn't an admin group.
158
     */
159 2
    public function hasGroupPermission($groupName, $permission)
160
    {
161
162 2
        if (strpos($groupName, 'admin:') === 0) {
163 1
            $groupName = str_replace("admin:", '', $groupName);
164
        }
165
166 2
        $logins = $this->adminGroupConfiguration->getGroupLogins($groupName);
167
168 2
        if (!empty($logins)) {
169 1
            return $this->hasPermission($logins[0], $permission);
170
        }
171
172 2
        if (is_null($logins)) {
173 2
            throw new UnknownGroupException("'$groupName' admin group does not exist.");
174
        }
175
176 1
        return false;
177
    }
178
179
    /**
180
     * @param string $groupName
181
     * @return string
182
     */
183
    public function getGroupLabel($groupName)
184
    {
185
        if (strpos($groupName, 'admin:') === 0) {
186
            $groupName = str_replace("admin:", '', $groupName);
187
        }
188
189
        return $this->adminGroupConfiguration->getGroupLabel($groupName);
190
191
    }
192
193
    /**
194
     * gets if the player is admin
195
     *
196
     * @param string $login
197
     * @return bool
198
     */
199
    public function isAdmin($login)
200
    {
201
        return ($this->getLoginUserGroups($login)->getName() !== "admin:guest") ? true : false;
202
    }
203
204
}
205