AclArrayProvider::loadAllAcls()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ubiquity\security\acl\persistence;
3
4
use Ubiquity\security\acl\models\AclElement;
5
use Ubiquity\security\acl\models\Permission;
6
use Ubiquity\security\acl\models\Resource;
7
use Ubiquity\security\acl\models\Role;
8
use Ubiquity\exceptions\AclException;
9
use Ubiquity\security\acl\models\AbstractAclPart;
10
11
/**
12
 * Ubiquity\security\acl\persistence$AclArrayProvider
13
 * This class is part of Ubiquity
14
 *
15
 * @author jc
16
 * @version 1.0.0
17
 *
18
 */
19
abstract class AclArrayProvider implements AclProviderInterface {
20
21
	protected $aclsArray;
22
23
	protected $parts;
24
25
	public function __construct() {}
26
27 6
	protected function loadAllPart($class): array {
28 6
		$elements = [];
29 6
		$part = $this->parts[$class] ?? [];
30 6
		foreach ($part as $partArray) {
31 6
			$elm = new $class();
32 6
			$elm->fromArray($partArray);
33 6
			$elements[$partArray['name']] = $elm;
34
		}
35 6
		return $elements;
36
	}
37
38
	/**
39
	 *
40
	 * {@inheritdoc}
41
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllAcls()
42
	 */
43 6
	public function loadAllAcls(): array {
44 6
		$acls = [];
45 6
		foreach ($this->aclsArray as $aclArray) {
46 6
			$aclElement = new AclElement();
47 6
			$aclElement->fromArray($aclArray);
48 6
			$acls[$aclElement->getId_()] = $aclElement;
49
		}
50 6
		return $acls;
51
	}
52
53 4
	public function saveAcl(AclElement $aclElement) {
54 4
		$this->aclsArray[$aclElement->getId_()] = $aclElement->toArray();
55
	}
56
57 1
	public function removeAcl(AclElement $aclElement) {
58 1
		unset($this->aclsArray[$aclElement->getId_()]);
59
	}
60
61
	/**
62
	 *
63
	 * {@inheritdoc}
64
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllPermissions()
65
	 */
66 6
	public function loadAllPermissions(): array {
67 6
		return $this->loadAllPart(Permission::class);
68
	}
69
70
	/**
71
	 *
72
	 * {@inheritdoc}
73
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllResources()
74
	 */
75 6
	public function loadAllResources(): array {
76 6
		return $this->loadAllPart(Resource::class);
77
	}
78
79
	/**
80
	 *
81
	 * {@inheritdoc}
82
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllRoles()
83
	 */
84 6
	public function loadAllRoles(): array {
85 6
		return $this->loadAllPart(Role::class);
86
	}
87
88 4
	public function savePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
89 4
		$class = \get_class($part);
90 4
		$this->parts[$class][$part->getName()] = $part->toArray();
91
	}
92
93 1
	public function updatePart(string $id,\Ubiquity\security\acl\models\AbstractAclPart $part) {
94 1
		$class = \get_class($part);
95 1
		$this->parts[$class][$id] = $part->toArray();
96
	}
97
98 2
	public function removePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
99 2
		$name = $part->getName();
100 2
		if ($part instanceof Resource) {
101
			$field = 'resource';
102 2
		} elseif ($part instanceof Role) {
103 1
			$field = 'role';
104
		} else {
105 1
			$field = 'permission';
106
		}
107 2
		foreach ($this->aclsArray as $acl) {
108 2
			if ($acl[$field]['name'] === $name) {
109
				throw new AclException("$name is in use in ACLs and can't be removed!");
110
			}
111
		}
112 2
		unset($this->parts[\get_class($part)][$name]);
113
	}
114
115 1
	public function existPart(AbstractAclPart $part): bool {
116 1
		$name = $part->getName();
117 1
		return isset($this->parts[\get_class($part)][$name]);
118
	}
119
120 1
	public function existAcl(AclElement $aclElement): bool {
121 1
		return isset($this->aclsArray[$aclElement->getId_()]);
122
	}
123
124
	public function getModelClassesSwap(): array {
125
		return [];
126
	}
127
128
	/**
129
	 *
130
	 * {@inheritdoc}
131
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::clearAll()
132
	 */
133 2
	public function clearAll(): void {
134 2
		$this->parts = [];
135 2
		$this->aclsArray = [];
136
	}
137
}
138
139