Test Failed
Push — master ( de1b4b...7d7395 )
by Jean-Christophe
14:32
created

DAOCache::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Ubiquity\cache\dao;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\cache\system\AbstractDataCache;
7
use Ubiquity\cache\system\MemCachedDriver;
8
9
/**
10
 * Ubiquity\cache\dao$DAOCache
11
 * This class is part of Ubiquity
12
 *
13
 * @author jc
14
 * @version 1.0.0
15
 *
16
 */
17
class DAOCache extends AbstractDAOCache {
18
	/**
19
	 *
20
	 * @var array
21
	 */
22
	private $items;
23
	/**
24
	 *
25
	 * @var AbstractDataCache
26
	 */
27
	protected $cache;
28
29
	protected function getKey($class, $key) {
30
		return \md5 ( $class . $key );
31
	}
32
33
	public function __construct($cacheSystem = MemCachedDriver::class) {
34
		if (\is_string ( $cacheSystem )) {
35
			$this->cache = new $cacheSystem ( CacheManager::getCacheSubDirectory ( 'objects' ), '.object' );
36
		} else {
37
			$this->cache = $cacheSystem;
38
		}
39
	}
40
41
	public function store($class, $key, $object) {
42
		$this->cache->store ( $this->getKey ( $class, $key ), $object );
43
	}
44
45
	public function fetch($class, $key) {
46
		$k = $this->getKey ( $class, $key );
47
		if (! isset ( $this->items [$k] )) {
48
			$this->items [$k] = $this->cache->fetch ( $k );
49
		}
50
		return $this->items [$k];
51
	}
52
53
	public function delete($class, $key) {
54
		$key = $this->getKey ( $class, $key );
55
		if ($this->cache->exists ( $key )) {
56
			return $this->cache->remove ( $key );
57
		}
58
		return false;
59
	}
60
}
61
62