Passed
Push — main ( 6cee10...25dcc1 )
by Jean-Christophe
02:23
created

AclListOperationsTrait   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 88.3%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
eloc 60
dl 0
loc 138
ccs 83
cts 94
cp 0.883
rs 9.2
c 7
b 1
f 1
wmc 40

18 Methods

Rating   Name   Duplication   Size   Complexity  
A saveAll() 0 4 3
A setPermissionLevel() 0 4 1
A removeRole() 0 6 1
A clear() 0 2 1
A allow() 0 5 1
B removeAcl() 0 12 10
A unsetCache() 0 3 2
A addRole() 0 3 1
A addAndAllow() 0 11 6
A savePart() 0 3 2
A addResource() 0 3 1
A addPermission() 0 3 1
A removePart() 0 3 2
A updatePart() 0 3 2
A removePermission() 0 6 1
A removeAclElement() 0 3 2
A saveAclElement() 0 3 2
A removeResource() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like AclListOperationsTrait 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 AclListOperationsTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Ubiquity\security\acl\models\traits;
3
4
use Ubiquity\security\acl\models\AclElement;
5
use Ubiquity\security\acl\models\AbstractAclPart;
6
use Ubiquity\security\acl\models\Role;
7
use Ubiquity\security\acl\models\Resource;
8
use Ubiquity\security\acl\models\Permission;
9
use Ubiquity\security\acl\persistence\AclProviderInterface;
10
11
/**
12
 * Ubiquity\security\acl\models\traits$AclListOperations
13
 * This class is part of Ubiquity
14
 *
15
 * @author jc
16
 * @version 1.0.0
17
 * @property Role[] $roles
18
 * @property Resource[] $resources
19
 * @property Permission[] $permissions
20
 * @property AclProviderInterface[] $providers
21
 * @property array $elementsCache
22
 * @property AclElement[] $acls
23
 *
24
 */
25
trait AclListOperationsTrait {
26
27
	abstract public function getRoleByName(string $name);
28
29
	abstract public function getResourceByName(string $name);
30
31
	abstract public function getPermissionByName(string $name);
32
33
	abstract public function init();
34
35
	abstract protected function elementExistByName(string $name, array $inArray): bool;
36
37 13
	public function saveAclElement(AclElement $aclElement) {
38 13
		foreach ($this->providers as $provider) {
39 5
			$provider->saveAcl($aclElement);
40
		}
41 13
	}
42
43
	public function removeAclElement(AclElement $aclElement) {
44
		foreach ($this->providers as $provider) {
45
			$provider->removeAcl($aclElement);
46
		}
47
	}
48
49 14
	public function savePart(AbstractAclPart $aclPart) {
50 14
		foreach ($this->providers as $provider) {
51 6
			$provider->savePart($aclPart);
52
		}
53 14
	}
54
55 2
	public function updatePart(AbstractAclPart $aclPart) {
56 2
		foreach ($this->providers as $provider) {
57 2
			$provider->updatePart($aclPart);
58
		}
59 2
	}
60
61 4
	public function removePart(AbstractAclPart $aclPart) {
62 4
		foreach ($this->providers as $provider) {
63 4
			$provider->removePart($aclPart);
64
		}
65 4
	}
66
67 2
	public function removeRole(string $roleName) {
68 2
		$role = $this->getRoleByName($roleName);
69 2
		unset($this->roles[$roleName]);
70 2
		$this->unsetCache("role_$roleName");
71 2
		$this->removeAcl($roleName);
72 2
		$this->removePart($role);
73 2
	}
74
75 4
	protected function unsetCache($name) {
76 4
		if (isset($this->elementsCache[$name])) {
77 4
			unset($this->elementsCache[$name]);
78
		}
79 4
	}
80
81 2
	public function removePermission(string $permissionName) {
82 2
		$permission = $this->getPermissionByName($permissionName);
83 2
		unset($this->permissions[$permissionName]);
84 2
		$this->unsetCache("perm_$permissionName");
85 2
		$this->removeAcl(null, null, $permissionName);
86 2
		$this->removePart($permission);
87 2
	}
88
89
	public function removeResource(string $resourceName) {
90
		$resource = $this->getResourceByName($resourceName);
91
		unset($this->resources[$resourceName]);
92
		$this->unsetCache("res_$resourceName");
93
		$this->removeAcl(null, $resourceName);
94
		$this->removePart($resource);
95
	}
96
97 6
	public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
98 6
		$toRemove = [];
99 6
		foreach ($this->acls as $index => $acl) {
100 6
			if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
101 2
				foreach ($this->providers as $provider) {
102 2
					$provider->removeAcl($acl);
103
				}
104 2
				$toRemove[] = $index;
105
			}
106
		}
107 6
		foreach ($toRemove as $remove) {
108 2
			unset($this->acls[$remove]);
109
		}
110 6
	}
111
112 4
	public function saveAll() {
113 4
		foreach ($this->providers as $provider) {
114 4
			if (! $provider->isAutosave()) {
115 3
				$provider->saveAll();
116
			}
117
		}
118 4
	}
119
120 3
	public function clear() {
121 3
		$this->init();
122 3
	}
123
124 10
	public function addRole(Role $role) {
125 10
		$this->roles[$role->getName()] = $role;
126 10
		$this->savePart($role);
127 10
	}
128
129 9
	public function addResource(Resource $resource) {
130 9
		$this->resources[$resource->getName()] = $resource;
131 9
		$this->savePart($resource);
132 9
	}
133
134 11
	public function addPermission(Permission $permission) {
135 11
		$this->permissions[$permission->getName()] = $permission;
136 11
		$this->savePart($permission);
137 11
	}
138
139 3
	public function setPermissionLevel(string $name, int $level) {
140 3
		$perm = $this->getPermissionByName($name);
141 2
		$perm->setLevel($level);
142 2
		$this->updatePart($perm);
143 2
	}
144
145 13
	public function allow(string $roleName, string $resourceName, string $permissionName) {
146 13
		$aclElm = new AclElement();
147 13
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
148 13
		$this->acls[] = $aclElm;
149 13
		$this->saveAclElement($aclElm);
150 13
	}
151
152 3
	public function addAndAllow(string $roleName, string $resourceName, string $permissionName) {
153 3
		if (! $this->elementExistByName($roleName, $this->roles)) {
154 3
			$this->addRole(new Role($roleName));
155
		}
156 3
		if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
157 3
			$this->addResource(new Resource($resourceName));
158
		}
159 3
		if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
160 3
			$this->addPermission(new Permission($permissionName));
161
		}
162 3
		$this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL');
163 3
	}
164
}
165
166