diff --git a/config/packages/swap.yaml b/config/packages/swap.yaml index 6fabc893..02c10f61 100644 --- a/config/packages/swap.yaml +++ b/config/packages/swap.yaml @@ -5,6 +5,9 @@ florianv_swap: providers: european_central_bank: ~ # European Central Bank (only works for EUR base currency) - fixer: # Fixer.io (needs an API key) - access_key: "%env(string:settings:exchange_rate:fixerApiKey)%" - #exchange_rates_api: ~ + central_bank_of_czech_republic: ~ + central_bank_of_republic_turkey: ~ + national_bank_of_romania: ~ + + fixer: # Fixer.io (needs an API key) + access_key: "%env(string:settings:exchange_rate:fixerApiKey)%" diff --git a/src/Services/Tools/ExchangeRateUpdater.php b/src/Services/Tools/ExchangeRateUpdater.php index 7c14b16f..6eb7ec13 100644 --- a/src/Services/Tools/ExchangeRateUpdater.php +++ b/src/Services/Tools/ExchangeRateUpdater.php @@ -26,6 +26,8 @@ use App\Entity\PriceInformations\Currency; use App\Settings\SystemSettings\LocalizationSettings; use Brick\Math\BigDecimal; use Brick\Math\RoundingMode; +use Exchanger\Exception\UnsupportedCurrencyPairException; +use Exchanger\Exception\UnsupportedExchangeQueryException; use Swap\Swap; class ExchangeRateUpdater @@ -39,15 +41,21 @@ class ExchangeRateUpdater */ public function update(Currency $currency): Currency { - //Currency pairs are always in the format "BASE/QUOTE" - $rate = $this->swap->latest($this->localizationSettings->baseCurrency.'/'.$currency->getIsoCode()); - //The rate says how many quote units are worth one base unit - //So we need to invert it to get the exchange rate + try { + //Try it in the direction QUOTE/BASE first, as most providers provide rates in this direction + $rate = $this->swap->latest($currency->getIsoCode().'/'.$this->localizationSettings->baseCurrency); + $effective_rate = BigDecimal::of($rate->getValue()); + } catch (UnsupportedCurrencyPairException|UnsupportedExchangeQueryException $exception) { + //Otherwise try to get it inverse and calculate it ourselfes, from the format "BASE/QUOTE" + $rate = $this->swap->latest($this->localizationSettings->baseCurrency.'/'.$currency->getIsoCode()); + //The rate says how many quote units are worth one base unit + //So we need to invert it to get the exchange rate - $rate_bd = BigDecimal::of($rate->getValue()); - $rate_inverted = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP); + $rate_bd = BigDecimal::of($rate->getValue()); + $effective_rate = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP); + } - $currency->setExchangeRate($rate_inverted); + $currency->setExchangeRate($effective_rate); return $currency; }