osm-commerces/src/Command/UpdateCityFollowupCommand.php
2025-07-15 21:22:02 +02:00

70 lines
No EOL
2.8 KiB
PHP

<?php
namespace App\Command;
use App\Entity\Stats;
use App\Service\Motocultrice;
use App\Service\FollowUpService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:update-city-followup',
description: 'Met à jour les CityFollowUp d\'une ville à partir de son code INSEE'
)]
class UpdateCityFollowupCommand extends Command
{
public function __construct(
private EntityManagerInterface $entityManager,
private Motocultrice $motocultrice,
private FollowUpService $followUpService
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('ville', InputArgument::REQUIRED, 'Code INSEE ou nom de la ville à mettre à jour');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$inputVille = $input->getArgument('ville');
$stats = null;
if (ctype_digit($inputVille)) {
// Recherche par code INSEE
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $inputVille]);
if (!$stats) {
$io->error("Aucune stats trouvée pour le code INSEE $inputVille.");
return Command::FAILURE;
}
} else {
// Recherche par nom (insensible à la casse)
$qb = $this->entityManager->getRepository(Stats::class)->createQueryBuilder('s');
$qb->where('LOWER(s.name) = :name')
->setParameter('name', mb_strtolower($inputVille));
$results = $qb->getQuery()->getResult();
if (count($results) === 0) {
$io->error("Aucune ville trouvée avec le nom '$inputVille'.");
return Command::FAILURE;
} elseif (count($results) > 1) {
$io->error("Plusieurs villes trouvées pour le nom '$inputVille'. Veuillez préciser le code INSEE.");
foreach ($results as $stat) {
$io->text("- " . $stat->getName() . " (" . $stat->getZone() . ")");
}
return Command::FAILURE;
}
$stats = $results[0];
}
$this->followUpService->generateCityFollowUps($stats, $this->motocultrice, $this->entityManager);
$io->success("Les CityFollowUp de la ville " . $stats->getName() . " (" . $stats->getZone() . ") ont été mis à jour.");
return Command::SUCCESS;
}
}