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

DAOSerialCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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