fix: add load step for other provider dependent exchange operations
This commit is contained in:
@@ -19,12 +19,20 @@ abstract class AbstractExchange implements ExchangeInterface
|
||||
protected CurrencyConverter $converter;
|
||||
protected ExchangeRateProvider $provider;
|
||||
|
||||
/**
|
||||
* Here is where your implementation should pull the data from its sources.
|
||||
* The methods `convert`, `getConversionRate`, and `getConversionDate` will call this method before making any operation.
|
||||
*/
|
||||
abstract protected function load(): void;
|
||||
|
||||
public function convert(
|
||||
MoneyInterface $from,
|
||||
string $toCurrency,
|
||||
?Context $context = null,
|
||||
RoundingMode $roundingMode = RoundingMode::UP,
|
||||
): MoneyInterface {
|
||||
$this->load();
|
||||
|
||||
$converted = $this->converter->convert(
|
||||
MoneyService::toBrick($from),
|
||||
$toCurrency,
|
||||
@@ -48,11 +56,15 @@ abstract class AbstractExchange implements ExchangeInterface
|
||||
|
||||
public function getConversionRate(string $fromCurrency, string $toCurrency): float
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return $this->provider->getExchangeRate($fromCurrency, $toCurrency)->toFloat();
|
||||
}
|
||||
|
||||
public function getConversionDate(string $fromCurrency, string $toCurrency): string
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return $this->date;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
|
||||
namespace App\Money\Conversion\Exchange;
|
||||
|
||||
use App\Money\MoneyInterface;
|
||||
use Brick\Math\RoundingMode;
|
||||
use Brick\Money\Context;
|
||||
use Brick\Money\CurrencyConverter;
|
||||
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
|
||||
use Brick\Money\ExchangeRateProvider\ConfigurableProvider;
|
||||
@@ -48,7 +45,7 @@ class EuropeanCentralBankExchange extends AbstractExchange
|
||||
return self::WEIGHT;
|
||||
}
|
||||
|
||||
public function convert(MoneyInterface $from, string $toCurrency, ?Context $context = null, RoundingMode $roundingMode = RoundingMode::UP): MoneyInterface
|
||||
protected function load(): void
|
||||
{
|
||||
$data = $this->getData();
|
||||
|
||||
@@ -61,8 +58,6 @@ class EuropeanCentralBankExchange extends AbstractExchange
|
||||
$this->date = $data['@attributes']['time'];
|
||||
$this->provider = new BaseCurrencyProvider($provider, self::ISO_4217);
|
||||
$this->converter = new CurrencyConverter($this->provider);
|
||||
|
||||
return parent::convert($from, $toCurrency, $context, $roundingMode);
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
|
||||
namespace App\Money\Conversion\Exchange;
|
||||
|
||||
use App\Money\MoneyInterface;
|
||||
use Brick\Math\RoundingMode;
|
||||
use Brick\Money\Context;
|
||||
use Brick\Money\CurrencyConverter;
|
||||
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
|
||||
use Brick\Money\ExchangeRateProvider\ConfigurableProvider;
|
||||
@@ -38,7 +35,7 @@ class FrankfurterExchange extends AbstractExchange
|
||||
private CacheInterface $cache,
|
||||
) {}
|
||||
|
||||
public function convert(MoneyInterface $from, string $toCurrency, ?Context $context = null, RoundingMode $roundingMode = RoundingMode::UP): MoneyInterface
|
||||
protected function load(): void
|
||||
{
|
||||
$data = $this->cache->get(
|
||||
self::NAME,
|
||||
@@ -57,8 +54,6 @@ class FrankfurterExchange extends AbstractExchange
|
||||
$this->date = $data['date'];
|
||||
$this->provider = new BaseCurrencyProvider($provider, self::ISO_4217);
|
||||
$this->converter = new CurrencyConverter($this->provider);
|
||||
|
||||
return parent::convert($from, $toCurrency, $context, $roundingMode);
|
||||
}
|
||||
|
||||
private function getDataLatest(): array
|
||||
|
||||
Reference in New Issue
Block a user