2023-06-11 12:14:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Currency\Services;
|
|
|
|
|
|
|
|
use Swap\Swap;
|
|
|
|
use Exchanger\Exception\Exception;
|
|
|
|
|
|
|
|
class CurrencyRateExchanger
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Instance of swap.
|
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @var Swap
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
|
|
|
private $swap;
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* Create a new CurrencyRateExchanger instance.
|
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @param Swap $swap
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
|
|
|
public function __construct(Swap $swap)
|
|
|
|
{
|
|
|
|
$this->swap = $swap;
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* Exchange to the latest currency rate.
|
|
|
|
*
|
|
|
|
* @param string $fromCurrency
|
|
|
|
* @param string $toCurrency
|
2023-12-03 14:07:47 +00:00
|
|
|
*
|
2023-06-11 12:14:03 +00:00
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function exchange($fromCurrency, $toCurrency)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->swap->latest("{$fromCurrency}/{$toCurrency}")->getValue();
|
2023-12-03 14:07:47 +00:00
|
|
|
} catch (Exception) {
|
2023-06-11 12:14:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|