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:
Darko Gjorgjijoski
2026-04-11 16:30:00 +02:00
parent 4c3d809f89
commit f657b53215
21 changed files with 28 additions and 28 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Support\Integrations\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',
};
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Support\Integrations\ExchangeRate;
use Illuminate\Support\Facades\Http;
class CurrencyFreakDriver extends ExchangeRateDriver
{
private string $baseUrl = 'https://api.currencyfreaks.com';
public function getExchangeRate(string $baseCurrency, string $targetCurrency): array
{
$url = "{$this->baseUrl}/latest?apikey={$this->apiKey}&symbols={$targetCurrency}&base={$baseCurrency}";
$response = Http::get($url)->json();
if (array_key_exists('success', $response) && $response['success'] == false) {
throw new ExchangeRateException($response['error']['message'], 'provider_error');
}
return array_values($response['rates']);
}
public function getSupportedCurrencies(): array
{
$url = "{$this->baseUrl}/currency-symbols";
$response = Http::get($url)->json();
if ($response == null) {
throw new ExchangeRateException('Server not responding', 'server_error');
}
$checkKey = $this->validateConnection();
return array_keys($response);
}
public function validateConnection(): array
{
$url = "{$this->baseUrl}/latest?apikey={$this->apiKey}&symbols=INR&base=USD";
$response = Http::get($url)->json();
if ($response == null) {
throw new ExchangeRateException('Server not responding', 'server_error');
}
if (array_key_exists('success', $response) && array_key_exists('error', $response)) {
if ($response['error']['status'] == 404) {
throw new ExchangeRateException('Please Enter Valid Provider Key.', 'invalid_key');
}
}
return array_values($response['rates']);
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Support\Integrations\ExchangeRate;
use Illuminate\Support\Facades\Http;
class CurrencyLayerDriver extends ExchangeRateDriver
{
private string $baseUrl = 'http://api.currencylayer.com';
public function getExchangeRate(string $baseCurrency, string $targetCurrency): array
{
$url = "{$this->baseUrl}/live?access_key={$this->apiKey}&source={$baseCurrency}&currencies={$targetCurrency}";
$response = Http::get($url)->json();
if (array_key_exists('success', $response) && $response['success'] == false) {
throw new ExchangeRateException($response['error']['info'], 'provider_error');
}
return array_values($response['quotes']);
}
public function getSupportedCurrencies(): array
{
$url = "{$this->baseUrl}/list?access_key={$this->apiKey}";
$response = Http::get($url)->json();
if ($response == null) {
throw new ExchangeRateException('Server not responding', 'server_error');
}
if (array_key_exists('currencies', $response)) {
return array_keys($response['currencies']);
}
throw new ExchangeRateException('Please Enter Valid Provider Key.', 'invalid_key');
}
public function validateConnection(): array
{
$url = "{$this->baseUrl}/live?access_key={$this->apiKey}&source=INR&currencies=USD";
$response = Http::get($url)->json();
if (array_key_exists('success', $response) && $response['success'] == false) {
throw new ExchangeRateException($response['error']['info'], 'provider_error');
}
return array_values($response['quotes']);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Support\Integrations\ExchangeRate;
abstract class ExchangeRateDriver
{
public function __construct(
protected string $apiKey,
protected array $config = [],
) {}
/**
* Fetch exchange rate between two currencies.
*
* @return array Exchange rate values
*/
abstract public function getExchangeRate(string $baseCurrency, string $targetCurrency): array;
/**
* Get list of currencies supported by this provider.
*
* @return array Currency codes
*/
abstract public function getSupportedCurrencies(): array;
/**
* Validate that the API key and connection work.
*
* @return array Exchange rate values (proof of working connection)
*
* @throws ExchangeRateException
*/
abstract public function validateConnection(): array;
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Support\Integrations\ExchangeRate;
class ExchangeRateException extends \RuntimeException
{
public function __construct(string $message, public readonly string $errorKey = 'exchange_rate_error')
{
parent::__construct($message);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Support\Integrations\ExchangeRate;
use Illuminate\Support\Facades\Http;
class OpenExchangeRateDriver extends ExchangeRateDriver
{
private string $baseUrl = 'https://openexchangerates.org/api';
public function getExchangeRate(string $baseCurrency, string $targetCurrency): array
{
$url = "{$this->baseUrl}/latest.json?app_id={$this->apiKey}&base={$baseCurrency}&symbols={$targetCurrency}";
$response = Http::get($url)->json();
if (array_key_exists('error', $response)) {
throw new ExchangeRateException($response['description'], $response['message']);
}
return array_values($response['rates']);
}
public function getSupportedCurrencies(): array
{
$url = "{$this->baseUrl}/currencies.json";
$response = Http::get($url)->json();
if ($response == null) {
throw new ExchangeRateException('Server not responding', 'server_error');
}
$checkKey = $this->validateConnection();
return array_keys($response);
}
public function validateConnection(): array
{
$url = "{$this->baseUrl}/latest.json?app_id={$this->apiKey}&base=USD&symbols=EUR";
$response = Http::get($url)->json();
if (array_key_exists('error', $response)) {
if ($response['status'] == 401) {
throw new ExchangeRateException('Please Enter Valid Provider Key.', 'invalid_key');
}
throw new ExchangeRateException($response['description'], $response['message']);
}
return array_values($response['rates']);
}
}