Test Failed
Push — main ( 2ab83b...f4673a )
by Jean-Christophe
02:50
created

AclManager   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Test Coverage

Coverage 91.38%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 89
c 1
b 0
f 1
dl 0
loc 328
ccs 106
cts 116
cp 0.9138
rs 8.48
wmc 49

33 Methods

Rating   Name   Duplication   Size   Complexity  
A existPartIn() 0 2 1
A addRole() 0 2 1
A getPermissions() 0 2 1
A getProvider() 0 2 1
A initFromProviders() 0 7 2
A removeResource() 0 2 1
A removeRole() 0 2 1
A getAclList() 0 2 1
A filterProviders() 0 10 3
A setPermissionLevel() 0 2 1
A removePermission() 0 2 1
A start() 0 3 1
A allow() 0 2 1
A addAndAllow() 0 2 1
A removefilterProviders() 0 2 1
A addResources() 0 3 2
A initCache() 0 23 5
A addPermissions() 0 3 2
A getAcls() 0 2 1
A getModelClassesSwap() 0 9 3
A addRoles() 0 3 2
A addResource() 0 2 1
A removeAcl() 0 2 1
A reloadFromSelectedProviders() 0 11 4
A getRoles() 0 2 1
A existAclIn() 0 2 1
A getPermissionMap() 0 6 2
A getResources() 0 2 1
A isAllowed() 0 2 1
A registerAnnotations() 0 5 1
A addPermission() 0 2 1
A saveAll() 0 2 1
A associate() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like AclManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AclManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Ubiquity\security\acl;
3
4
use Ubiquity\security\acl\models\AclList;
5
use Ubiquity\security\acl\models\Role;
6
use Ubiquity\security\acl\models\Resource;
7
use Ubiquity\security\acl\models\Permission;
8
use Ubiquity\security\acl\persistence\AclProviderInterface;
9
use Ubiquity\cache\ClassUtils;
10
use Ubiquity\security\acl\cache\AclControllerParser;
11
use Ubiquity\exceptions\AclException;
12
use Ubiquity\cache\CacheManager;
13
use Ubiquity\annotations\acl\AllowAnnotation;
14
use Ubiquity\annotations\acl\ResourceAnnotation;
15
use Ubiquity\annotations\acl\PermissionAnnotation;
16
use Ubiquity\security\acl\cache\PermissionsMap;
17
use Ubiquity\security\acl\models\AbstractAclPart;
18
use Ubiquity\security\acl\models\AclElement;
19
use Ubiquity\security\acl\persistence\AclCacheProvider;
20
21
/**
22
 * Ubiquity\security\acl$AclManager
23
 * This class is part of Ubiquity
24
 *
25
 * @author jc
26
 * @version 1.0.0
27
 *
28
 */
