|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Silverback\ApiComponentsBundle\Command; |
| 4 | + |
| 5 | +use Doctrine\ORM\EntityManagerInterface; |
| 6 | +use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
| 7 | +use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup; |
| 8 | +use Silverback\ApiComponentsBundle\Helper\OrphanedResourceHelper; |
| 9 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Helper\ProgressBar; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | + |
| 15 | +#[AsCommand(name: 'silverback:api-components:clean-orphaned')] |
| 16 | +class CleanOrphanedCommand extends Command |
| 17 | +{ |
| 18 | + public function __construct(private readonly OrphanedResourceHelper $orphanedResourceHelper, private readonly EntityManagerInterface $entityManager) |
| 19 | + { |
| 20 | + parent::__construct(); |
| 21 | + } |
| 22 | + |
| 23 | + protected function configure(): void |
| 24 | + { |
| 25 | + $this |
| 26 | + ->setDescription('Checks every component and component group and will delete all orphaned components.') |
| 27 | + ->setDefinition([]); |
| 28 | + } |
| 29 | + |
| 30 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 31 | + { |
| 32 | + $count = 0; |
| 33 | + $componentGroupRepository = $this->entityManager->getRepository(ComponentGroup::class); |
| 34 | + $componentGroups = $componentGroupRepository->findAll(); |
| 35 | + $groupsProgressBar = new ProgressBar($output, count($componentGroups)); |
| 36 | + $groupsProgressBar->start(); |
| 37 | + foreach ($componentGroups as $componentGroup) { |
| 38 | + $groupsProgressBar->advance(); |
| 39 | + if ($this->orphanedResourceHelper->checkAndRemoveOrphanedComponentGroup($componentGroup)) { |
| 40 | + $count++; |
| 41 | + } |
| 42 | + } |
| 43 | + $groupsProgressBar->finish(); |
| 44 | + |
| 45 | + $componentRepository = $this->entityManager->getRepository(AbstractComponent::class); |
| 46 | + $components = $componentRepository->findAll(); |
| 47 | + $componentsProgressBar = new ProgressBar($output, count($componentGroups)); |
| 48 | + $componentsProgressBar->start(); |
| 49 | + foreach ($components as $component) { |
| 50 | + $componentsProgressBar->advance(); |
| 51 | + if ($this->orphanedResourceHelper->checkAndRemoveOrphanedComponent($component)) { |
| 52 | + $count++; |
| 53 | + } |
| 54 | + } |
| 55 | + $componentsProgressBar->finish(); |
| 56 | + |
| 57 | + $output->writeln(\sprintf('Removed <comment>%d</comment> orphaned components (excluding cascades)', $count)); |
| 58 | + return 0; |
| 59 | + } |
| 60 | +} |
0 commit comments