|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Command\Checksum; |
| 6 | + |
| 7 | +use App\Entity\Tenant; |
| 8 | +use App\Entity\Tenant\Media; |
| 9 | +use App\Entity\Tenant\Slide; |
| 10 | +use App\Repository\TenantRepository; |
| 11 | +use App\Service\RelationsChecksumCalculator; |
| 12 | +use Doctrine\ORM\EntityManagerInterface; |
| 13 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Completion\CompletionInput; |
| 16 | +use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use Symfony\Component\Stopwatch\Stopwatch; |
| 22 | + |
| 23 | +#[AsCommand( |
| 24 | + name: 'app:checksum:recalculate', |
| 25 | + description: 'Recalculate relation checksums for slides and media entities' |
| 26 | +)] |
| 27 | +class RecalculateChecksumCommand extends Command |
| 28 | +{ |
| 29 | + private const string OPTION_TENANT = 'tenant'; |
| 30 | + private const string OPTION_MODIFIED_AFTER = 'modified-after'; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private readonly EntityManagerInterface $entityManager, |
| 34 | + private readonly TenantRepository $tenantRepository, |
| 35 | + private readonly RelationsChecksumCalculator $calculator, |
| 36 | + ) { |
| 37 | + parent::__construct(); |
| 38 | + } |
| 39 | + |
| 40 | + #[\Override] |
| 41 | + protected function configure(): void |
| 42 | + { |
| 43 | + $this |
| 44 | + ->addOption(self::OPTION_TENANT, null, InputOption::VALUE_REQUIRED, 'Filter by tenant key') |
| 45 | + ->addOption(self::OPTION_MODIFIED_AFTER, null, InputOption::VALUE_REQUIRED, 'Filter by modified_at >= date (e.g. "2024-01-01" or "2024-01-01 12:00:00")') |
| 46 | + ->setHelp(<<<'HELP' |
| 47 | +The <info>%command.name%</info> command recalculates relation checksums for slides and media, |
| 48 | +then propagates the changes up the entity tree. |
| 49 | +
|
| 50 | + <info>php %command.full_name%</info> |
| 51 | +
|
| 52 | +You can filter by tenant key and/or modification date: |
| 53 | +
|
| 54 | + <info>php %command.full_name% --tenant=ABC</info> |
| 55 | + <info>php %command.full_name% --modified-after="2024-01-01"</info> |
| 56 | + <info>php %command.full_name% --tenant=ABC --modified-after="2024-01-01 12:00:00"</info> |
| 57 | +
|
| 58 | +Without any filters, all slides and media will be recalculated. |
| 59 | +HELP) |
| 60 | + ; |
| 61 | + } |
| 62 | + |
| 63 | + #[\Override] |
| 64 | + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 65 | + { |
| 66 | + if ($input->mustSuggestOptionValuesFor(self::OPTION_TENANT)) { |
| 67 | + $tenants = $this->tenantRepository->findAll(); |
| 68 | + foreach ($tenants as $tenant) { |
| 69 | + $suggestions->suggestValue($tenant->getTenantKey()); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #[\Override] |
| 75 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 76 | + { |
| 77 | + $io = new SymfonyStyle($input, $output); |
| 78 | + $stopwatch = new Stopwatch(); |
| 79 | + $stopwatch->start('checksum-recalculate'); |
| 80 | + |
| 81 | + $tenantKey = $input->getOption(self::OPTION_TENANT); |
| 82 | + $modifiedAfterStr = $input->getOption(self::OPTION_MODIFIED_AFTER); |
| 83 | + |
| 84 | + // Resolve tenant |
| 85 | + $tenant = null; |
| 86 | + if (null !== $tenantKey) { |
| 87 | + $tenant = $this->tenantRepository->findOneBy(['tenantKey' => $tenantKey]); |
| 88 | + if (null === $tenant) { |
| 89 | + $io->error(sprintf('Tenant with key "%s" not found.', $tenantKey)); |
| 90 | + |
| 91 | + return Command::FAILURE; |
| 92 | + } |
| 93 | + $io->info(sprintf('Filtering by tenant: %s', $tenantKey)); |
| 94 | + } |
| 95 | + |
| 96 | + // Parse date |
| 97 | + $modifiedAfter = null; |
| 98 | + if (null !== $modifiedAfterStr) { |
| 99 | + try { |
| 100 | + $modifiedAfter = new \DateTimeImmutable($modifiedAfterStr); |
| 101 | + } catch (\Exception) { |
| 102 | + $io->error(sprintf('Invalid date format: "%s". Use formats like "Y-m-d" or "Y-m-d H:i:s".', $modifiedAfterStr)); |
| 103 | + |
| 104 | + return Command::FAILURE; |
| 105 | + } |
| 106 | + $io->info(sprintf('Filtering by modified after: %s', $modifiedAfter->format('Y-m-d H:i:s'))); |
| 107 | + } |
| 108 | + |
| 109 | + // Mark matching slides and media as changed using DQL UPDATE |
| 110 | + $targetEntities = [ |
| 111 | + 'slide' => Slide::class, |
| 112 | + 'media' => Media::class, |
| 113 | + ]; |
| 114 | + $totalAffected = 0; |
| 115 | + |
| 116 | + foreach ($targetEntities as $label => $entityClass) { |
| 117 | + $qb = $this->entityManager->createQueryBuilder() |
| 118 | + ->update($entityClass, 'e') |
| 119 | + ->set('e.changed', ':changed') |
| 120 | + ->setParameter('changed', true); |
| 121 | + |
| 122 | + if (null !== $tenant) { |
| 123 | + $qb->andWhere('e.tenant = :tenant') |
| 124 | + ->setParameter('tenant', $tenant, Tenant::class); |
| 125 | + } |
| 126 | + |
| 127 | + if (null !== $modifiedAfter) { |
| 128 | + $qb->andWhere('e.modifiedAt >= :modifiedAfter') |
| 129 | + ->setParameter('modifiedAfter', $modifiedAfter, 'datetime_immutable'); |
| 130 | + } |
| 131 | + |
| 132 | + $affected = $qb->getQuery()->execute(); |
| 133 | + $totalAffected += $affected; |
| 134 | + $io->info(sprintf('Marked %d rows in "%s" as changed.', $affected, $label)); |
| 135 | + } |
| 136 | + |
| 137 | + if (0 === $totalAffected) { |
| 138 | + $io->warning('No rows matched the given filters. Nothing to recalculate.'); |
| 139 | + |
| 140 | + return Command::SUCCESS; |
| 141 | + } |
| 142 | + |
| 143 | + // Propagate checksums through entity tree |
| 144 | + $io->info('Propagating checksums through entity tree...'); |
| 145 | + $this->calculator->execute(withWhereClause: true); |
| 146 | + |
| 147 | + $event = $stopwatch->stop('checksum-recalculate'); |
| 148 | + |
| 149 | + $io->success(sprintf( |
| 150 | + 'Checksums recalculated. %d rows marked. Elapsed: %.2f ms, Memory: %.2f MB', |
| 151 | + $totalAffected, |
| 152 | + $event->getDuration(), |
| 153 | + $event->getMemory() / (1024 ** 2) |
| 154 | + )); |
| 155 | + |
| 156 | + return Command::SUCCESS; |
| 157 | + } |
| 158 | +} |
0 commit comments