29
class AclManager {
30
31
	/**
32
	 *
33
	 * @var AclList
34
	 */
35
	protected static $aclList;
36
37
	/**
38
	 *
39
	 * @var PermissionsMap
40
	 */
41
	protected static $permissionMap;
42
43
	/**
44
	 *
45 22
	 * @var AclProviderInterface[]
46 22
	 */
47 22
	protected static $providersPersistence;
48 22
49
	/**
50
	 * Create AclList with default roles and resources.
51
	 */
52
	public static function start(): void {
53
		self::$aclList = new AclList();
54
		self::$aclList->init();
55 11
	}
56 11
57 11
	/**
58 10
	 * Load acls, roles, resources and permissions from providers.
59 10
	 *
60 10
	 * @param AclProviderInterface[] $providers
61 10
	 */
62
	public static function initFromProviders(?array $providers = []): void {
63 11
		self::$aclList->setProviders($providers);
64
		if (\count($providers) > 0) {
65
			self::$aclList->loadAcls();
66
			self::$aclList->loadRoles();
67
			self::$aclList->loadResources();
68
			self::$aclList->loadPermissions();
69 1
		}
70 1
	}
71 1
72 1
	/**
73 1
	 *
74 1
	 * @param array|string $selectedProviders
75
	 */
76
	public static function reloadFromSelectedProviders($selectedProviders = '*') {
77 1
		$sProviders = self::$aclList->getProviders();
78 1
		self::$aclList->clear();
79
		$providers = [];
80 6
		foreach ($sProviders as $prov) {
81 6
			if ($selectedProviders === '*' || \array_search(\get_class($prov), $selectedProviders) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $selectedProviders can also be of type string; however, parameter $haystack of array_search() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
			if ($selectedProviders === '*' || \array_search(\get_class($prov), /** @scrutinizer ignore-type */ $selectedProviders) !== false) {
Loading history...
82 6
				$providers[] = $prov;
83
			}
84 1
		}
85 1
		self::initFromProviders($providers);
86 1
		self::$aclList->setProviders($sProviders);
87
	}
88 1
89
	public static function addRole(string $name, ?array $parents = []) {
90 8
		self::$aclList->addRole(new Role($name, $parents));
91 8
	}
92 8
93
	public static function addRoles(array $nameParents) {
94 1
		foreach ($nameParents as $name => $parents) {
95 1
			self::$aclList->addRole(new Role($name, $parents));
96 1
		}
97
	}
98 1
99
	public static function addResource(string $name, ?string $value = null) {
100 10
		self::$aclList->addResource(new Resource($name, $value));
101 10
	}
102 10
103
	public static function addResources(array $nameValue) {
104 1
		foreach ($nameValue as $name => $value) {
105 1
			self::$aclList->addResource(new Resource($name, $value));
106 1
		}
107
	}
108 1
109
	public static function addPermission(string $name, int $level = 0) {
110 3
		self::$aclList->addPermission(new Permission($name, $level));
111 3
	}
112 2
113
	public static function addPermissions(array $nameLevel) {
114 8
		foreach ($nameLevel as $name => $level) {
115 8
			self::$aclList->addPermission(new Permission($name, $level));
116
		}
117
	}
118 7
119 7
	public static function setPermissionLevel(string $name, int $level) {
120
		self::$aclList->setPermissionLevel($name, $level);
121
	}
122
123
	public static function getRoles() {
124
		return self::$aclList->getRoles();
125
	}
126 8
127 8
	public static function getResources() {
128
		return self::$aclList->getResources();
129
	}
130 11
131 11
	/**
132
	 *
133
	 * @return \Ubiquity\security\acl\models\AclList
134 6
	 */
135 6
	public static function getAclList() {
136
		return AclManager::$aclList;
137
	}
138
139
	public static function getPermissions() {
140
		return self::$aclList->getPermissions();
141
	}
142
143
	public static function getAcls() {
144
		return self::$aclList->getAcls();
145 9
	}
146 9
147 9
	/**
148
	 * Allow role to access to resource with the permission.
149
	 *
150
	 * @param string $role
151
	 * @param string $resource
152
	 * @param string $permission
153
	 */
154
	public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
155
		self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL');
156 4
	}
157 4
158 4
	/**
159
	 * Add role, resource and permission and allow this role to access to resource with the permission.
160
	 *
161
	 * @param string $role
162
	 * @param string $resource
163
	 * @param string $permission
164
	 */
165
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
166
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
167
	}
168 20
169 20
	/**
170
	 * Check if access to resource is allowed for role with the permission.
171
	 *
172
	 * @param string $role
173
	 * @param string $resource
174
	 * @param string $permission
175 5
	 * @return bool
176 5
	 */
177 5
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
178
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
179
	}
180
181
	/**
182
	 * Save all acls,roles, resources and permissions for AclProviders with no autoSave.
183 4
	 */
184 4
	public static function saveAll() {
185 4
		self::$aclList->saveAll();
186
	}
187
188
	/**
189
	 *
190
	 * @param string $role
191 4
	 */
192 4
	public static function removeRole(string $role) {
193 4
		self::$aclList->removeRole($role);
194
	}
195
196
	/**
197
	 *
198
	 * @param string $permission
199 2
	 */
200 2
	public static function removePermission(string $permission) {
201 2
		self::$aclList->removePermission($permission);
202
	}
203
204
	/**
205
	 *
206
	 * @param string $resource
207
	 */
208
	public static function removeResource(string $resource) {
209 4
		self::$aclList->removeResource($resource);
210 4
	}
211 4
212
	/**
213
	 *
214
	 * @param string $role
215
	 * @param string $resource
216
	 * @param string $permission
217
	 */
218
	public static function removeAcl(string $role, string $resource, string $permission = null) {
219
		self::$aclList->removeAcl($role, $resource, $permission);
220 3
	}
