deleteShortUrlService = $deleteShortUrlService; } protected function configure(): void { $this ->setName(self::NAME) ->setAliases(self::ALIASES) ->setDescription('Deletes a short URL') ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code for the short URL to be deleted') ->addOption( 'ignore-threshold', 'i', InputOption::VALUE_NONE, 'Ignores the safety visits threshold check, which could make short URLs with many visits to be ' . 'accidentally deleted' ); } protected function execute(InputInterface $input, OutputInterface $output): ?int { $io = new SymfonyStyle($input, $output); $shortCode = $input->getArgument('shortCode'); $ignoreThreshold = $input->getOption('ignore-threshold'); try { $this->runDelete($io, $shortCode, $ignoreThreshold); return ExitCodes::EXIT_SUCCESS; } catch (Exception\InvalidShortCodeException $e) { $io->error(sprintf('Provided short code "%s" could not be found.', $shortCode)); return ExitCodes::EXIT_FAILURE; } catch (Exception\DeleteShortUrlException $e) { return $this->retry($io, $shortCode, $e); } } private function retry(SymfonyStyle $io, string $shortCode, Exception\DeleteShortUrlException $e): int { $warningMsg = sprintf( 'It was not possible to delete the short URL with short code "%s" because it has more than %s visits.', $shortCode, $e->getVisitsThreshold() ); $io->writeln('' . $warningMsg . ''); $forceDelete = $io->confirm('Do you want to delete it anyway?', false); if ($forceDelete) { $this->runDelete($io, $shortCode, true); } else { $io->warning('Short URL was not deleted.'); } return $forceDelete ? ExitCodes::EXIT_SUCCESS : ExitCodes::EXIT_WARNING; } private function runDelete(SymfonyStyle $io, string $shortCode, bool $ignoreThreshold): void { $this->deleteShortUrlService->deleteByShortCode($shortCode, $ignoreThreshold); $io->success(sprintf('Short URL with short code "%s" successfully deleted.', $shortCode)); } }