gothick /
omm
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Command; |
||
| 4 | |||
| 5 | use App\Repository\ImageRepository; |
||
| 6 | use Doctrine\ORM\EntityManagerInterface; |
||
| 7 | use Symfony\Component\Console\Command\Command; |
||
| 8 | use Symfony\Component\Console\Helper\ProgressBar; |
||
| 9 | use Symfony\Component\Console\Input\InputArgument; |
||
| 10 | use Symfony\Component\Console\Input\InputInterface; |
||
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 12 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
| 13 | |||
| 14 | class DeleteImagesNewerThanCommand extends Command |
||
| 15 | { |
||
| 16 | protected static $defaultName = 'images:deletenewerthan'; |
||
| 17 | |||
| 18 | /** @var ImageRepository */ |
||
| 19 | private $imageRepository; |
||
| 20 | |||
| 21 | /** @var EntityManagerInterface */ |
||
| 22 | private $entityManager; |
||
| 23 | |||
| 24 | public function __construct(ImageRepository $imageRepository, EntityManagerInterface $entityManager) |
||
| 25 | { |
||
| 26 | $this->imageRepository = $imageRepository; |
||
| 27 | $this->entityManager = $entityManager; |
||
| 28 | parent::__construct(); |
||
| 29 | } |
||
| 30 | |||
| 31 | protected function configure():void |
||
| 32 | { |
||
| 33 | $this |
||
| 34 | ->setDescription('Deletes Images newer than a certain id.') |
||
| 35 | ->setHelp('Deletes specific Image entities and their associated uploaded files.') |
||
| 36 | ->addArgument('id', InputArgument::REQUIRED, 'Image ID. Images with IDs from this ID onward will be deleted.'); |
||
| 37 | } |
||
| 38 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 39 | { |
||
| 40 | $helper = $this->getHelper('question'); |
||
| 41 | $id = filter_var($input->getArgument('id'), FILTER_VALIDATE_INT, ['min_range' => 0]); |
||
| 42 | if ($id === false) { |
||
| 43 | $output->writeln('id must be an integer.'); |
||
| 44 | return Command::FAILURE; |
||
| 45 | } |
||
| 46 | |||
| 47 | $images = $this->imageRepository->findFromIdOnwards($id); |
||
| 48 | $count = count($images); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 49 | |||
| 50 | $question = new ConfirmationQuestion('Are you sure you want to delete the ' . $count . ' images from image ' . $id . ' onwards? ', false); |
||
| 51 | if (!$helper->ask($input, $output, $question)) { |
||
| 52 | $output->writeln('Aborting.'); |
||
| 53 | return Command::SUCCESS; // Well, technically I think it's not a failure. |
||
| 54 | } |
||
| 55 | $output->writeln('Deleting ' . $count . ' images'); |
||
| 56 | |||
| 57 | $progressBar = new ProgressBar($output, $count); |
||
| 58 | $progressBar->start(); |
||
| 59 | |||
| 60 | foreach ($images as $image) { |
||
| 61 | $this->entityManager->remove($image); |
||
| 62 | $progressBar->advance(); |
||
| 63 | } |
||
| 64 | $this->entityManager->flush(); |
||
| 65 | $progressBar->finish(); |
||
| 66 | |||
| 67 | return Command::SUCCESS; |
||
| 68 | } |
||
| 69 | } |