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

87 lines
3.1 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
use Shlinkio\Shlink\CLI\Input\ShortUrlIdentifierInput;
2023-05-15 09:43:05 +02:00
use Shlinkio\Shlink\CLI\Util\ExitCode;
2018-09-15 17:57:12 +02:00
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
2018-09-15 17:57:12 +02:00
use Symfony\Component\Console\Command\Command;
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
private readonly ShortUrlIdentifierInput $shortUrlIdentifierInput;
public function __construct(private readonly DeleteShortUrlServiceInterface $deleteShortUrlService)
2018-09-15 17:57:12 +02:00
{
parent::__construct();
$this->shortUrlIdentifierInput = new ShortUrlIdentifierInput(
$this,
shortCodeDesc: 'The short code for the short URL to be deleted',
domainDesc: 'The domain if the short code does not belong to the default one',
);
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')
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 '
2020-01-01 20:48:31 +01:00
. 'accidentally deleted',
2018-09-15 17:57:12 +02:00
);
}
2024-02-17 10:46:29 +01:00
protected function execute(InputInterface $input, OutputInterface $output): int
2018-09-15 17:57:12 +02:00
{
$io = new SymfonyStyle($input, $output);
$identifier = $this->shortUrlIdentifierInput->toShortUrlIdentifier($input);
2018-09-15 17:57:12 +02:00
$ignoreThreshold = $input->getOption('ignore-threshold');
try {
$this->runDelete($io, $identifier, $ignoreThreshold);
2023-05-15 09:43:05 +02:00
return ExitCode::EXIT_SUCCESS;
} catch (Exception\ShortUrlNotFoundException $e) {
$io->error($e->getMessage());
2023-05-15 09:43:05 +02:00
return ExitCode::EXIT_FAILURE;
2018-09-15 17:57:12 +02:00
} catch (Exception\DeleteShortUrlException $e) {
return $this->retry($io, $identifier, $e->getMessage());
2018-09-15 17:57:12 +02:00
}
}
private function retry(SymfonyStyle $io, ShortUrlIdentifier $identifier, 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, $identifier, true);
2018-09-15 17:57:12 +02:00
} else {
2018-11-18 16:02:52 +01:00
$io->warning('Short URL was not deleted.');
2018-09-15 17:57:12 +02:00
}
2023-05-15 09:43:05 +02:00
return $forceDelete ? ExitCode::EXIT_SUCCESS : ExitCode::EXIT_WARNING;
2018-09-15 17:57:12 +02:00
}
private function runDelete(SymfonyStyle $io, ShortUrlIdentifier $identifier, bool $ignoreThreshold): void
2018-09-15 17:57:12 +02:00
{
$this->deleteShortUrlService->deleteByShortCode($identifier, $ignoreThreshold);
$io->success(sprintf('Short URL with short code "%s" successfully deleted.', $identifier->shortCode));
2018-09-15 17:57:12 +02:00
}
}