Skip to content

Commit 0717239

Browse files
committed
Use central banks of czechia, turkey and romania as a free provider for their currencies exchange rates
1 parent d3e3c4e commit 0717239

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

config/packages/swap.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ florianv_swap:
55

66
providers:
77
european_central_bank: ~ # European Central Bank (only works for EUR base currency)
8-
fixer: # Fixer.io (needs an API key)
9-
access_key: "%env(string:settings:exchange_rate:fixerApiKey)%"
10-
#exchange_rates_api: ~
8+
central_bank_of_czech_republic: ~
9+
central_bank_of_republic_turkey: ~
10+
national_bank_of_romania: ~
11+
12+
fixer: # Fixer.io (needs an API key)
13+
access_key: "%env(string:settings:exchange_rate:fixerApiKey)%"

src/Services/Tools/ExchangeRateUpdater.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use App\Settings\SystemSettings\LocalizationSettings;
2727
use Brick\Math\BigDecimal;
2828
use Brick\Math\RoundingMode;
29+
use Exchanger\Exception\UnsupportedCurrencyPairException;
30+
use Exchanger\Exception\UnsupportedExchangeQueryException;
2931
use Swap\Swap;
3032

3133
class ExchangeRateUpdater
@@ -39,15 +41,21 @@ public function __construct(private LocalizationSettings $localizationSettings,
3941
*/
4042
public function update(Currency $currency): Currency
4143
{
42-
//Currency pairs are always in the format "BASE/QUOTE"
43-
$rate = $this->swap->latest($this->localizationSettings->baseCurrency.'/'.$currency->getIsoCode());
44-
//The rate says how many quote units are worth one base unit
45-
//So we need to invert it to get the exchange rate
46-
47-
$rate_bd = BigDecimal::of($rate->getValue());
48-
$rate_inverted = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP);
49-
50-
$currency->setExchangeRate($rate_inverted);
44+
try {
45+
//Try it in the direction QUOTE/BASE first, as most providers provide rates in this direction
46+
$rate = $this->swap->latest($currency->getIsoCode().'/'.$this->localizationSettings->baseCurrency);
47+
$effective_rate = BigDecimal::of($rate->getValue());
48+
} catch (UnsupportedCurrencyPairException|UnsupportedExchangeQueryException $exception) {
49+
//Otherwise try to get it inverse and calculate it ourselfes, from the format "BASE/QUOTE"
50+
$rate = $this->swap->latest($this->localizationSettings->baseCurrency.'/'.$currency->getIsoCode());
51+
//The rate says how many quote units are worth one base unit
52+
//So we need to invert it to get the exchange rate
53+
54+
$rate_bd = BigDecimal::of($rate->getValue());
55+
$effective_rate = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP);
56+
}
57+
58+
$currency->setExchangeRate($effective_rate);
5159

5260
return $currency;
5361
}

0 commit comments

Comments
 (0)