Files
InvoiceShelf/app/Support/Integrations/ExchangeRate/CurrencyConverterDriver.php
Darko Gjorgjijoski f657b53215 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.
2026-04-11 16:30:00 +02:00

58 lines
1.8 KiB
PHP

<?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',
};
}
}