DeleteTagsCommand.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\CLI\Command\Tag;
  4. use Shlinkio\Shlink\CLI\Util\ExitCodes;
  5. use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. class DeleteTagsCommand extends Command
  12. {
  13. public const NAME = 'tag:delete';
  14. private TagServiceInterface $tagService;
  15. public function __construct(TagServiceInterface $tagService)
  16. {
  17. parent::__construct();
  18. $this->tagService = $tagService;
  19. }
  20. protected function configure(): void
  21. {
  22. $this
  23. ->setName(self::NAME)
  24. ->setDescription('Deletes one or more tags.')
  25. ->addOption(
  26. 'name',
  27. 't',
  28. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  29. 'The name of the tags to delete',
  30. );
  31. }
  32. protected function execute(InputInterface $input, OutputInterface $output): ?int
  33. {
  34. $io = new SymfonyStyle($input, $output);
  35. $tagNames = $input->getOption('name');
  36. if (empty($tagNames)) {
  37. $io->warning('You have to provide at least one tag name');
  38. return ExitCodes::EXIT_WARNING;
  39. }
  40. $this->tagService->deleteTags($tagNames);
  41. $io->success('Tags properly deleted');
  42. return ExitCodes::EXIT_SUCCESS;
  43. }
  44. }