Skip to content

Commit

Permalink
Merge pull request #307 from moneyphp/swap
Browse files Browse the repository at this point in the history
Update Swap
  • Loading branch information
sagikazarmark committed Oct 21, 2016
2 parents 2f918f1 + 3985d80 commit 5e10cc2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
"ext-bcmath": "*",
"ext-gmp": "*",
"ext-intl": "*",
"florianv/swap": "^2.3",
"florianv/swap": "^3.0",
"psr/cache": "^1.0",
"cache/taggable-cache": "^0.4.0",
"phpspec/phpspec": "^2.5",
"henrikbjorn/phpspec-code-coverage": "^2.0.2",
"moneyphp/iso-currencies": "~1.0.0",
"sllh/php-cs-fixer-styleci-bridge": "^2.1"
"sllh/php-cs-fixer-styleci-bridge": "^2.1",
"php-http/message": "^1.4",
"php-http/mock-client": "^0.3.3"
},
"suggest": {
"ext-bcmath": "Calculate without integer limits",
Expand Down
25 changes: 13 additions & 12 deletions spec/Exchange/SwapExchangeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace spec\Money\Exchange;

use Exchanger\Contract\ExchangeRate;
use Exchanger\Exception\Exception;
use Money\Currency;
use Money\CurrencyPair;
use Money\Exception\UnresolvableCurrencyPairException;
use Money\Exchange\SwapExchange;
use Swap\Exception\Exception;
use Swap\Model\CurrencyPair;
use Swap\Model\Rate;
use Swap\SwapInterface;
use Swap\Swap;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

final class SwapExchangeSpec extends ObjectBehavior
{
function let(SwapInterface $swap)
function let(Swap $swap)
{
$this->beConstructedWith($swap);
}
Expand All @@ -24,23 +23,25 @@ function it_is_initializable()
$this->shouldHaveType(SwapExchange::class);
}

function it_exchanges_currencies(SwapInterface $swap)
function it_exchanges_currencies(Swap $swap, ExchangeRate $exchangeRate)
{
$swap->quote(Argument::type(CurrencyPair::class))->willReturn(new Rate(1.0));
$exchangeRate->getValue()->willReturn(1.0);

$swap->latest('EUR/USD')->willReturn($exchangeRate);

$currencyPair = $this->quote($base = new Currency('EUR'), $counter = new Currency('USD'));

$currencyPair->shouldHaveType(\Money\CurrencyPair::class);
$currencyPair->shouldHaveType(CurrencyPair::class);
$currencyPair->getBaseCurrency()->shouldReturn($base);
$currencyPair->getCounterCurrency()->shouldReturn($counter);
$currencyPair->getConversionRatio()->shouldReturn(1.0);
}

function it_cannot_exchange_currencies(SwapInterface $swap)
function it_throws_an_exception_when_cannot_exchange_currencies(Swap $swap)
{
$swap->quote(Argument::type(CurrencyPair::class))->willThrow(Exception::class);
$swap->latest('EUR/XYZ')->willThrow(Exception::class);

$this->shouldThrow(UnresolvableCurrencyPairException::class)
->duringQuote(new Currency('EUR'), new Currency('USD'));
->duringQuote(new Currency('EUR'), new Currency('XYZ'));
}
}
17 changes: 7 additions & 10 deletions src/Exchange/SwapExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Money\Exchange;

use Exchanger\Exception\Exception as ExchangerException;
use Money\Currency;
use Money\CurrencyPair;
use Money\Exception\UnresolvableCurrencyPairException;
use Money\Exchange;
use Swap\Exception\Exception as SwapException;
use Swap\Model\CurrencyPair as SwapCurrencyPair;
use Swap\SwapInterface;
use Swap\Swap;

/**
* Provides a way to get exchange rate from a third-party source and return a currency pair.
Expand All @@ -18,14 +17,14 @@
final class SwapExchange implements Exchange
{
/**
* @var SwapInterface
* @var Swap
*/
private $swap;

/**
* @param SwapInterface $swap
* @param Swap $swap
*/
public function __construct(SwapInterface $swap)
public function __construct(Swap $swap)
{
$this->swap = $swap;
}
Expand All @@ -35,11 +34,9 @@ public function __construct(SwapInterface $swap)
*/
public function quote(Currency $baseCurrency, Currency $counterCurrency)
{
$swapCurrencyPair = new SwapCurrencyPair($baseCurrency->getCode(), $counterCurrency->getCode());

try {
$rate = $this->swap->quote($swapCurrencyPair);
} catch (SwapException $e) {
$rate = $this->swap->latest($baseCurrency->getCode().'/'.$counterCurrency->getCode());
} catch (ExchangerException $e) {
throw UnresolvableCurrencyPairException::createFromCurrencies($baseCurrency, $counterCurrency);
}

Expand Down

0 comments on commit 5e10cc2

Please sign in to comment.