Skip to content

Commit

Permalink
Merge pull request #6 from mateuszbieniek/provide-iterations-number-i…
Browse files Browse the repository at this point in the history
…nput

provide a command input for number of iterations, update gateway
  • Loading branch information
mateuszbieniek authored Dec 13, 2021
2 parents 12e75ad + a4ff4c8 commit 43a33a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/bundle/Command/PageFieldTypeCleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');

Expand All @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}

0 comments on commit 43a33a9

Please sign in to comment.