mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +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:
43
app/Services/ExchangeRate/ExchangeRateDriverFactory.php
Normal file
43
app/Services/ExchangeRate/ExchangeRateDriverFactory.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExchangeRate;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class ExchangeRateDriverFactory
|
||||
{
|
||||
/** @var array<string, class-string<ExchangeRateDriver>> */
|
||||
protected static array $drivers = [
|
||||
'currency_freak' => CurrencyFreakDriver::class,
|
||||
'currency_layer' => CurrencyLayerDriver::class,
|
||||
'open_exchange_rate' => OpenExchangeRateDriver::class,
|
||||
'currency_converter' => CurrencyConverterDriver::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Register a custom exchange rate driver (for module extensibility).
|
||||
*/
|
||||
public static function register(string $name, string $driverClass): void
|
||||
{
|
||||
static::$drivers[$name] = $driverClass;
|
||||
}
|
||||
|
||||
public static function make(string $driver, string $apiKey, array $config = []): ExchangeRateDriver
|
||||
{
|
||||
$class = static::$drivers[$driver] ?? null;
|
||||
|
||||
if (! $class) {
|
||||
throw new InvalidArgumentException("Unknown exchange rate driver: {$driver}");
|
||||
}
|
||||
|
||||
return new $class($apiKey, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered driver names.
|
||||
*/
|
||||
public static function availableDrivers(): array
|
||||
{
|
||||
return array_keys(static::$drivers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user