Completed
Push — master ( e46910...56fa23 )
by Alexander
02:53
created

DatabaseCache::cacheTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Database;
12
13
14
use Aura\Sql\ExtendedPdoInterface;
15
16
class DatabaseCache
17
{
18
19
	/**
20
	 * Database cache.
21
	 *
22
	 * @var array
23
	 */
24
	private $_cache = array();
25
26
	/**
27
	 * Database.
28
	 *
29
	 * @var ExtendedPdoInterface
30
	 */
31
	private $_database;
32
33
	/**
34
	 * Creates cache instance.
35
	 *
36
	 * @param ExtendedPdoInterface $database Database.
37
	 */
38 4
	public function __construct(ExtendedPdoInterface $database)
39
	{
40 4
		$this->_database = $database;
41 4
	}
42
43
	/**
44
	 * Adds table to caching.
45
	 *
46
	 * @param string $table Table.
47
	 *
48
	 * @return void
49
	 */
50 4
	public function cacheTable($table)
51
	{
52 4
		$this->_cache[$table] = array();
53 4
	}
54
55
	/**
56
	 * Gets cached data.
57
	 *
58
	 * @param string      $table     Table.
59
	 * @param mixed       $cache_key Key.
60
	 * @param string|null $sql       Fallback sql used to populate cache on the fly.
61
	 * @param array       $values    Fallback values used together with above sql.
62
	 *
63
	 * @return array|boolean
64
	 */
65 4
	public function getFromCache($table, $cache_key, $sql = null, array $values = array())
66
	{
67 4
		if ( isset($this->_cache[$table][$cache_key]) ) {
68 3
			return $this->_cache[$table][$cache_key];
69
		}
70
71 4
		if ( isset($sql) ) {
72 2
			$result = $this->_database->fetchOne($sql, $values);
73
74 2
			if ( $result !== false ) {
75 1
				$this->setIntoCache($table, $cache_key, $result);
76
77 1
				return $this->_cache[$table][$cache_key];
78
			}
79 1
		}
80
81 3
		return false;
82
	}
83
84
	/**
85
	 * Gets cached data.
86
	 *
87
	 * @param string $table     Table.
88
	 * @param mixed  $cache_key Key.
89
	 * @param array  $data      Data.
90
	 *
91
	 * @return void
92
	 */
93 3
	public function setIntoCache($table, $cache_key, array $data)
94
	{
95 3
		if ( isset($this->_cache[$table][$cache_key]) ) {
96 1
			$this->_cache[$table][$cache_key] = array_replace($this->_cache[$table][$cache_key], $data);
97 1
		}
98
		else {
99 3
			$this->_cache[$table][$cache_key] = $data;
100
		}
101 3
	}
102
103
	/**
104
	 * Clears cache.
105
	 *
106
	 * @return void
107
	 */
108 1
	public function clear()
109
	{
110 1
		$tables = array_keys($this->_cache);
111 1
		$this->_cache = array();
112
113 1
		foreach ( $tables as $table_name ) {
114 1
			$this->cacheTable($table_name);
115 1
		}
116 1
	}
117
118
}
119