Files
shlink/module/CLI/src/Command/ShortUrl/DeleteShortUrlCommand.php

83 lines
2.9 KiB
PHP
Raw Normal View History

2018-09-15 17:57:12 +02:00
<?php
2019-10-05 17:26:10 +02:00
2018-09-15 17:57:12 +02:00
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
2018-09-15 17:57:12 +02:00
2019-02-17 10:06:27 +01:00
use Shlinkio\Shlink\CLI\Util\ExitCodes;
2018-09-15 17:57:12 +02:00
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function sprintf;
2018-09-15 17:57:12 +02:00
class DeleteShortUrlCommand extends Command
2018-09-15 17:57:12 +02:00
{
public const NAME = 'short-url:delete';
2018-09-15 17:57:12 +02:00
2019-12-29 22:27:00 +01:00
private DeleteShortUrlServiceInterface $deleteShortUrlService;
2018-09-15 17:57:12 +02:00
2018-11-18 16:02:52 +01:00
public function __construct(DeleteShortUrlServiceInterface $deleteShortUrlService)
2018-09-15 17:57:12 +02:00
{
parent::__construct();
2018-11-18 16:02:52 +01:00
$this->deleteShortUrlService = $deleteShortUrlService;
2018-09-15 17:57:12 +02:00
}
protected function configure(): void
{
$this
->setName(self::NAME)
2018-11-18 16:02:52 +01:00
->setDescription('Deletes a short URL')
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code for the short URL to be deleted')
2018-09-15 17:57:12 +02:00
->addOption(
'ignore-threshold',
'i',
InputOption::VALUE_NONE,
2018-11-18 16:02:52 +01:00
'Ignores the safety visits threshold check, which could make short URLs with many visits to be '
. 'accidentally deleted'
2018-09-15 17:57:12 +02:00
);
}
protected function execute(InputInterface $input, OutputInterface $output): ?int
2018-09-15 17:57:12 +02:00
{
$io = new SymfonyStyle($input, $output);
$shortCode = $input->getArgument('shortCode');
$ignoreThreshold = $input->getOption('ignore-threshold');
try {
$this->runDelete($io, $shortCode, $ignoreThreshold);
2019-02-17 10:06:27 +01:00
return ExitCodes::EXIT_SUCCESS;
} catch (Exception\ShortUrlNotFoundException $e) {
$io->error($e->getMessage());
2019-02-17 10:06:27 +01:00
return ExitCodes::EXIT_FAILURE;
2018-09-15 17:57:12 +02:00
} catch (Exception\DeleteShortUrlException $e) {
return $this->retry($io, $shortCode, $e->getMessage());
2018-09-15 17:57:12 +02:00
}
}
private function retry(SymfonyStyle $io, string $shortCode, string $warningMsg): int
2018-09-15 17:57:12 +02:00
{
$io->writeln(sprintf('<bg=yellow>%s</>', $warningMsg));
2018-11-18 16:02:52 +01:00
$forceDelete = $io->confirm('Do you want to delete it anyway?', false);
2018-09-15 17:57:12 +02:00
if ($forceDelete) {
$this->runDelete($io, $shortCode, true);
} else {
2018-11-18 16:02:52 +01:00
$io->warning('Short URL was not deleted.');
2018-09-15 17:57:12 +02:00
}
2019-02-17 10:06:27 +01:00
return $forceDelete ? ExitCodes::EXIT_SUCCESS : ExitCodes::EXIT_WARNING;
2018-09-15 17:57:12 +02:00
}
private function runDelete(SymfonyStyle $io, string $shortCode, bool $ignoreThreshold): void
{
$this->deleteShortUrlService->deleteByShortCode($shortCode, $ignoreThreshold);
2018-11-18 16:02:52 +01:00
$io->success(sprintf('Short URL with short code "%s" successfully deleted.', $shortCode));
2018-09-15 17:57:12 +02:00
}
}