entryRepository = $entryRepository; $this->downloadImages = $downloadImages; parent::__construct(); } protected function configure() { $this ->addOption( 'dry-run', null, InputOption::VALUE_NONE, 'Do not remove images, just dump counters' ); } protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $dryRun = (bool) $input->getOption('dry-run'); if ($dryRun) { $io->text('Dry run mode enabled (no images will be removed)'); } $baseFolder = $this->downloadImages->getBaseFolder(); $io->text('Retrieve existing images'); // retrieve _existing_ folders in the image folder $finder = new Finder(); $finder ->directories() ->ignoreDotFiles(true) ->depth(2) ->in($baseFolder); $existingPaths = []; foreach ($finder as $file) { $existingPaths[] = $file->getFilename(); } $io->text(sprintf(' -> %d images found', \count($existingPaths))); $io->text('Retrieve valid folders attached to a user'); $entries = $this->entryRepository->findAllEntriesIdByUserId(); // retrieve _valid_ folders from existing entries $validPaths = []; foreach ($entries as $entry) { $path = $this->downloadImages->getRelativePath($entry['id']); if (!file_exists($baseFolder . '/' . $path)) { continue; } // only store the hash, not the full path $validPaths[] = explode('/', $path)[2]; } $io->text(sprintf(' -> %d folders found', \count($validPaths))); $deletedCount = 0; $io->text('Remove images'); // check if existing path are valid, if not, remove all images and the folder foreach ($existingPaths as $existingPath) { if (!\in_array($existingPath, $validPaths, true)) { $fullPath = $baseFolder . '/' . $existingPath[0] . '/' . $existingPath[1] . '/' . $existingPath; $files = glob($fullPath . '/*.*'); if (!$dryRun) { array_map('unlink', $files); rmdir($fullPath); } $deletedCount += \count($files); $io->text(sprintf('Deleted images in %s: %d', $existingPath, \count($files))); } } $io->success(sprintf('Finished cleaning. %d deleted images', $deletedCount)); return 0; } }