diff --git a/src/bundle/Command/PageFieldTypeCleanupCommand.php b/src/bundle/Command/PageFieldTypeCleanupCommand.php index 825620d..6de1df4 100644 --- a/src/bundle/Command/PageFieldTypeCleanupCommand.php +++ b/src/bundle/Command/PageFieldTypeCleanupCommand.php @@ -5,15 +5,17 @@ namespace MateuszBieniek\EzPlatformDatabaseHealthCheckerBundle\Command; use MateuszBieniek\EzPlatformDatabaseHealthChecker\Persistence\Legacy\Content\Gateway\PageFieldTypeGatewayInterface as Gateway; +use Symfony\Bundle\MakerBundle\Validator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Validator\Constraints\Regex; +use Symfony\Component\Validator\Validation; class PageFieldTypeCleanupCommand extends Command { - const PAGE_LIMIT = 100; - /** @var \Symfony\Component\Console\Style\SymfonyStyle */ private $io; @@ -77,11 +79,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + $helper = $this->getHelper('question'); + + $question = new Question('Please enter the limit of pages to be iterated:', 100); + $validation = Validation::createCallable(new Regex([ + 'pattern' => '/^\d+$/', + 'message' => 'The input should be an integer', + ])); + $question->setValidator($validation); + + $limit = (int) $helper->ask($input, $output, $question); + if ($this->countOrphanedPageRelations() <= 0) { return 0; } - $this->deleteOrphanedPageRelations(); + $this->deleteOrphanedPageRelations($limit); $this->io->success('Done'); @@ -99,21 +112,21 @@ private function countOrphanedPageRelations(): int return $count; } - private function deleteOrphanedPageRelations(): void + private function deleteOrphanedPageRelations(int $limit): void { if (!$this->io->confirm( sprintf('Are you sure that you want to proceed? The maximum number of pages that will be cleaned - in first iteration is equal to %d.', self::PAGE_LIMIT), + in this iteration is equal to %d.', $limit), false) ) { return; } - $records = $this->gateway->getOrphanedPageRelations(self::PAGE_LIMIT); + $records = $this->gateway->getOrphanedPageRelations($limit); $progressBar = $this->io->createProgressBar(count($records)); - for ($i = 0; $i < self::PAGE_LIMIT; ++$i) { + for ($i = 0; $i < $limit; ++$i) { if (isset($records[$i])) { $progressBar->advance(1); $this->gateway->removePage((int) $records[$i]); diff --git a/src/lib/Persistence/Legacy/Content/Gateway/PageFieldTypeDoctrineDatabase.php b/src/lib/Persistence/Legacy/Content/Gateway/PageFieldTypeDoctrineDatabase.php index 1abf1a1..c26b025 100644 --- a/src/lib/Persistence/Legacy/Content/Gateway/PageFieldTypeDoctrineDatabase.php +++ b/src/lib/Persistence/Legacy/Content/Gateway/PageFieldTypeDoctrineDatabase.php @@ -85,5 +85,10 @@ public function removePage(int $pageId): void $this->pageFieldTypeGateway->removeZone((int) $attribute['zone_id']); } } + + foreach ($this->pageFieldTypeGateway->loadZonesAssignedToPage($pageId) as $zone) { + $this->pageFieldTypeGateway->unassignZoneFromPage((int) $zone['id'], $pageId); + $this->pageFieldTypeGateway->removeZone((int) $zone['id']); + } } }