Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

FileCache   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filePath() 0 3 1
A save() 0 9 1
A __construct() 0 20 4
A load() 0 15 5
1
<?php
2
3
namespace XoopsModules\Wggithub\Github\Storages;
4
5
use XoopsModules\Wggithub\Github;
6
7
8
/**
9
 * Naive file cache implementation.
10
 *
11
 * @author  Miloslav Hůla (https://github.com/milo)
12
 */
13
class FileCache extends Github\Sanity implements ICache
14
{
15
	/** @var string */
16
	private $dir;
17
18
19
	/**
20
	 * @param  string  temporary directory
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wggithub\Github\Storages\temporary was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
	 *
22
	 * @throws MissingDirectoryException
23
	 */
24
	public function __construct($tempDir)
25
	{
26
		if (!is_dir($tempDir)) {
27
			throw new MissingDirectoryException("Directory '$tempDir' is missing.");
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wggithub\Gi...ssingDirectoryException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
		}
29
30
		$dir = $tempDir . DIRECTORY_SEPARATOR . 'milo.github-api';
31
32
		if (!is_dir($dir)) {
33
			set_error_handler(function($severity, $message, $file, $line) use ($dir, & $valid) {
34
				restore_error_handler();
35
				if (!is_dir($dir)) {
36
					throw new MissingDirectoryException("Cannot create '$dir' directory.", 0, new \ErrorException($message, 0, $severity, $file, $line));
37
				}
38
			});
39
			mkdir($dir);
40
			restore_error_handler();
41
		}
42
43
		$this->dir = $dir;
44
	}
45
46
47
	/**
48
	 * @param  string
49
	 * @param  mixed
50
	 * @return mixed  stored value
51
	 */
52
	public function save($key, $value)
53
	{
54
		file_put_contents(
55
			$this->filePath($key),
56
			serialize($value),
57
			LOCK_EX
58
		);
59
60
		return $value;
61
	}
62
63
64
	/**
65
	 * @param  string
66
	 * @return mixed|NULL
67
	 */
68
	public function load($key)
69
	{
70
		$path = $this->filePath($key);
71
		if (is_file($path) && ($fd = fopen($path, 'rb')) && flock($fd, LOCK_SH)) {
72
			$cached = stream_get_contents($fd);
73
			flock($fd, LOCK_UN);
74
			fclose($fd);
75
76
			$success = TRUE;
77
			set_error_handler(function() use (& $success) { return $success = FALSE; }, E_NOTICE);
78
			$cached = unserialize($cached);
79
			restore_error_handler();
80
81
			if ($success) {
82
				return $cached;
83
			}
84
		}
85
	}
86
87
88
	/**
89
	 * @param  string
90
	 * @return string
91
	 */
92
	private function filePath($key)
93
	{
94
		return $this->dir . DIRECTORY_SEPARATOR . sha1($key) . '.php';
95
	}
96
97
}
98