2020-06-07 22:38:10 +02:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-06-07 22:38:10 +02:00
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
namespace App\Services\Tools;
|
2020-06-07 22:38:10 +02:00
|
|
|
|
|
|
|
use App\Entity\PriceInformations\Currency;
|
|
|
|
use Brick\Math\BigDecimal;
|
2022-12-11 15:33:50 +01:00
|
|
|
use Brick\Math\RoundingMode;
|
2020-06-07 22:38:10 +02:00
|
|
|
use Swap\Swap;
|
|
|
|
|
|
|
|
class ExchangeRateUpdater
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
private string $base_currency;
|
|
|
|
private Swap $swap;
|
2020-06-07 22:38:10 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-12-11 15:33:50 +01:00
|
|
|
//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);
|
2020-08-21 21:36:22 +02:00
|
|
|
|
2020-06-07 22:38:10 +02:00
|
|
|
return $currency;
|
|
|
|
}
|
2020-08-21 21:36:22 +02:00
|
|
|
}
|