forked from moneyphp/money
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exchange for retrying with reversed currencies
Closes moneyphp#323
- Loading branch information
1 parent
4e09a82
commit 2df935b
Showing
5 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ Hieatt | |
Mee | ||
USD | ||
greaterThanOrEqual | ||
|
||
behaviour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace spec\Money\Exchange; | ||
|
||
use Money\Currency; | ||
use Money\CurrencyPair; | ||
use Money\Exception\UnresolvableCurrencyPairException; | ||
use Money\Exchange; | ||
use Money\Exchange\ReversedCurrenciesExchange; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
final class ReversedCurrenciesExchangeSpec extends ObjectBehavior | ||
{ | ||
function let(Exchange $exchange) | ||
{ | ||
$this->beConstructedWith($exchange); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(ReversedCurrenciesExchange::class); | ||
} | ||
|
||
function it_is_an_exchange() | ||
{ | ||
$this->shouldImplement(Exchange::class); | ||
} | ||
|
||
function it_exchanges_currencies(Exchange $exchange) | ||
{ | ||
$baseCurrency = new Currency('EUR'); | ||
$counterCurrency = new Currency('USD'); | ||
$currencyPair = new CurrencyPair($baseCurrency, $counterCurrency, 1.25); | ||
|
||
$exchange->quote($baseCurrency, $counterCurrency)->willReturn($currencyPair); | ||
|
||
$this->quote($baseCurrency, $counterCurrency)->shouldreturn($currencyPair); | ||
} | ||
|
||
function it_exchanges_reversed_currencies_when_the_original_pair_is_not_found(Exchange $exchange) | ||
{ | ||
$baseCurrency = new Currency('USD'); | ||
$counterCurrency = new Currency('EUR'); | ||
$conversionRatio = 1.25; | ||
$currencyPair = new CurrencyPair($counterCurrency, $baseCurrency, $conversionRatio); | ||
|
||
$exchange->quote($baseCurrency, $counterCurrency)->willThrow(UnresolvableCurrencyPairException::class); | ||
$exchange->quote($counterCurrency, $baseCurrency)->willReturn($currencyPair); | ||
|
||
$currencyPair = $this->quote($baseCurrency, $counterCurrency); | ||
|
||
$currencyPair->shouldHaveType(CurrencyPair::class); | ||
$currencyPair->getBaseCurrency()->shouldReturn($baseCurrency); | ||
$currencyPair->getCounterCurrency()->shouldReturn($counterCurrency); | ||
$currencyPair->getConversionRatio()->shouldReturn(1 / $conversionRatio); | ||
} | ||
|
||
function it_throws_an_exception_when_neither_the_original_nor_the_reversed_currency_pair_can_be_resolved(Exchange $exchange) | ||
{ | ||
$baseCurrency = new Currency('USD'); | ||
$counterCurrency = new Currency('EUR'); | ||
|
||
// Exceptions are not matched based on identity, but instance and properties | ||
$exchange->quote($baseCurrency, $counterCurrency)->willThrow(UnresolvableCurrencyPairException::createFromCurrencies($baseCurrency, $counterCurrency)); | ||
$exchange->quote($counterCurrency, $baseCurrency)->willThrow(UnresolvableCurrencyPairException::createFromCurrencies($counterCurrency, $baseCurrency)); | ||
|
||
$this->shouldThrow(UnresolvableCurrencyPairException::createFromCurrencies($baseCurrency, $counterCurrency))->duringQuote($baseCurrency, $counterCurrency); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Money\Exchange; | ||
|
||
use Money\Currency; | ||
use Money\CurrencyPair; | ||
use Money\Exception\UnresolvableCurrencyPairException; | ||
use Money\Exchange; | ||
|
||
/** | ||
* Tries the reverse of the currency pair if one is not available. | ||
* | ||
* Note: adding nested ReversedCurrenciesExchange could cause a huge performance hit. | ||
* | ||
* @author Márk Sági-Kazár <[email protected]> | ||
*/ | ||
final class ReversedCurrenciesExchange implements Exchange | ||
{ | ||
/** | ||
* @var Exchange | ||
*/ | ||
private $exchange; | ||
|
||
/** | ||
* @param Exchange $exchange | ||
*/ | ||
public function __construct(Exchange $exchange) | ||
{ | ||
$this->exchange = $exchange; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function quote(Currency $baseCurrency, Currency $counterCurrency) | ||
{ | ||
try { | ||
return $this->exchange->quote($baseCurrency, $counterCurrency); | ||
} catch (UnresolvableCurrencyPairException $exception) { | ||
try { | ||
$currencyPair = $this->exchange->quote($counterCurrency, $baseCurrency); | ||
|
||
return new CurrencyPair($baseCurrency, $counterCurrency, 1 / $currencyPair->getConversionRatio()); | ||
} catch (UnresolvableCurrencyPairException $inversedException) { | ||
throw $exception; | ||
} | ||
} | ||
} | ||
} |