FailedMessagesRemoveCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\Command;
  11. use Symfony\Component\Console\Exception\RuntimeException;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
  19. use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
  20. /**
  21. * @author Ryan Weaver <ryan@symfonycasts.com>
  22. */
  23. class FailedMessagesRemoveCommand extends AbstractFailedMessagesCommand
  24. {
  25. protected static $defaultName = 'messenger:failed:remove';
  26. protected static $defaultDescription = 'Remove given messages from the failure transport';
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function configure(): void
  31. {
  32. $this
  33. ->setDefinition([
  34. new InputArgument('id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
  35. new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
  36. new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
  37. new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
  38. ])
  39. ->setDescription(self::$defaultDescription)
  40. ->setHelp(<<<'EOF'
  41. The <info>%command.name%</info> removes given messages that are pending in the failure transport.
  42. <info>php %command.full_name% {id1} [{id2} ...]</info>
  43. The specific ids can be found via the messenger:failed:show command.
  44. EOF
  45. )
  46. ;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function execute(InputInterface $input, OutputInterface $output)
  52. {
  53. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  54. $failureTransportName = $input->getOption('transport');
  55. if (self::DEFAULT_TRANSPORT_OPTION === $failureTransportName) {
  56. $failureTransportName = $this->getGlobalFailureReceiverName();
  57. }
  58. $receiver = $this->getReceiver($failureTransportName);
  59. $shouldForce = $input->getOption('force');
  60. $ids = (array) $input->getArgument('id');
  61. $shouldDisplayMessages = $input->getOption('show-messages') || 1 === \count($ids);
  62. $this->removeMessages($failureTransportName, $ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
  63. return 0;
  64. }
  65. private function removeMessages(string $failureTransportName, array $ids, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
  66. {
  67. if (!$receiver instanceof ListableReceiverInterface) {
  68. throw new RuntimeException(sprintf('The "%s" receiver does not support removing specific messages.', $failureTransportName));
  69. }
  70. foreach ($ids as $id) {
  71. $envelope = $receiver->find($id);
  72. if (null === $envelope) {
  73. $io->error(sprintf('The message with id "%s" was not found.', $id));
  74. continue;
  75. }
  76. if ($shouldDisplayMessages) {
  77. $this->displaySingleMessage($envelope, $io);
  78. }
  79. if ($shouldForce || $io->confirm('Do you want to permanently remove this message?', false)) {
  80. $receiver->reject($envelope);
  81. $io->success(sprintf('Message with id %s removed.', $id));
  82. } else {
  83. $io->note(sprintf('Message with id %s not removed.', $id));
  84. }
  85. }
  86. }
  87. }