Passed
Push — master ( 3b5a7c...0f24e7 )
by Jean-Christophe
05:29
created

TableCache::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\cache\database;
4
5
use Ubiquity\utils\base\UArray;
6
7
class TableCache extends DbCache {
8
	protected $arrayCache;
9
10 5
	public function store($tableName, $condition, $result) {
11 5
		$exists=$this->getCache($tableName);
12 5
		$exists[$this->getKey($condition)]=$result;
13 5
		$this->cache->store($tableName, "return " . UArray::asPhpArray($exists, "array") . ";");
14 5
	}
15
16 5
	public function getCache($tableName) {
17 5
		if ($this->cache->exists($tableName))
18 4
			return $this->cache->fetch($tableName);
19 2
		return [ ];
20
	}
21
22 6
	protected function getArrayCache($tableName) {
23 6
		if (isset($this->arrayCache[$tableName]))
24
			return $this->arrayCache[$tableName];
25 6
		if ($this->cache->exists($tableName)) {
26 5
			return $this->arrayCache[$tableName]=$this->cache->fetch($tableName);
27
		}
28 2
		return false;
29
	}
30
31 6
	public function fetch($tableName, $condition) {
32 6
		if ($cache=$this->getArrayCache($tableName)) {
33 5
			$key=$this->getKey($condition);
34 5
			if (isset($cache[$key]))
35 2
				return $cache[$key];
36
		}
37 5
		return false;
38
	}
39
40
	public function delete($tableName, $condition){
41
		if ($cache=$this->getArrayCache($tableName)) {
42
			$key=$this->getKey($condition);
43
			if (isset($cache[$key])){
44
				unset($cache[$key]);
45
				$this->cache->store($tableName, "return " . UArray::asPhpArray($cache, "array") . ";");
46
			}
47
		}
48
	}
49
}
50