135 lines
4.4 KiB
PHP
135 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\BusProNet\DataProvider\HotelDataProvider;
|
|
use App\Repository\DestinationRepository;
|
|
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:destination:update-cost-units',
|
|
description: 'One-time command to update cost units for destinations from their assigned hotels',
|
|
)]
|
|
class UpdateDestinationCostUnitsCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private readonly DestinationRepository $destinationRepository,
|
|
private readonly HotelDataProvider $hotelDataProvider,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this->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;
|
|
}
|
|
}
|