From fa850242d86fe68be8eef7e4b4b89218c8be1b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= Date: Mon, 1 Aug 2016 11:04:12 +0300 Subject: [PATCH] fixed past date currency rate saving to avoid duplicates of current date --- Service/CurrencyRatesService.php | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/Service/CurrencyRatesService.php b/Service/CurrencyRatesService.php index 2ff7fcb..52fafba 100644 --- a/Service/CurrencyRatesService.php +++ b/Service/CurrencyRatesService.php @@ -160,27 +160,25 @@ public function reloadRates($date = null) $this->rates[$date] = $rawRates; $this->cache->save($date, $rawRates); - $repository = $this->manager->getRepository('ONGRCurrencyExchangeBundle:CurrencyDocument'); - /** @var CurrencyDocument $currency */ - $document = $repository->findOneBy(['date' => $date]); - - if (!$document) { + $rates = $this->getCurrencyFromEs($date); + if (empty($rates)) { $document = new CurrencyDocument(); - } + $document->setDate($date); - $rates = []; - foreach ($rawRates as $rate => $value) { - $rateObj = new RatesObject(); - $rateObj->setName($rate); - $rateObj->setValue($value); - $rates[] = $rateObj; - } + $rates = []; + foreach ($rawRates as $rate => $value) { + $rateObj = new RatesObject(); + $rateObj->setName($rate); + $rateObj->setValue($value); + $rates[] = $rateObj; + } - $document->setRates(new Collection($rates)); - $this->manager->persist($document); - $this->manager->commit(); + $document->setRates(new Collection($rates)); + $this->manager->persist($document); + $this->manager->commit(); - return $rawRates; + return $rawRates; + } } return null;