php-commerce/Console/RefreshExchangeRates.php

59 lines
1.7 KiB
PHP
Raw Normal View History

2026-01-27 00:24:22 +00:00
<?php
declare(strict_types=1);
namespace Core\Mod\Commerce\Console;
2026-01-27 00:24:22 +00:00
use Illuminate\Console\Command;
use Core\Mod\Commerce\Services\CurrencyService;
2026-01-27 00:24:22 +00:00
/**
* Refresh exchange rates from configured provider.
*
* Should be scheduled to run periodically (e.g., hourly).
*/
class RefreshExchangeRates extends Command
{
protected $signature = 'commerce:refresh-exchange-rates
{--force : Force refresh even if rates are fresh}';
protected $description = 'Refresh exchange rates from the configured provider';
public function handle(CurrencyService $currencyService): int
{
$this->info('Refreshing exchange rates...');
$baseCurrency = $currencyService->getBaseCurrency();
$provider = config('commerce.currencies.exchange_rates.provider', 'ecb');
$this->line("Base currency: {$baseCurrency}");
$this->line("Provider: {$provider}");
// Check if rates need refresh
if (! $this->option('force') && ! \Core\Mod\Commerce\Models\ExchangeRate::needsRefresh()) {
2026-01-27 00:24:22 +00:00
$this->info('Rates are still fresh. Use --force to refresh anyway.');
return self::SUCCESS;
}
$rates = $currencyService->refreshExchangeRates();
if (empty($rates)) {
$this->error('No rates were updated. Check logs for errors.');
return self::FAILURE;
}
$this->info('Updated '.count($rates).' exchange rates:');
$rows = [];
foreach ($rates as $currency => $rate) {
$rows[] = [$baseCurrency, $currency, number_format($rate, 6)];
}
$this->table(['From', 'To', 'Rate'], $rows);
return self::SUCCESS;
}
}