221 3
222 3
	/**
223 3
	 * Initialize acls cache with controllers annotations.
224
	 * Do not execute at runtime
225
	 *
226
	 * @param array $config
227 3
	 * @throws \Ubiquity\exceptions\AclException
228 3
	 */
229 3
	public static function initCache(&$config) {
230 3
		CacheManager::start($config);
231 3
		self::filterProviders(AclCacheProvider::class);
232 3
		self::reloadFromSelectedProviders([]);
233
		self::registerAnnotations($config);
234 3
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
235
		$parser = new AclControllerParser();
236
		$parser->init();
237
		foreach ($files as $file) {
238
			if (\is_file($file)) {
239
				$controller = ClassUtils::getClassFullNameFromFile($file);
240
				try {
241
					$parser->parse($controller);
242 3
				} catch (\Exception $e) {
243 3
					if ($e instanceof AclException) {
244
						throw $e;
245
					}
246
				}
247
			}
248
		}
249 1
		$parser->save();
250 1
		self::removefilterProviders();
251 1
		self::reloadFromSelectedProviders();
252 1
	}
253
254 1
	/**
255
	 *
256
	 * @param array $config
257
	 */
258
	public static function registerAnnotations(&$config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

258
	public static function registerAnnotations(/** @scrutinizer ignore-unused */ &$config) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
259
		CacheManager::registerAnnotations([
260
			'allow' => AllowAnnotation::class,
261
			'resource' => ResourceAnnotation::class,
262
			'permission' => PermissionAnnotation::class
263
		]);
264 1
	}
265 1
266 1
	/**
267 1
	 *
268 1
	 * @return \Ubiquity\security\acl\cache\PermissionsMap
269
	 */
270
	public static function getPermissionMap() {
271
		if (! isset(self::$permissionMap)) {
272
			self::$permissionMap = new PermissionsMap();
273
			self::$permissionMap->load();
274
		}
275
		return self::$permissionMap;
276 2
	}
277 2
278
	/**
279
	 *
280
	 * @param string $controller
281
	 * @param string $action
282
	 * @param string $resource
283
	 * @param string $permission
284
	 */
285
	public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL') {
286 2
		self::$aclList->getResourceByName($resource);
287 2
		self::$aclList->getPermissionByName($permission);
288
		self::$permissionMap->addAction($controller, $action, $resource, $permission);
289
	}
290
291
	/**
292
	 *
293
	 * @param AbstractAclPart $part
294
	 * @param string $providerClass
295 2
	 * @return boolean
296 2
	 */
297
	public static function existPartIn(AbstractAclPart $part, string $providerClass) {
298
		return self::$aclList->existPartIn($part, $providerClass);
299
	}
300
301
	/**
302
	 *
303
	 * @param AclElement $elm
304
	 * @param string $providerClass
305
	 * @return boolean
306
	 */
307
	public static function existAclIn(AclElement $elm, string $providerClass) {
308
		return self::$aclList->existAclIn($elm, $providerClass);
309
	}
310
311
	/**
312
	 *
313
	 * @param string $providerClass
314
	 * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL
315
	 */
316
	public static function getProvider(string $providerClass) {
317
		return self::$aclList->getProvider($providerClass);
318
	}
319
320
	/**
321
	 *
322
	 * @return array
323
	 */
324
	public static function getModelClassesSwap(): array {
325
		$result = [];
326
		$aclList = self::getAclList();
327
		if (isset($aclList)) {
328
			foreach ($aclList->getProviders() as $prov) {
329
				$result += $prov->getModelClassesSwap();
330
			}
331
		}
332
		return $result;
333
	}
334
335
	/**
336
	 * Temporarily filters providers.
337
	 *
338
	 * @param string $providerClass
339
	 */
340
	public static function filterProviders(string $providerClass): void {
341
		$providers = self::$aclList->getProviders();
342
		$filter = [];
343
		foreach ($providers as $prov) {
344
			if ($prov instanceof $providerClass) {
345
				$filter[] = $prov;
346
			}
347
		}
348
		self::$aclList->setProviders($filter);
349
		self::$providersPersistence = $providers;
350
	}
351
352
	/**
353
	 * Remove the providers filtering set with filterProviders call.
354
	 */
355
	public static function removefilterProviders(): void {
356
		self::$aclList->setProviders(self::$providersPersistence);
357
	}
358
}
359
360