Failed Conditions
Push — master ( 830d2d...99bc8c )
by Alexander
02:52
created

CacheManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 130
ccs 45
cts 45
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setCache() 0 14 3
B getCache() 0 19 5
B _getStorage() 0 29 5
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
use ConsoleHelpers\SVNBuddy\Helper\SizeHelper;
16
17
class CacheManager
18
{
19
20
	/**
21
	 * Working directory.
22
	 *
23
	 * @var string
24
	 */
25
	private $_workingDirectory;
26
27
	/**
28
	 * Console IO.
29
	 *
30
	 * @var ConsoleIO
31
	 */
32
	private $_io;
33
34
	/**
35
	 * Size helper.
36
	 *
37
	 * @var SizeHelper
38
	 */
39
	private $_sizeHelper;
40
41
	/**
42
	 * Create cache manager.
43
	 *
44
	 * @param string     $working_directory Working directory.
45
	 * @param SizeHelper $size_helper       Size helper.
46
	 * @param ConsoleIO  $io                Console IO.
47
	 */
48 14
	public function __construct($working_directory, SizeHelper $size_helper, ConsoleIO $io = null)
49
	{
50 14
		$this->_workingDirectory = $working_directory;
51 14
		$this->_sizeHelper = $size_helper;
52 14
		$this->_io = $io;
53 14
	}
54
55
	/**
56
	 * Sets value in cache.
57
	 *
58
	 * @param string  $name        Name.
59
	 * @param mixed   $value       Value.
60
	 * @param mixed   $invalidator Invalidator.
61
	 * @param integer $duration    Duration in seconds.
62
	 *
63
	 * @return void
64
	 */
65 8
	public function setCache($name, $value, $invalidator = null, $duration = null)
66
	{
67 8
		if ( is_numeric($duration) ) {
68 1
			$duration .= ' seconds';
69 1
		}
70
71 8
		$storage = $this->_getStorage($name);
72 7
		$storage->set(array(
73 7
			'name' => $name,
74 7
			'invalidator' => $invalidator,
75 7
			'expiration' => $duration ? strtotime('+' . $duration) : null,
76 7
			'data' => $value,
77 7
		));
78 7
	}
79
80
	/**
81
	 * Gets value from cache.
82
	 *
83
	 * @param string $name        Name.
84
	 * @param mixed  $invalidator Invalidator.
85
	 *
86
	 * @return mixed
87
	 */
88 9
	public function getCache($name, $invalidator = null)
89
	{
90 9
		$storage = $this->_getStorage($name);
91 9
		$cache = $storage->get();
92
93 9
		if ( !is_array($cache) || $cache['invalidator'] !== $invalidator ) {
94 4
			$storage->invalidate();
95
96 4
			return null;
97
		}
98
99 5
		if ( $cache['expiration'] && $cache['expiration'] < time() ) {
100 1
			$storage->invalidate();
101
102 1
			return null;
103
		}
104
105 5
		return $cache['data'];
106
	}
107
108
	/**
109
	 * Returns file-based cache storage.
110
	 *
111
	 * @param string $name Cache name.
112
	 *
113
	 * @return ICacheStorage
114
	 * @throws \InvalidArgumentException When namespace is missing in the name.
115
	 */
116 11
	private function _getStorage($name)
117
	{
118 11
		$parts = explode(':', $name, 2);
119
120 11
		if ( count($parts) != 2 ) {
121 1
			throw new \InvalidArgumentException('The $name parameter must be in "namespace:name" format.');
122
		}
123
124 10
		$name_hash = substr(hash_hmac('sha1', $parts[1], 'svn-buddy'), 0, 8);
125 10
		$cache_filename = $this->_workingDirectory . DIRECTORY_SEPARATOR . $parts[0] . '_' . $name_hash . '.cache';
126
127 10
		if ( isset($this->_io) && $this->_io->isVerbose() ) {
128 2
			$message = $cache_filename;
129
130 2
			if ( file_exists($cache_filename) ) {
131 1
				$message .= ' (hit: ' . $this->_sizeHelper->formatSize(filesize($cache_filename)) . ')';
132 1
			}
133
			else {
134 2
				$message .= ' (miss)';
135
			}
136
137 2
			$this->_io->writeln(array(
138 2
				'',
139 2
				'<debug>[cache]: ' . $message . '</debug>',
140 2
			));
141 2
		}
142
143 10
		return new FileCacheStorage($cache_filename);
144
	}
145
146
}
147