Completed
Push — master ( 87f113...1d200b )
by Milan
03:04
created

Cache::createListRate()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 6
nop 3
1
<?php
2
3
namespace h4kuna\Exchange\Caching;
4
5
use h4kuna\Exchange\Currency,
6
	h4kuna\Exchange\Driver,
7
	Nette\Utils;
8
9
class Cache implements ICache
10
{
11
	const FILE_CURRENT = 'current';
12
13
	/** @var string */
14
	private $temp;
15
16
	/** @var Currency\ListRates[] */
17
	private $listRates;
18
19
	/** @var array */
20
	private $allowedCurrencies = [];
21
22
	private $refresh = '15:00';
23
24
	public function __construct($temp)
25
	{
26
		$this->temp = $temp;
27
	}
28
29
	public function loadListRate(Driver\ADriver $driver, \DateTime $date = NULL)
30
	{
31
		$file = $this->createFileInfo($driver, $date);
32
		if (isset($this->listRates[$file->getPathname()])) {
33
			return $this->listRates[$file->getPathname()];
34
		}
35
		return $this->listRates[$file->getPathname()] = $this->createListRate($driver, $file, $date);
36
	}
37
38
	public function flushCache(Driver\ADriver $driver, \DateTime $date = NULL)
39
	{
40
		$file = $this->createFileInfo($driver, $date);
41
		$file->isFile() && unlink($file->getPathname());
42
	}
43
44
	/**
45
	 * @param array $allowedCurrencies
46
	 */
47
	public function setAllowedCurrencies(array $allowedCurrencies)
48
	{
49
		$this->allowedCurrencies = $allowedCurrencies;
50
	}
51
52
	/**
53
	 * @param string $hour
54
	 * @return Storage
55
	 */
56
	public function setRefresh($hour)
57
	{
58
		$this->refresh = $hour;
59
		return $this;
60
	}
61
62
	private function saveCurrencies(Driver\ADriver $driver, \SplFileInfo $file, \DateTime $date = NULL)
63
	{
64
		Utils\FileSystem::createDir($file->getPath(), 0755);
65
66
		$handle = fopen(Utils\SafeStream::PROTOCOL . '://' . $this->temp . DIRECTORY_SEPARATOR . 'lock', 'w');
67
		$listRates = $driver->download($date, $this->allowedCurrencies);
68
69
		if (self::isFileCurrent($file) || !$file->isFile()) {
70
			file_put_contents($file->getPathname(), serialize($listRates));
71
			touch($file->getPathname(), $this->getRefresh());
72
		}
73
74
		fclose($handle);
75
76
		return $listRates;
77
	}
78
79
	private function createListRate(Driver\ADriver $driver, \SplFileInfo $file, \DateTime $date = NULL)
80
	{
81
		return !$file->isFile() || (self::isFileCurrent($file) && $file->getMTime() < time()) ?
82
			$this->saveCurrencies($driver, $file, $date) :
83
			$listRate = unserialize(file_get_contents($file->getPathname()));
84
85
	}
86
87
	/** @return int */
88
	private function getRefresh()
89
	{
90
		if (!is_int($this->refresh)) {
91
			$this->refresh = (new \DateTime('today ' . $this->refresh))->format('U');
92
			if (time() >= $this->refresh) {
93
				$this->refresh += Utils\DateTime::DAY;
94
			}
95
		}
96
		return $this->refresh;
97
	}
98
99
	private function createFileInfo(Driver\ADriver $driver, \DateTime $date = NULL)
100
	{
101
		$filename = $date === NULL ? self::FILE_CURRENT : $date->format('Y-m-d');
102
		return new \SplFileInfo($this->temp . DIRECTORY_SEPARATOR . $driver->getName() . DIRECTORY_SEPARATOR . $filename);
103
	}
104
105
	private static function isFileCurrent(\SplFileInfo $file)
106
	{
107
		return $file->getFilename() === self::FILE_CURRENT;
108
	}
109
110
}
111