Passed
Push — main ( f54f2d...d212fa )
by Jean-Christophe
02:04
created

AclCacheProvider::saveAcl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
namespace Ubiquity\security\acl\persistence;
3
4
use Ubiquity\cache\CacheManager;
5
use Ubiquity\security\acl\models\AbstractAclPart;
6
use Ubiquity\security\acl\models\AclElement;
7
use Ubiquity\security\acl\models\Role;
8
use Ubiquity\security\acl\models\Resource;
9
use Ubiquity\security\acl\models\Permission;
10
11
class AclCacheProvider extends AclArrayProvider {
12
13
	private $key = 'acls/';
14
15 1
	private function getRootKey($element) {
16 1
		return $this->key . $element;
17
	}
18
19 1
	public function __construct() {
20 1
		$cacheKeys = $this->getCacheKeys();
21 1
		foreach ($cacheKeys as $key) {
22 1
			$this->createCache($key);
23
		}
24 1
	}
25
26 1
	protected function getCacheKeys(): array {
27
		return [
28 1
			$this->getRootKey('acls'),
29 1
			$this->getRootKey(Role::class),
30 1
			$this->getRootKey(Resource::class),
31 1
			$this->getRootKey(Permission::class)
32
		];
33
	}
34
35 1
	protected function createCache($part) {
36 1
		if (! CacheManager::$cache->exists($part)) {
37 1
			CacheManager::$cache->store($part, []);
38
		}
39 1
	}
40
41 1
	protected function loadAllPart($class): array {
42 1
		$this->parts[$class] = CacheManager::$cache->fetch($this->getRootKey($class));
43 1
		return parent::loadAllPart($class);
44
	}
45
46 1
	public function loadAllAcls(): array {
47 1
		$this->aclsArray = CacheManager::$cache->fetch($this->getRootKey('acls'));
48 1
		return parent::loadAllAcls();
49
	}
50
51
	public function savePart(AbstractAclPart $part) {
52
		$this->_savePart($part);
53
	}
54
55
	public function saveAcl(AclElement $aclElement) {
56
		$this->_saveAcl($aclElement);
57
	}
58
59
	public function isAutosave(): bool {
60
		return false;
61
	}
62
63
	public function saveAll(): void {
64
		CacheManager::$cache->store($this->getRootKey('acls'), $this->aclsArray);
65
		$classes = [
66
			Role::class,
67
			Resource::class,
68
			Permission::class
69
		];
70
		foreach ($classes as $class) {
71
			CacheManager::$cache->store($this->getRootKey($class), $this->parts[$class]);
72
		}
73
	}
74
}
75
76