Test Failed
Push — main ( d55b52...b5ed50 )
by Jean-Christophe
03:55
created

AclListOperationsTrait::removeResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 4
b 0
f 1
nc 1
nop 1
dl 0
loc 6
ccs 6
cts 6
cp 1
crap 1
rs 10
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
7
/**
8
 * Ubiquity\security\acl\models\traits$AclListOperations
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
trait AclListOperationsTrait {
16
17
	abstract public function getRoleByName(string $name);
18
19
	abstract public function getResourceByName(string $name);
20
21
	abstract public function getPermissionByName(string $name);
22
23 13
	public function saveAclElement(AclElement $aclElement) {
24 13
		foreach ($this->providers as $provider) {
25 5
			$provider->saveAcl($aclElement);
26
		}
27 13
	}
28
29
	public function removeAclElement(AclElement $aclElement) {
30
		foreach ($this->providers as $provider) {
31
			$provider->removeAcl($aclElement);
32
		}
33
	}
34
35 15
	public function savePart(AbstractAclPart $aclPart) {
36 15
		foreach ($this->providers as $provider) {
37 7
			$provider->savePart($aclPart);
38
		}
39 15
	}
40
41 2
	public function updatePart(AbstractAclPart $aclPart) {
42 2
		foreach ($this->providers as $provider) {
43 2
			$provider->updatePart($aclPart);
44
		}
45 2
	}
46
47 6
	public function removePart(AbstractAclPart $aclPart) {
48 6
		foreach ($this->providers as $provider) {
49 6
			$provider->removePart($aclPart);
50
		}
51 6
	}
52
53 4
	public function removeRole(string $roleName) {
54 4
		$role = $this->getRoleByName($roleName);
55 4
		unset($this->roles[$roleName]);
56 4
		$this->unsetCache("role_$roleName");
57 4
		$this->removeAcl($roleName);
58 4
		$this->removePart($role);
59 4
	}
60
61 6
	protected function unsetCache($name) {
62 6
		if (isset($this->elementsCache[$name])) {
63 6
			unset($this->elementsCache[$name]);
64
		}
65 6
	}
66
67 4
	public function removePermission(string $permissionName) {
68 4
		$permission = $this->getPermissionByName($permissionName);
69 4
		unset($this->permissions[$permissionName]);
70 4
		$this->unsetCache("perm_$permissionName");
71 4
		$this->removeAcl(null, null, $permissionName);
72 4
		$this->removePart($permission);
73 4
	}
74
75 2
	public function removeResource(string $resourceName) {
76 2
		$resource = $this->getResourceByName($resourceName);
77 2
		unset($this->resources[$resourceName]);
78 2
		$this->unsetCache("res_$resourceName");
79 2
		$this->removeAcl(null, $resourceName);
80 2
		$this->removePart($resource);
81 2
	}
82
83 8
	public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
84 8
		$toRemove = [];
85 8
		foreach ($this->acls as $index => $acl) {
86 8
			if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
87 4
				foreach ($this->providers as $provider) {
88 4
					$provider->removeAcl($acl);
89
				}
90 4
				$toRemove[] = $index;
91
			}
92
		}
93 8
		foreach ($toRemove as $remove) {
94 4
			unset($this->acls[$remove]);
95
		}
96 8
	}
97
98 5
	public function saveAll() {
99 5
		foreach ($this->providers as $provider) {
100 5
			if (! $provider->isAutosave()) {
101 4
				$provider->saveAll();
102
			}
103
		}
104 5
	}
105
106
	public function clear() {
107
		$this->roles = [];
0 ignored issues
show
Bug Best Practice introduced by
The property roles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
108
		$this->resources = [];
0 ignored issues
show
Bug Best Practice introduced by
The property resources does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
		$this->permissions = [];
0 ignored issues
show
Bug Best Practice introduced by
The property permissions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
110
		$this->elementsCache = [];
0 ignored issues
show
Bug Best Practice introduced by
The property elementsCache does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
		$this->init();
0 ignored issues
show
Bug introduced by
It seems like init() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

111
		$this->/** @scrutinizer ignore-call */ 
112
         init();
Loading history...
112
	}
113
114
	public function addRole(Role $role) {
0 ignored issues
show
Bug introduced by
The type Ubiquity\security\acl\models\traits\Role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
		$this->roles[$role->getName()] = $role;
0 ignored issues
show
Bug Best Practice introduced by
The property roles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
116
		$this->savePart($role);
117
	}
118
119
	public function addResource(Resource $resource) {
0 ignored issues
show
Bug introduced by
The type Ubiquity\security\acl\models\traits\Resource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
		$this->resources[$resource->getName()] = $resource;
0 ignored issues
show
Bug Best Practice introduced by
The property resources does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
121
		$this->savePart($resource);
122
	}
123
124
	public function addPermission(Permission $permission) {
0 ignored issues
show
Bug introduced by
The type Ubiquity\security\acl\models\traits\Permission was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
		$this->permissions[$permission->getName()] = $permission;
0 ignored issues
show
Bug Best Practice introduced by
The property permissions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
126
		$this->savePart($permission);
127
	}
128
129
	public function setPermissionLevel(string $name, int $level) {
130
		$perm = $this->getPermissionByName($name);
131
		$perm->setLevel($level);
132
		$this->updatePart($perm);
133
	}
134
135
	public function allow(string $roleName, string $resourceName, string $permissionName) {
136
		$aclElm = new AclElement();
137
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
138
		$this->acls[] = $aclElm;
0 ignored issues
show
Bug Best Practice introduced by
The property acls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
139
		$this->saveAclElement($aclElm);
140
	}
141
142
	public function addAndAllow(string $roleName, string $resourceName, string $permissionName) {
143
		if (! $this->elementExistByName($roleName, $this->roles)) {
0 ignored issues
show
Bug introduced by
It seems like elementExistByName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

143
		if (! $this->/** @scrutinizer ignore-call */ elementExistByName($roleName, $this->roles)) {
Loading history...
144
			$this->addRole(new Role($roleName));
145
		}
146
		if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
147
			$this->addResource(new Resource($resourceName));
148
		}
149
		if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
150
			$this->addPermission(new Permission($permissionName));
151
		}
152
		$this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL');
153
	}
154
}
155
156