mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-16 22:05:20 +00:00
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.
121 lines
3.7 KiB
PHP
121 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Http\Requests\ExchangeRateProviderRequest;
|
|
use App\Models\CompanySetting;
|
|
use App\Models\ExchangeRateLog;
|
|
use App\Models\ExchangeRateProvider;
|
|
use App\Support\Integrations\ExchangeRate\ExchangeRateDriverFactory;
|
|
use App\Support\Integrations\ExchangeRate\ExchangeRateException;
|
|
|
|
class ExchangeRateProviderService
|
|
{
|
|
public function create(ExchangeRateProviderRequest $request): ExchangeRateProvider
|
|
{
|
|
return ExchangeRateProvider::create($request->getExchangeRateProviderPayload());
|
|
}
|
|
|
|
public function update(ExchangeRateProvider $provider, ExchangeRateProviderRequest $request): ExchangeRateProvider
|
|
{
|
|
$provider->update($request->getExchangeRateProviderPayload());
|
|
|
|
return $provider;
|
|
}
|
|
|
|
public function checkActiveCurrencies($request)
|
|
{
|
|
$currencies = $request->currencies;
|
|
|
|
if (empty($currencies)) {
|
|
return collect();
|
|
}
|
|
|
|
$query = ExchangeRateProvider::where('active', true);
|
|
|
|
foreach ($currencies as $currency) {
|
|
$query->orWhere(function ($q) use ($currency) {
|
|
$q->where('active', true)
|
|
->whereJsonContains('currencies', $currency);
|
|
});
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function checkUpdateActiveCurrencies(ExchangeRateProvider $provider, $request)
|
|
{
|
|
$currencies = $request->currencies;
|
|
|
|
if (empty($currencies)) {
|
|
return collect();
|
|
}
|
|
|
|
$query = ExchangeRateProvider::where('id', '<>', $provider->id)
|
|
->where('active', true);
|
|
|
|
$query->where(function ($q) use ($currencies) {
|
|
foreach ($currencies as $currency) {
|
|
$q->orWhereJsonContains('currencies', $currency);
|
|
}
|
|
});
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function checkProviderStatus($request)
|
|
{
|
|
try {
|
|
$driver = ExchangeRateDriverFactory::make(
|
|
$request['driver'],
|
|
$request['key'],
|
|
$request['driver_config'] ?? []
|
|
);
|
|
|
|
$rates = $driver->validateConnection();
|
|
|
|
return response()->json([
|
|
'exchangeRate' => $rates,
|
|
], 200);
|
|
} catch (ExchangeRateException $e) {
|
|
return respondJson($e->errorKey, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getExchangeRate(string $driver, string $apiKey, array $driverConfig, string $baseCurrency, string $targetCurrency)
|
|
{
|
|
try {
|
|
$driverInstance = ExchangeRateDriverFactory::make($driver, $apiKey, $driverConfig);
|
|
|
|
return response()->json([
|
|
'exchangeRate' => $driverInstance->getExchangeRate($baseCurrency, $targetCurrency),
|
|
], 200);
|
|
} catch (ExchangeRateException $e) {
|
|
return respondJson($e->errorKey, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getSupportedCurrencies(string $driver, string $apiKey, array $driverConfig = [])
|
|
{
|
|
try {
|
|
$driverInstance = ExchangeRateDriverFactory::make($driver, $apiKey, $driverConfig);
|
|
|
|
return response()->json([
|
|
'supportedCurrencies' => $driverInstance->getSupportedCurrencies(),
|
|
]);
|
|
} catch (ExchangeRateException $e) {
|
|
return respondJson($e->errorKey, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function addExchangeRateLog($model): ExchangeRateLog
|
|
{
|
|
return ExchangeRateLog::create([
|
|
'exchange_rate' => $model->exchange_rate,
|
|
'company_id' => $model->company_id,
|
|
'base_currency_id' => $model->currency_id,
|
|
'currency_id' => CompanySetting::getSetting('currency', $model->company_id),
|
|
]);
|
|
}
|
|
}
|