Skip to content

Commit

Permalink
Require context in CurrencyConverter::convert()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jun 18, 2022
1 parent f883670 commit 4d81752
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Minimum PHP version is now 7.4
- `AbstractMoney::getAmount()` now has a return type
- `CurrencyConverter` constructor does not accept a default `$context` anymore
- `CurrencyConverter::convert()` now requires the `$context` previously accepted by the constructor as third parameter

**New ISO currencies**

Expand Down
21 changes: 6 additions & 15 deletions src/CurrencyConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

/**
* Converts monies into different currencies, using an exchange rate provider.
*
* @todo Now that this class provides methods to convert to both Money and RationalMoney, it makes little sense to
* provide the context in the constructor, as this only applies to convert() and not convertToRational().
* This should probably be a parameter to convert().
*/
final class CurrencyConverter
{
Expand All @@ -25,20 +21,12 @@ final class CurrencyConverter
*/
private ExchangeRateProvider $exchangeRateProvider;

/**
* The context of the monies created by this currency converter.
*/
private Context $context;

/**
* @param ExchangeRateProvider $exchangeRateProvider The exchange rate provider.
* @param Context|null $context A context to create the monies in, or null to use the default.
* The context only applies to convert(), not convertToRational().
*/
public function __construct(ExchangeRateProvider $exchangeRateProvider, ?Context $context = null)
public function __construct(ExchangeRateProvider $exchangeRateProvider)
{
$this->exchangeRateProvider = $exchangeRateProvider;
$this->context = $context ?? new DefaultContext();
}

/**
Expand All @@ -48,16 +36,19 @@ public function __construct(ExchangeRateProvider $exchangeRateProvider, ?Context
*
* @param MoneyContainer $moneyContainer The Money, RationalMoney or MoneyBag to convert.
* @param Currency|string|int $currency The Currency instance, ISO currency code or ISO numeric currency code.
* @param Context|null $context A context to create the money in, or null to use the default.
* @param int $roundingMode The rounding mode, if necessary.
*
* @return Money
*
* @throws CurrencyConversionException If the exchange rate is not available.
* @throws RoundingNecessaryException If rounding is necessary and RoundingMode::UNNECESSARY is used.
*/
public function convert(MoneyContainer $moneyContainer, $currency, int $roundingMode = RoundingMode::UNNECESSARY) : Money
public function convert(MoneyContainer $moneyContainer, $currency, ?Context $context = null, int $roundingMode = RoundingMode::UNNECESSARY) : Money
{
return $this->convertToRational($moneyContainer, $currency)->to($this->context, $roundingMode);
return $this
->convertToRational($moneyContainer, $currency)
->to($context ?? new DefaultContext(), $roundingMode);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/CurrencyConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testConvertMoney(array $money, string $toCurrency, int $rounding
$this->expectException($expectedResult);
}

$actualResult = $currencyConverter->convert($money, $toCurrency, $roundingMode);
$actualResult = $currencyConverter->convert($money, $toCurrency, null, $roundingMode);

if (! $this->isExceptionClass($expectedResult)) {
$this->assertMoneyIs($expectedResult, $actualResult);
Expand Down Expand Up @@ -97,8 +97,8 @@ public function testConvertMoneyBag(array $monies, string $currency, Context $co
$moneyBag->add($money);
}

$currencyConverter = new CurrencyConverter($exchangeRateProvider, $context);
$this->assertMoneyIs($total, $currencyConverter->convert($moneyBag, $currency, $roundingMode));
$currencyConverter = new CurrencyConverter($exchangeRateProvider);
$this->assertMoneyIs($total, $currencyConverter->convert($moneyBag, $currency, $context, $roundingMode));
}

public function providerConvertMoneyBag() : array
Expand Down Expand Up @@ -164,7 +164,7 @@ public function testConvertRationalMoney(array $money, string $toCurrency, int $
$this->expectException($expectedResult);
}

$actualResult = $currencyConverter->convert($rationalMoney, $toCurrency, $roundingMode);
$actualResult = $currencyConverter->convert($rationalMoney, $toCurrency, null, $roundingMode);

if (! $this->isExceptionClass($expectedResult)) {
$this->assertMoneyIs($expectedResult, $actualResult);
Expand Down

0 comments on commit 4d81752

Please sign in to comment.