mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-19 19:24:03 +00:00
Refactor exchange rate providers into driver-based architecture
Replace duplicated switch/case blocks across 4 methods with a clean abstract driver pattern: - ExchangeRateDriver (abstract): defines getExchangeRate(), getSupportedCurrencies(), validateConnection() - CurrencyFreakDriver, CurrencyLayerDriver, OpenExchangeRateDriver, CurrencyConverterDriver: concrete implementations - ExchangeRateDriverFactory: resolves driver name to class, with register() method for module extensibility Delete ExchangeRateProvidersTrait — all logic now lives in driver classes and ExchangeRateProviderService. Adding a new exchange rate provider only requires implementing ExchangeRateDriver and calling ExchangeRateDriverFactory::register() in a module service provider.
This commit is contained in:
57
app/Services/ExchangeRate/CurrencyConverterDriver.php
Normal file
57
app/Services/ExchangeRate/CurrencyConverterDriver.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExchangeRate;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class CurrencyConverterDriver extends ExchangeRateDriver
|
||||
{
|
||||
public function getExchangeRate(string $baseCurrency, string $targetCurrency): array
|
||||
{
|
||||
$baseUrl = $this->getBaseUrl();
|
||||
$query = "{$baseCurrency}_{$targetCurrency}";
|
||||
$url = "{$baseUrl}/api/v7/convert?apiKey={$this->apiKey}&q={$query}&compact=y";
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
return array_values($response[$query]);
|
||||
}
|
||||
|
||||
public function getSupportedCurrencies(): array
|
||||
{
|
||||
$baseUrl = $this->getBaseUrl();
|
||||
$url = "{$baseUrl}/api/v7/currencies?apiKey={$this->apiKey}";
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
if ($response == null) {
|
||||
throw new ExchangeRateException('Server not responding', 'server_error');
|
||||
}
|
||||
|
||||
if (array_key_exists('results', $response)) {
|
||||
return array_keys($response['results']);
|
||||
}
|
||||
|
||||
throw new ExchangeRateException('Please Enter Valid Provider Key.', 'invalid_key');
|
||||
}
|
||||
|
||||
public function validateConnection(): array
|
||||
{
|
||||
$baseUrl = $this->getBaseUrl();
|
||||
$query = 'INR_USD';
|
||||
$url = "{$baseUrl}/api/v7/convert?apiKey={$this->apiKey}&q={$query}&compact=y";
|
||||
$response = Http::get($url)->json();
|
||||
|
||||
return array_values($response[$query]);
|
||||
}
|
||||
|
||||
private function getBaseUrl(): string
|
||||
{
|
||||
$type = $this->config['type'] ?? 'FREE';
|
||||
|
||||
return match ($type) {
|
||||
'PREMIUM' => 'https://api.currconv.com',
|
||||
'PREPAID' => 'https://prepaid.currconv.com',
|
||||
'FREE' => 'https://free.currconv.com',
|
||||
'DEDICATED' => $this->config['url'] ?? 'https://free.currconv.com',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user