Files
2026-04-28 16:10:27 +03:00

67 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\FetchAttractionsJob;
use App\Models\City;
use App\Services\OpenTripMapService;
use App\Services\OverpassService;
use Illuminate\Console\Attributes\Argument;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('attractions:refresh {cityId}')]
#[Description('Refresh attraction data for a specific city')]
#[Argument('cityId', description: 'The ID of the city to refresh attractions for')]
class RefreshAttractions extends Command
{
/**
* Execute the console command.
*/
public function handle(
OpenTripMapService $openTripMapService,
OverpassService $overpassService
): int {
$cityId = $this->argument('cityId');
// Find the city
$city = City::find($cityId);
if (!$city) {
$this->error("City with ID {$cityId} not found.");
return Command::FAILURE;
}
$this->info("Refreshing attractions for {$city->name}, {$city->country}...");
if (!$city->latitude || !$city->longitude) {
$this->error("City coordinates not set. Cannot fetch attractions.");
return Command::FAILURE;
}
try {
// Clear cache for this city's coordinates
$this->info("Clearing cached data...");
$openTripMapService->clearCache($city->latitude, $city->longitude);
$overpassService->clearCache($city->latitude, $city->longitude);
// Delete existing attractions for this city
$deletedCount = $city->attractions()->delete();
$this->info("Deleted {$deletedCount} existing attractions.");
// Dispatch job to fetch new attractions
$this->info("Fetching fresh attraction data...");
FetchAttractionsJob::dispatch($city, true);
$this->info("Attractions refresh job dispatched successfully.");
$this->comment("The job will process in the background. Check logs for completion status.");
return Command::SUCCESS;
} catch (\Exception $e) {
$this->error("Error refreshing attractions: " . $e->getMessage());
return Command::FAILURE;
}
}
}