|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\CurrencyExchangeBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
15
|
|
|
use ONGR\CurrencyExchangeBundle\Document\CurrencyDocument; |
|
16
|
|
|
use ONGR\CurrencyExchangeBundle\Document\RatesObject; |
|
17
|
|
|
use ONGR\CurrencyExchangeBundle\Driver\CurrencyDriverInterface; |
|
18
|
|
|
use ONGR\CurrencyExchangeBundle\Exception\RatesNotLoadedException; |
|
19
|
|
|
use ONGR\ElasticsearchBundle\Collection\Collection; |
|
20
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
|
21
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This class provides currency rates. |
|
25
|
|
|
*/ |
|
26
|
|
|
class CurrencyRatesService |
|
27
|
|
|
{ |
|
28
|
|
|
use LoggerAwareTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var null|array |
|
32
|
|
|
*/ |
|
33
|
|
|
public $rates = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var CurrencyDriverInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $driver; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CacheProvider |
|
42
|
|
|
*/ |
|
43
|
|
|
private $cache; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Manager |
|
47
|
|
|
*/ |
|
48
|
|
|
private $manager; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param CurrencyDriverInterface $driver Currency exchange driver. |
|
52
|
|
|
* @param Manager $manager ES Manager. |
|
53
|
|
|
* @param CacheProvider $cache Cache pool. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
CurrencyDriverInterface $driver, |
|
57
|
|
|
Manager $manager, |
|
58
|
|
|
CacheProvider $cache |
|
59
|
|
|
) { |
|
60
|
5 |
|
$this->driver = $driver; |
|
61
|
|
|
$this->manager = $manager; |
|
62
|
|
|
$this->cache = $cache; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
/** |
|
66
|
5 |
|
* This method returns exchange rates. |
|
67
|
5 |
|
* |
|
68
|
5 |
|
* @param string|null $date |
|
69
|
|
|
* |
|
70
|
|
|
* @throws RatesNotLoadedException |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getRates($date = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$date = $date ? $date : $this->getCurrentDate(); |
|
76
|
4 |
|
|
|
77
|
|
|
if (isset($this->rates[$date])) { |
|
78
|
4 |
|
return $this->rates[$date]; |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
$rates = $this->cache->fetch($date); |
|
82
|
4 |
|
if ($rates) { |
|
83
|
4 |
|
$this->rates[$date] = $rates; |
|
84
|
4 |
|
return $rates; |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
$rates = $this->getCurrencyFromEs($date); |
|
88
|
3 |
|
if ($rates) { |
|
89
|
3 |
|
$this->rates[$date] = $rates; |
|
90
|
|
|
return $rates; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
$rates = $this->reloadRates($date); |
|
94
|
3 |
|
if ($rates) { |
|
95
|
2 |
|
$this->rates[$date] = $rates; |
|
96
|
|
|
return $rates; |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
throw new RatesNotLoadedException( |
|
100
|
|
|
'Currency rates for '.$date.' are not loaded and could not be loaded on demand' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
3 |
|
* Returns currency rates from ES. |
|
106
|
|
|
* |
|
107
|
3 |
|
* @param string|null $date |
|
108
|
3 |
|
* |
|
109
|
3 |
|
* @return array |
|
110
|
3 |
|
*/ |
|
111
|
3 |
|
private function getCurrencyFromEs($date = null) |
|
112
|
3 |
|
{ |
|
113
|
3 |
|
$date = $date ? $date : $this->getCurrentDate(); |
|
114
|
|
|
|
|
115
|
3 |
|
$rates = []; |
|
116
|
3 |
|
#TODO Should be used service instead of getRepository |
|
117
|
1 |
|
$repository = $this->manager->getRepository('ONGRCurrencyExchangeBundle:CurrencyDocument'); |
|
118
|
|
|
/** @var CurrencyDocument $currency */ |
|
119
|
1 |
|
$currency = $repository->findOneBy(['date' => $date]); |
|
120
|
|
|
|
|
121
|
|
|
if ($currency) { |
|
122
|
2 |
|
/** @var RatesObject $rate */ |
|
123
|
|
|
foreach ($currency->getRates() as $rate) { |
|
|
|
|
|
|
124
|
|
|
$rates[$rate->getName()] = $rate->getValue(); |
|
125
|
|
|
} |
|
126
|
|
|
// $this->logger && $this->logger->info('Rates returned from ES.'); |
|
127
|
|
|
return $rates; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return null; |
|
131
|
|
|
} |
|
132
|
2 |
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Reloads rates using given driver. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string|null $date |
|
137
|
|
|
* |
|
138
|
|
|
* @return array|null |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function reloadRates($date = null) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$date = $date ? $date : $this->getCurrentDate(); |
|
143
|
2 |
|
|
|
144
|
|
|
$rawRates = $this->driver->getRates($date); |
|
145
|
|
|
|
|
146
|
|
|
if ($rawRates) { |
|
|
|
|
|
|
147
|
|
|
$this->rates[$date] = $rawRates; |
|
148
|
4 |
|
$this->cache->save($date, $rawRates); |
|
149
|
|
|
|
|
150
|
4 |
|
$repository = $this->manager->getRepository('ONGRCurrencyExchangeBundle:CurrencyDocument'); |
|
151
|
|
|
/** @var CurrencyDocument $currency */ |
|
152
|
|
|
$document = $repository->findOneBy(['date' => $date]); |
|
153
|
|
|
|
|
154
|
|
|
if (!$document) { |
|
155
|
|
|
$document = new CurrencyDocument(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
$rates = []; |
|
159
|
|
|
foreach ($rawRates as $rate => $value) { |
|
160
|
3 |
|
$rateObj = new RatesObject(); |
|
161
|
3 |
|
$rateObj->setName($rate); |
|
162
|
|
|
$rateObj->setValue($value); |
|
163
|
|
|
$rates[] = $rateObj; |
|
164
|
3 |
|
} |
|
165
|
3 |
|
|
|
166
|
|
|
$document->setRates(new Collection($rates)); |
|
167
|
3 |
|
$this->manager->persist($document); |
|
168
|
2 |
|
$this->manager->commit(); |
|
169
|
2 |
|
|
|
170
|
2 |
|
return $rawRates; |
|
171
|
2 |
|
} |
|
172
|
2 |
|
|
|
173
|
2 |
|
return null; |
|
174
|
2 |
|
} |
|
175
|
2 |
|
|
|
176
|
2 |
|
/** |
|
177
|
2 |
|
* Returns actual base currency name. |
|
178
|
|
|
* |
|
179
|
2 |
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function getBaseCurrency() |
|
182
|
|
|
{ |
|
183
|
1 |
|
return $this->driver->getBaseCurrency(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns formatted current date |
|
188
|
|
|
* |
|
189
|
|
|
* @returns string |
|
190
|
|
|
*/ |
|
191
|
2 |
|
private function getCurrentDate() |
|
192
|
|
|
{ |
|
193
|
2 |
|
return date('Y-m-d'); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|