Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

igarakatidus/exchange-rate

0 stars
4 code files
View igarakatidus/exchange-rate on GitHub

app/Services/ExchangeRateHost.php

Open in GitHub
use App\Exceptions\ExchangeRateException;
use App\Services\Contract\ExchangeRate as ExchangeRateContract;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
 
class ExchangeRateHost implements ExchangeRateContract
{
private const API_URL = 'https://api.exchangerate.host';
 
public function convert(string $from, string $to): float
{
$cacheKey = sprintf('%s-%s-%s', __CLASS__, $from, $to);
$ttl = (int) config('services.conversion.ttl', 3600);
return Cache::remember($cacheKey, $ttl, function () use ($from, $to) {
$uri = sprintf('/convert?from=%s&to=%s', $from, $to);
$response = Http::get(self::API_URL . $uri);
$array = $response->json();
if(!$array['success'] ?? false){
Log::error("Error in API Response of Exchange Rate Conversion", [
'class' => __CLASS__,
'from' => $from,
'to' => $to,
'response' => $response->body(),
'status' => $response->status(),
]);
throw new ExchangeRateException("Could not convert from $from to $to");
}
 
return (float) $array['result'];
});
}
 
public function getAllowedCurrencies(): array
{
$cacheKey = sprintf('%s-%s', __CLASS__, 'symbols');
$ttl = (int) config('services.symbols.ttl', 3600);
return Cache::remember($cacheKey, $ttl, function (){
$response = Http::get(self::API_URL . '/symbols');
$array = $response->json();
if(!$array['success'] ?? false){
Log::error("Error in API Response of fetching symbols", [
'class' => __CLASS__,
'response' => $response->body(),
'status' => $response->status(),
]);
throw new ExchangeRateException("Could not fetch symbols");
}
 
return array_keys($array['symbols']);
});
}
}

app/Services/Contract/ExchangeRate.php

Open in GitHub
interface ExchangeRate
{
/**
* Get the Exchange Rate
*/
public function convert(string $from, string $to): float;
 
/**
* Get valid currencies
*/
public function getAllowedCurrencies(): array;
}

app/Providers/AppServiceProvider.php

Open in GitHub
use App\Services\Contract\ExchangeRate as ExchangeRateContract;
use App\Services\ExchangeRateHost;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
// ...
public function boot(): void
{
$this->app->bind(ExchangeRateContract::class, ExchangeRateHost::class);
}
}

app/Http/Livewire/Conversion.php

Open in GitHub
use App\Exceptions\ExchangeRateException;
use App\Services\Contract\ExchangeRate as ExchangeRateContract;
use Livewire\Component;
 
class Conversion extends Component
{
// ...
public function mount(ExchangeRateContract $service)
{
try {
$symbols = $service->getAllowedCurrencies();
$this->currencies = array_filter($symbols, fn ($symbol) => in_array($symbol, self::ALLOWED_SYMBOLS));
$this->conversions = $this->getConversions();
} catch (ExchangeRateException $e) {
session()->flash('api_error', $e->getMessage());
}
}
// ...
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.