mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 06:15:20 +00:00
refactor(services): split driver infrastructure out of Services into Support
Services/Integrations/ExchangeRate/ and Services/Pdf/ were both mostly Support-shaped: interfaces, abstract classes, static factories, concrete adapter drivers, DTOs, and exceptions — infrastructure that doesn't carry business logic. They only each had one real DI-injected service mixed in. This commit applies the same Services=DI-business-logic / Support=stateless-plumbing rule we've been using throughout the reorg: **Moved to Support/Integrations/ExchangeRate/** (7 files): ExchangeRateDriver (abstract), ExchangeRateDriverFactory (static), ExchangeRateException, and the four concrete drivers (CurrencyConverter, CurrencyFreak, CurrencyLayer, OpenExchangeRate). These are HTTP adapters over third-party currency APIs — same shape as the Hashids library wrapper classes already in Support. **Moved to Support/Pdf/** (6 files, merging with existing Pdf utilities): PdfDriver (interface), PdfDriverFactory (static), PdfService (static facade), GotenbergPdfDriver, GotenbergPdfResponse (DTO), ResponseStream (interface). The Support/Pdf/ dir now contains the full PDF rendering subsystem — drivers + sanitizer + template/image utilities. **Promoted to Services/ root** (the real DI services): ExchangeRateProviderService (CRUD for ExchangeRateProvider model) and FontService (font package install/download orchestration). Both are proper DI services — instance methods, model writes, HTTP side effects. Services/Integrations/ and Services/Pdf/ are now empty and deleted. Services/ holds only DI-injected classes; Support/ holds all the plumbing. 17 files renamed (git detects 90-99% similarity), 4 consumer files updated (DriverRegistryProvider, PdfServiceProvider, ExchangeRateProviderController, FontController, GeneratesPdfTrait, test). 350 tests pass, Pint clean.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Integrations\ExchangeRate;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use InvoiceShelf\Modules\Registry;
|
||||
|
||||
class ExchangeRateDriverFactory
|
||||
{
|
||||
/**
|
||||
* Built-in driver fallback map.
|
||||
*
|
||||
* Kept as a backstop so that direct calls to register() (without going through
|
||||
* the module Registry) continue to work. Built-in drivers are also registered
|
||||
* via the Registry by DriverRegistryProvider — that registration is the
|
||||
* canonical source for driver metadata (label, website, config_fields).
|
||||
*
|
||||
* @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 directly with the factory.
|
||||
*
|
||||
* Modules should prefer Registry::registerExchangeRateDriver() instead, which
|
||||
* carries metadata (label, website, config_fields) the frontend needs to render
|
||||
* a configuration form for the driver.
|
||||
*/
|
||||
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::resolveDriverClass($driver);
|
||||
|
||||
if (! $class) {
|
||||
throw new InvalidArgumentException("Unknown exchange rate driver: {$driver}");
|
||||
}
|
||||
|
||||
return new $class($apiKey, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all known driver names — both built-in/factory-registered and Registry-registered.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function availableDrivers(): array
|
||||
{
|
||||
$local = array_keys(static::$drivers);
|
||||
$registry = array_keys(Registry::allDrivers('exchange_rate'));
|
||||
|
||||
return array_values(array_unique(array_merge($local, $registry)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a driver name to its concrete class.
|
||||
*
|
||||
* Checks the local $drivers map first (built-ins and factory::register() calls),
|
||||
* then falls back to the module Registry.
|
||||
*/
|
||||
protected static function resolveDriverClass(string $driver): ?string
|
||||
{
|
||||
if (isset(static::$drivers[$driver])) {
|
||||
return static::$drivers[$driver];
|
||||
}
|
||||
|
||||
$meta = Registry::driverMeta('exchange_rate', $driver);
|
||||
|
||||
return $meta['class'] ?? null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user