. */ namespace App\Services; use App\Entity\PriceInformations\Currency; use Brick\Math\BigDecimal; use Brick\Math\RoundingMode; use Swap\Swap; class ExchangeRateUpdater { private string $base_currency; private Swap $swap; public function __construct(string $base_currency, Swap $swap) { $this->base_currency = $base_currency; $this->swap = $swap; } /** * Updates the exchange rate of the given currency using the globally configured providers. */ public function update(Currency $currency): Currency { //Currency pairs are always in the format "BASE/QUOTE" $rate = $this->swap->latest($this->base_currency.'/'.$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); $currency->setExchangeRate($rate_inverted); return $currency; } }