Failed Conditions
Push — master ( 1ba572...22cc0d )
by Alexander
02:39 queued 11s
created

FileCacheStorage::get()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.9332
cc 4
nc 5
nop 0
crap 4
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
/**
15
 * Caches information about repository.
16
 */
17
class FileCacheStorage implements ICacheStorage
18
{
19
20
	const COMPRESS_THRESHOLD = 10240;
21
22
	/**
23
	 * Cache file.
24
	 *
25
	 * @var string
26
	 */
27
	private $_file;
28
29
	/**
30
	 * Creates instance of revision log cache.
31
	 *
32
	 * @param string $file Cache file.
33
	 */
34 21
	public function __construct($file)
35
	{
36 21
		$this->_file = $file;
37
38 21
		$parent_path = dirname($this->_file);
39
40 21
		if ( !file_exists($parent_path) ) {
41 2
			mkdir($parent_path, 0777, true);
42
		}
43 21
	}
44
45
	/**
46
	 * @inheritDoc
47
	 */
48 2
	public function getUniqueId()
49
	{
50 2
		return $this->_file;
51
	}
52
53
	/**
54
	 * @inheritDoc
55
	 *
56
	 * @throws \RuntimeException When cache file doesn't exist.
57
	 */
58 1
	public function getSize()
59
	{
60 1
		if ( !file_exists($this->_file) ) {
61
			throw new \RuntimeException('File "' . $this->_file . '" does not exist.');
62
		}
63
64 1
		return filesize($this->_file);
65
	}
66
67
	/**
68
	 * Gets information from cache.
69
	 *
70
	 * @return array|null
71
	 */
72 14
	public function get()
73
	{
74 14
		if ( !file_exists($this->_file) ) {
75 4
			return null;
76
		}
77
78 11
		$file_contents = file_get_contents($this->_file);
79 11
		$first_symbol = substr($file_contents, 0, 1);
80
81 11
		if ( !in_array($first_symbol, array('{', '[')) ) {
82 1
			$file_contents = gzuncompress($file_contents);
83
		}
84
85 11
		$cache = json_decode($file_contents, true);
86
87 11
		if ( $cache ) {
88 10
			return $cache;
89
		}
90
91 1
		return null;
92
	}
93
94
	/**
95
	 * Stores information in cache.
96
	 *
97
	 * @param array $cache Cache.
98
	 *
99
	 * @return void
100
	 */
101 15
	public function set(array $cache)
102
	{
103 15
		$file_contents = json_encode($cache);
104
105 15
		if ( strlen($file_contents) > self::COMPRESS_THRESHOLD ) {
106 1
			$file_contents = gzcompress($file_contents);
107
		}
108
109 15
		file_put_contents($this->_file, $file_contents);
110 15
	}
111
112
	/**
113
	 * Invalidates cache.
114
	 *
115
	 * @return void
116
	 */
117 10
	public function invalidate()
118
	{
119 10
		if ( file_exists($this->_file) ) {
120 6
			unlink($this->_file);
121
		}
122 10
	}
123
124
}
125