Passed
Push — main ( 280cf4...2747d9 )
by Jean-Christophe
02:02
created

AclCacheProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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