|
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
|
|
|
* Invalid by cron. |
|
46
|
|
|
* @param Driver\ADriver $driver |
|
47
|
|
|
* @param \DateTime|NULL $date |
|
48
|
|
|
*/ |
|
49
|
|
|
public function invalidForce(Driver\ADriver $driver, \DateTime $date = NULL) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->refresh = time() + Utils\DateTime::DAY; |
|
|
|
|
|
|
52
|
|
|
$file = $this->createFileInfo($driver, $date); |
|
53
|
|
|
$this->saveCurrencies($driver, $file, $date); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $allowedCurrencies |
|
58
|
|
|
* @return static |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setAllowedCurrencies(array $allowedCurrencies) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->allowedCurrencies = $allowedCurrencies; |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $hour |
|
68
|
|
|
* @return Storage |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setRefresh($hour) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->refresh = $hour; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function saveCurrencies(Driver\ADriver $driver, \SplFileInfo $file, \DateTime $date = NULL) |
|
77
|
|
|
{ |
|
78
|
|
|
$listRates = $driver->download($date, $this->allowedCurrencies); |
|
79
|
|
|
|
|
80
|
|
|
file_put_contents(Utils\SafeStream::PROTOCOL . '://' . $file->getPathname(), serialize($listRates)); |
|
81
|
|
|
if (self::isFileCurrent($file)) { |
|
82
|
|
|
touch($file->getPathname(), $this->getRefresh()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $listRates; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function createListRate(Driver\ADriver $driver, \SplFileInfo $file, \DateTime $date = NULL) |
|
89
|
|
|
{ |
|
90
|
|
|
if ($this->isFileValid($file)) { |
|
91
|
|
|
Utils\FileSystem::createDir($file->getPath(), 0755); |
|
92
|
|
|
$handle = fopen(Utils\SafeStream::PROTOCOL . '://' . $this->temp . DIRECTORY_SEPARATOR . 'lock', 'w'); |
|
93
|
|
|
|
|
94
|
|
|
if ($this->isFileValid($file)) { |
|
95
|
|
|
$listRate = $this->saveCurrencies($driver, $file, $date); |
|
96
|
|
|
fclose($handle); |
|
97
|
|
|
return $listRate; |
|
98
|
|
|
} |
|
99
|
|
|
fclose($handle); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return unserialize(file_get_contents($file->getPathname())); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param \SplFileInfo $file |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
private function isFileValid(\SplFileInfo $file) |
|
110
|
|
|
{ |
|
111
|
|
|
return !$file->isFile() || (self::isFileCurrent($file) && $file->getMTime() < time()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** @return int */ |
|
115
|
|
|
private function getRefresh() |
|
116
|
|
|
{ |
|
117
|
|
|
if (!is_int($this->refresh)) { |
|
118
|
|
|
$this->refresh = (new \DateTime('today ' . $this->refresh))->format('U'); |
|
119
|
|
|
if (time() >= $this->refresh) { |
|
120
|
|
|
$this->refresh += Utils\DateTime::DAY; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
return $this->refresh; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function createFileInfo(Driver\ADriver $driver, \DateTime $date = NULL) |
|
127
|
|
|
{ |
|
128
|
|
|
$filename = $date === NULL ? self::FILE_CURRENT : $date->format('Y-m-d'); |
|
129
|
|
|
return new \SplFileInfo($this->temp . DIRECTORY_SEPARATOR . $driver->getName() . DIRECTORY_SEPARATOR . $filename); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private static function isFileCurrent(\SplFileInfo $file) |
|
133
|
|
|
{ |
|
134
|
|
|
return $file->getFilename() === self::FILE_CURRENT; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.