Files
InvoiceShelf/app/Domains/Money/Application/ExchangeRateProviderService.php
T
Darko Gjorgjijoski 5ef7804e60 refactor: adopt modular domain architecture (#747)
* refactor: stabilize model identities for domain migration

* refactor: extract module platform context

* refactor: assign models to domain contexts

* refactor: extract ai platform context

* refactor: extract storage platform context

* refactor: extract mail platform context

* refactor: extract pdf platform context

* refactor: extract operations platform context

* refactor: move installation into operations platform

* refactor: extract money domain context

* refactor: extract taxation domain context

* refactor: extract catalog domain context

* refactor: extract metadata domain context

* refactor: extract reporting domain context

* refactor: extract purchases domain context

* refactor: extract receivables domain context

* refactor: extract accounts domain context

* refactor: complete reporting statement boundary

* refactor: extract contacts domain context

* refactor: extract sales domain context

* refactor: remove legacy application layers

* fix: migrate legacy bouncer role identities
2026-08-05 17:40:03 +02:00

97 lines
2.8 KiB
PHP

<?php
namespace App\Domains\Money\Application;
use App\Domains\Money\ExchangeRates\ExchangeRateDriverFactory;
use App\Domains\Money\Models\ExchangeRateProvider;
use Illuminate\Database\Eloquent\Collection;
class ExchangeRateProviderService
{
/** @param array<string, mixed> $payload */
public function create(array $payload): ExchangeRateProvider
{
return ExchangeRateProvider::create($payload);
}
/** @param array<string, mixed> $payload */
public function update(ExchangeRateProvider $provider, array $payload): ExchangeRateProvider
{
$provider->update($payload);
return $provider;
}
/**
* @param array<int, string> $currencies
* @return Collection<int, ExchangeRateProvider>
*/
public function checkActiveCurrencies(array $currencies): Collection
{
if (empty($currencies)) {
return new Collection;
}
$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();
}
/**
* @param array<int, string> $currencies
* @return Collection<int, ExchangeRateProvider>
*/
public function checkUpdateActiveCurrencies(ExchangeRateProvider $provider, array $currencies): Collection
{
if (empty($currencies)) {
return new Collection;
}
$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();
}
/** @param array<string, mixed> $configuration */
public function validateProvider(array $configuration): array
{
return ExchangeRateDriverFactory::make(
$configuration['driver'],
$configuration['key'],
$configuration['driver_config'] ?? [],
)->validateConnection();
}
public function getExchangeRate(
string $driver,
string $apiKey,
array $driverConfig,
string $baseCurrency,
string $targetCurrency,
): array {
return ExchangeRateDriverFactory::make($driver, $apiKey, $driverConfig)
->getExchangeRate($baseCurrency, $targetCurrency);
}
/** @return array<int, string> */
public function getSupportedCurrencies(string $driver, string $apiKey, array $driverConfig = []): array
{
return ExchangeRateDriverFactory::make($driver, $apiKey, $driverConfig)
->getSupportedCurrencies();
}
}