ExchangerExchange.php
4 years ago
FixedExchange.php
4 years ago
IndirectExchange.php
4 years ago
ReversedCurrenciesExchange.php
4 years ago
SwapExchange.php
4 years ago
SwapExchange.php
46 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Money\Exchange; |
| 4 | |
| 5 | use Exchanger\Exception\Exception as ExchangerException; |
| 6 | use Money\Currency; |
| 7 | use Money\CurrencyPair; |
| 8 | use Money\Exception\UnresolvableCurrencyPairException; |
| 9 | use Money\Exchange; |
| 10 | use Swap\Swap; |
| 11 | |
| 12 | /** |
| 13 | * Provides a way to get exchange rate from a third-party source and return a currency pair. |
| 14 | * |
| 15 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 16 | */ |
| 17 | final class SwapExchange implements Exchange |
| 18 | { |
| 19 | /** |
| 20 | * @var Swap |
| 21 | */ |
| 22 | private $swap; |
| 23 | |
| 24 | /** |
| 25 | * @param Swap $swap |
| 26 | */ |
| 27 | public function __construct(Swap $swap) |
| 28 | { |
| 29 | $this->swap = $swap; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * {@inheritdoc} |
| 34 | */ |
| 35 | public function quote(Currency $baseCurrency, Currency $counterCurrency) |
| 36 | { |
| 37 | try { |
| 38 | $rate = $this->swap->latest($baseCurrency->getCode().'/'.$counterCurrency->getCode()); |
| 39 | } catch (ExchangerException $e) { |
| 40 | throw UnresolvableCurrencyPairException::createFromCurrencies($baseCurrency, $counterCurrency); |
| 41 | } |
| 42 | |
| 43 | return new CurrencyPair($baseCurrency, $counterCurrency, $rate->getValue()); |
| 44 | } |
| 45 | } |
| 46 |