Completed
Push — master ( 95a77b...30a496 )
by Alexander
02:30
created

CacheManager::_getStorage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.9102

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 8
cts 13
cp 0.6153
rs 9.2
cc 4
eloc 11
nc 3
nop 1
crap 4.9102
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\Cache;
12
13
14
use ConsoleHelpers\ConsoleKit\ConsoleIO;
15
16
class CacheManager
17
{
18
19
	/**
20
	 * Working directory.
21
	 *
22
	 * @var string
23
	 */
24
	private $_workingDirectory;
25
26
	/**
27
	 * Console IO.
28
	 *
29
	 * @var ConsoleIO
30
	 */
31
	private $_io;
32
33
	/**
34
	 * Create cache manager.
35
	 *
36
	 * @param string    $working_directory Working directory.
37
	 * @param ConsoleIO $io                Console IO.
38
	 */
39 9
	public function __construct($working_directory, ConsoleIO $io = null)
40
	{
41 9
		$this->_workingDirectory = $working_directory;
42 9
		$this->_io = $io;
43 9
	}
44
45
	/**
46
	 * Sets value in cache.
47
	 *
48
	 * @param string  $name        Name.
49
	 * @param mixed   $value       Value.
50
	 * @param mixed   $invalidator Invalidator.
51
	 * @param integer $duration    Duration in seconds.
52
	 *
53
	 * @return void
54
	 */
55 6
	public function setCache($name, $value, $invalidator = null, $duration = null)
56
	{
57 6
		if ( is_numeric($duration) ) {
58 1
			$duration .= ' seconds';
59 1
		}
60
61 6
		$storage = $this->_getStorage($name);
62 5
		$storage->set(array(
63 5
			'name' => $name,
64 5
			'invalidator' => $invalidator,
65 5
			'expiration' => $duration ? strtotime('+' . $duration) : null,
66 5
			'data' => $value,
67 5
		));
68 5
	}
69
70
	/**
71
	 * Gets value from cache.
72
	 *
73
	 * @param string $name        Name.
74
	 * @param mixed  $invalidator Invalidator.
75
	 *
76
	 * @return mixed
77
	 */
78 4
	public function getCache($name, $invalidator = null)
79
	{
80 4
		$storage = $this->_getStorage($name);
81 4
		$cache = $storage->get();
82
83 4
		if ( !is_array($cache) || $cache['invalidator'] !== $invalidator ) {
84 1
			$storage->invalidate();
85
86 1
			return null;
87
		}
88
89 3
		if ( $cache['expiration'] && $cache['expiration'] < time() ) {
90 1
			$storage->invalidate();
91
92 1
			return null;
93
		}
94
95 3
		return $cache['data'];
96
	}
97
98
	/**
99
	 * Returns file-based cache storage.
100
	 *
101
	 * @param string $name Cache name.
102
	 *
103
	 * @return ICacheStorage
104
	 * @throws \InvalidArgumentException When namespace is missing in the name.
105
	 */
106 6
	private function _getStorage($name)
107
	{
108 6
		$parts = explode(':', $name, 2);
109
110 6
		if ( count($parts) != 2 ) {
111 1
			throw new \InvalidArgumentException('The $name parameter must be in "namespace:name" format.');
112
		}
113
114 5
		$name_hash = substr(hash_hmac('sha1', $parts[1], 'svn-buddy'), 0, 8);
115 5
		$cache_filename = $this->_workingDirectory . DIRECTORY_SEPARATOR . $parts[0] . '_' . $name_hash . '.cache';
116
117 5
		if ( isset($this->_io) && $this->_io->isVerbose() ) {
118
			$this->_io->writeln(array(
119
				'',
120
				'<fg=white;bg=magenta>[cache]: ' . $cache_filename . '</>',
121
			));
122
		}
123
124 5
		return new FileCacheStorage($cache_filename);
125
	}
126
127
}
128