addOption( 'dry-run', null, InputOption::VALUE_NONE, 'Run without persisting changes to the database' ); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $isDryRun = true === $input->getOption('dry-run'); if ($isDryRun) { $io->note('Running in dry-run mode. No changes will be persisted.'); } $qb = $this->destinationRepository->createQueryBuilder('d'); $destinations = $qb ->where($qb->expr()->isNotNull('d.hotelBusProId')) ->andWhere($qb->expr()->orX( $qb->expr()->isNull('d.costUnit'), $qb->expr()->eq('d.costUnit', ':emptyString') )) ->andWhere($qb->expr()->isNull('d.deletedAt')) ->setParameter('emptyString', '') ->getQuery() ->getResult() ; $totalCount = count($destinations); if (0 === $totalCount) { $io->success('No destinations found that need cost unit updates.'); return Command::SUCCESS; } $io->info(sprintf('Found %d destination(s) with assigned hotels and empty cost units.', $totalCount)); $updatedCount = 0; $skippedCount = 0; foreach ($destinations as $destination) { $hotelBusProId = $destination->getHotelBusProId(); $hotel = $this->hotelDataProvider->get($hotelBusProId); if (null === $hotel) { $io->warning(sprintf( 'Hotel with BusProId %d not found for destination #%d', $hotelBusProId, $destination->getId() )); ++$skippedCount; continue; } $costUnit = $hotel->getCostUnit(); if (null === $costUnit || '' === $costUnit) { $io->warning(sprintf( 'Hotel "%s" (BusProId: %d) has no cost unit. Skipping destination #%d', $hotel->getName(), $hotelBusProId, $destination->getId() )); ++$skippedCount; continue; } $destination->setCostUnit($costUnit); ++$updatedCount; $io->writeln(sprintf( 'Updated destination #%d (%s) with cost unit "%s" from hotel "%s"', $destination->getId(), $destination->getProduct(), $costUnit, $hotel->getName() )); } if (false === $isDryRun && $updatedCount > 0) { $this->entityManager->flush(); $io->success(sprintf( 'Successfully updated %d destination(s). Skipped %d destination(s).', $updatedCount, $skippedCount )); } elseif ($isDryRun && $updatedCount > 0) { $io->success(sprintf( 'Dry-run complete. Would update %d destination(s). Would skip %d destination(s).', $updatedCount, $skippedCount )); } else { $io->info(sprintf('No destinations updated. Skipped %d destination(s).', $skippedCount)); } return Command::SUCCESS; } }