osm-commerces/src/Command/LinkDemandesPlacesOsmCommand.php
2025-07-16 23:01:13 +02:00

116 lines
No EOL
3.9 KiB
PHP

<?php
namespace App\Command;
use App\Entity\Demande;
use App\Entity\Place;
use App\Repository\DemandeRepository;
use App\Repository\PlaceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:link-demandes-places-osm',
description: 'Link Demandes to Places based on matching OSM type and ID',
)]
class LinkDemandesPlacesOsmCommand extends Command
{
private EntityManagerInterface $entityManager;
private DemandeRepository $demandeRepository;
private PlaceRepository $placeRepository;
public function __construct(
EntityManagerInterface $entityManager,
DemandeRepository $demandeRepository,
PlaceRepository $placeRepository
) {
parent::__construct();
$this->entityManager = $entityManager;
$this->demandeRepository = $demandeRepository;
$this->placeRepository = $placeRepository;
}
protected function configure(): void
{
$this
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show matches without linking');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Linking Demandes to Places based on matching OSM type and ID');
$dryRun = $input->getOption('dry-run');
// Find all Demandes without a UUID but with OSM type and ID
$demandesWithoutUuid = $this->demandeRepository->createQueryBuilder('d')
->where('d.placeUuid IS NULL')
->andWhere('d.osmObjectType IS NOT NULL')
->andWhere('d.osmId IS NOT NULL')
->getQuery()
->getResult();
if (empty($demandesWithoutUuid)) {
$io->warning('No Demandes without UUID but with OSM type and ID found.');
return Command::SUCCESS;
}
$io->info(sprintf('Found %d Demandes without UUID but with OSM type and ID.', count($demandesWithoutUuid)));
// Process each Demande
$linkedCount = 0;
/** @var Demande $demande */
foreach ($demandesWithoutUuid as $demande) {
$osmType = $demande->getOsmObjectType();
$osmId = $demande->getOsmId();
// Find Place with matching OSM type and ID
$place = $this->placeRepository->findOneBy([
'osm_kind' => $osmType,
'osmId' => $osmId
]);
if ($place) {
$io->text(sprintf(
'Match found: Demande #%d -> Place #%d (OSM %s/%d)',
$demande->getId(),
$place->getId(),
$osmType,
$osmId
));
if (!$dryRun) {
$demande->setPlace($place);
$demande->setPlaceUuid($place->getUuidForUrl());
$demande->setStatus('linked_to_place');
$this->entityManager->persist($demande);
$linkedCount++;
}
} else {
$io->text(sprintf(
'No matching Place found for Demande #%d (OSM %s/%d)',
$demande->getId(),
$osmType,
$osmId
));
}
}
if (!$dryRun && $linkedCount > 0) {
$this->entityManager->flush();
$io->success(sprintf('Linked %d Demandes to Places based on OSM type and ID.', $linkedCount));
} elseif ($dryRun) {
$io->info('Dry run completed. No changes were made.');
} else {
$io->info('No Demandes were linked to Places.');
}
return Command::SUCCESS;
}
}