Completed
Pull Request — master (#151)
by De Cramer
03:02
created

AdminGroups::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 5
nc 1
nop 2
crap 1
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 (or create a new) admin user group
76
     *
77
     * @param string $groupName
78
     *
79
     * @return Group
80
     */
81 2
    protected function getUserGroup($groupName)
82
    {
83 2
        $groupName = "admin:$groupName";
84
85 2
        $group = $this->userGroupFactory->getGroup($groupName);
86 2
        if (!$group) {
87 2
            $this->userGroupFactory->create($groupName);
88 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...
89
        }
90
91 2
        return $group;
92
    }
93
94
    /**
95
     * Checks if group, a login or a player has a certain permission or not.
96
     *
97
     * @param string|Group|Player $recipient
98
     * @param string $permission The permission to check for.
99
     *
100
     * @return bool
101
     */
102 3
    public function hasPermission($recipient, $permission)
103
    {
104
105 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...
106
            $check = true;
107
            foreach ($recipient->getLogins() as $login) {
108
                if ($this->hasLoginPermission($login, $permission) === false) {
109
                    $check = false;
110
                }
111
            }
112
113
            return $check;
114
        } else {
115 3
            return $this->hasLoginPermission($recipient, $permission);
116
        }
117
    }
118
119 3
    protected function hasLoginPermission($login, $permission)
120
    {
121 3
        if ($login instanceof Player) {
122
            $login = $login->getLogin();
123
        }
124
125 3
        return $this->adminGroupConfiguration->hasPermission($login, $permission);
126
    }
127
128
129
    /**
130
     * Check if a group has a certain permission or not.
131
     *
132
     * @param string $groupName The name of the group to check permissions for.
133
     * @param string $permission The permission to check for.
134
     *
135
     * @return bool
136
     * @throws UnknownGroupException Thrown when group isn't an admin group.
137
     */
138 2
    public function hasGroupPermission($groupName, $permission)
139
    {
140
141 2
        if (strpos($groupName, 'admin:') === 0) {
142 1
            $groupName = str_replace("admin:", '', $groupName);
143
        }
144
145 2
        $logins = $this->adminGroupConfiguration->getGroupLogins($groupName);
146
147 2
        if (!empty($logins)) {
148 1
            return $this->hasPermission($logins[0], $permission);
149
        }
150
151 2
        if (is_null($logins)) {
152 2
            throw new UnknownGroupException("'$groupName' admin group does not exist.");
153
        }
154
155 1
        return false;
156
    }
157
158
    /**
159
     * @param string $groupName
160
     * @return string
161
     */
162
    public function getGroupLabel($groupName)
163
    {
164
        if (strpos($groupName, 'admin:') === 0) {
165
            $groupName = str_replace("admin:", '', $groupName);
166
        }
167
168
        return $this->adminGroupConfiguration->getGroupLabel($groupName);
169
170
    }
171
172
    /**
173
     * gets if the player is admin
174
     *
175
     * @param string $login
176
     * @return bool
177
     */
178
    public function isAdmin($login)
179
    {
180
        return ($this->getLoginUserGroups($login)->getName() !== "admin:guest") ? true : false;
181
    }
182
183
}
184