|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VojtaSvoboda\CnbRates\Providers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Provide data from ČNB and save it to cache |
|
7
|
|
|
* |
|
8
|
|
|
* @package VojtaSvoboda\CnbRates\Providers |
|
9
|
|
|
*/ |
|
10
|
|
|
class CnbDataProvider |
|
11
|
|
|
{ |
|
12
|
|
|
private $cachePathPrefix = 'vojtasvoboda_cnbrates'; |
|
13
|
|
|
|
|
14
|
|
|
private $undefinedFolderName = 'undefined'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get source data |
|
18
|
|
|
* |
|
19
|
|
|
* @param $url |
|
20
|
|
|
* @param null $date Concrete date specifier |
|
21
|
|
|
* @param null $ident Service idend for caching |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
* |
|
25
|
|
|
* @throws |
|
26
|
|
|
*/ |
|
27
|
15 |
|
public function getData($url, $date = null, $ident = null) |
|
28
|
|
|
{ |
|
29
|
15 |
|
if (!$url) { |
|
30
|
2 |
|
throw new \Exception("Service URL can't be empty."); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
13 |
|
$dateObj = new \DateTime($date); |
|
34
|
12 |
|
$date = $dateObj->format('d.m.Y'); |
|
35
|
|
|
|
|
36
|
12 |
|
if (!$this->isFileExists($date, $ident)) { |
|
37
|
12 |
|
$this->saveDataToFile($this->loadDataFromUrl($url), $date, $ident); |
|
38
|
11 |
|
} |
|
39
|
|
|
|
|
40
|
11 |
|
return $this->loadDataFromFile($date, $ident); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Load data from URL |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $url |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
12 |
|
private function loadDataFromUrl($url) |
|
51
|
|
|
{ |
|
52
|
12 |
|
$url .= (is_numeric(substr($url, -1)) ? '&r=' : '?r=') . time(); |
|
53
|
12 |
|
$data = file_get_contents($url); |
|
54
|
|
|
|
|
55
|
11 |
|
return $data; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Load data from file |
|
60
|
|
|
* |
|
61
|
|
|
* @param $date |
|
62
|
|
|
* @param $ident |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
11 |
|
private function loadDataFromFile($date, $ident) |
|
67
|
|
|
{ |
|
68
|
11 |
|
return file_get_contents($this->getFilePath($date, $ident)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Save loaded data to file |
|
73
|
|
|
* |
|
74
|
|
|
* @param $data |
|
75
|
|
|
* @param $date |
|
76
|
|
|
* @param $ident |
|
77
|
|
|
* |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
11 |
|
private function saveDataToFile($data, $date, $ident) |
|
81
|
|
|
{ |
|
82
|
11 |
|
$this->checkCacheFolder($ident); |
|
83
|
|
|
|
|
84
|
11 |
|
return file_put_contents($this->getFilePath($date, $ident), $data); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* If file exists |
|
89
|
|
|
* |
|
90
|
|
|
* @param $date |
|
91
|
|
|
* @param $ident |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
12 |
|
private function isFileExists($date, $ident) |
|
96
|
|
|
{ |
|
97
|
12 |
|
return file_exists($this->getFilePath($date, $ident)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get file path by date and service ident |
|
102
|
|
|
* |
|
103
|
|
|
* @param $date |
|
104
|
|
|
* @param null $ident |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
12 |
|
private function getFilePath($date, $ident = null) |
|
109
|
|
|
{ |
|
110
|
12 |
|
if (!$ident) { |
|
111
|
2 |
|
$ident = $this->undefinedFolderName; |
|
112
|
2 |
|
} |
|
113
|
|
|
|
|
114
|
12 |
|
return temp_path($this->cachePathPrefix . '/' . $ident . '/' . $date . '.txt'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Create cache folder if not exists |
|
119
|
|
|
* |
|
120
|
|
|
* @param $ident |
|
121
|
|
|
*/ |
|
122
|
11 |
|
private function checkCacheFolder($ident) |
|
123
|
|
|
{ |
|
124
|
11 |
|
$cacheFolder = temp_path($this->cachePathPrefix . '/'); |
|
125
|
11 |
|
if (!file_exists($cacheFolder)) { |
|
126
|
1 |
|
mkdir($cacheFolder); |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
11 |
|
if (!$ident) { |
|
130
|
1 |
|
$ident = $this->undefinedFolderName; |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
11 |
|
$identFolder = temp_path($this->cachePathPrefix . '/' . $ident . '/'); |
|
134
|
11 |
|
if (!file_exists($identFolder)) { |
|
135
|
11 |
|
mkdir($identFolder); |
|
136
|
11 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |