19
19
use Symfony \Component \Console \Output \ConsoleOutputInterface ;
20
20
use Symfony \Component \Console \Output \OutputInterface ;
21
21
use Symfony \Component \Console \Style \SymfonyStyle ;
22
+ use Symfony \Component \Messenger \Envelope ;
22
23
use Symfony \Component \Messenger \Transport \Receiver \ListableReceiverInterface ;
23
24
use Symfony \Component \Messenger \Transport \Receiver \ReceiverInterface ;
24
25
@@ -32,7 +33,8 @@ protected function configure(): void
32
33
{
33
34
$ this
34
35
->setDefinition ([
35
- new InputArgument ('id ' , InputArgument::REQUIRED | InputArgument::IS_ARRAY , 'Specific message id(s) to remove ' ),
36
+ new InputArgument ('id ' , InputArgument::OPTIONAL | InputArgument::IS_ARRAY , 'Specific message id(s) to remove ' ),
37
+ new InputOption ('all ' , null , InputOption::VALUE_NONE , 'Remove all failed messages from the transport ' ),
36
38
new InputOption ('force ' , null , InputOption::VALUE_NONE , 'Force the operation without confirmation ' ),
37
39
new InputOption ('transport ' , null , InputOption::VALUE_OPTIONAL , 'Use a specific failure transport ' , self ::DEFAULT_TRANSPORT_OPTION ),
38
40
new InputOption ('show-messages ' , null , InputOption::VALUE_NONE , 'Display messages before removing it (if multiple ids are given) ' ),
@@ -43,6 +45,10 @@ protected function configure(): void
43
45
<info>php %command.full_name% {id1} [{id2} ...]</info>
44
46
45
47
The specific ids can be found via the messenger:failed:show command.
48
+
49
+ It is possible to remove all messages from the failure transport by using the "--all" option.
50
+
51
+ <info>php %command.full_name% --all --force</info>
46
52
EOF
47
53
)
48
54
;
@@ -60,19 +66,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int
60
66
$ receiver = $ this ->getReceiver ($ failureTransportName );
61
67
62
68
$ shouldForce = $ input ->getOption ('force ' );
69
+
63
70
$ ids = (array ) $ input ->getArgument ('id ' );
64
- $ shouldDisplayMessages = $ input ->getOption ('show-messages ' ) || 1 === \count ($ ids );
65
- $ this ->removeMessages ($ failureTransportName , $ ids , $ receiver , $ io , $ shouldForce , $ shouldDisplayMessages );
71
+ $ shouldDeleteAllMessages = $ input ->getOption ('all ' );
66
72
67
- return 0 ;
68
- }
73
+ $ idsCount = \count ($ ids );
74
+ if (false === $ shouldDeleteAllMessages && 0 === $ idsCount ) {
75
+ throw new RuntimeException ('Message ids must be specified. If you want to remove all messages, use the "--all" option. ' );
76
+ } elseif (true === $ shouldDeleteAllMessages && 0 !== $ idsCount ) {
77
+ throw new RuntimeException ('You cannot specify message ids when using the "--all" option. ' );
78
+ }
79
+
80
+ if ($ shouldDeleteAllMessages && !$ shouldForce ) {
81
+ throw new RuntimeException ('You must use the "--force" option when using "--all" to confirm the removal of all failed messages in the transport. ' );
82
+ }
83
+
84
+ $ shouldDisplayMessages = $ input ->getOption ('show-messages ' ) || 1 === $ idsCount ;
69
85
70
- private function removeMessages (string $ failureTransportName , array $ ids , ReceiverInterface $ receiver , SymfonyStyle $ io , bool $ shouldForce , bool $ shouldDisplayMessages ): void
71
- {
72
86
if (!$ receiver instanceof ListableReceiverInterface) {
73
87
throw new RuntimeException (sprintf ('The "%s" receiver does not support removing specific messages. ' , $ failureTransportName ));
74
88
}
75
89
90
+ if ($ shouldDeleteAllMessages ) {
91
+ $ this ->removeEnvelopes ($ receiver ->all (), $ receiver , $ io , $ shouldDisplayMessages );
92
+ } else {
93
+ $ this ->removeMessagesById ($ ids , $ receiver , $ io , $ shouldForce , $ shouldDisplayMessages );
94
+ }
95
+
96
+ return 0 ;
97
+ }
98
+
99
+ private function removeMessagesById (array $ ids , ListableReceiverInterface $ receiver , SymfonyStyle $ io , bool $ shouldForce , bool $ shouldDisplayMessages ): void
100
+ {
76
101
foreach ($ ids as $ id ) {
77
102
$ this ->phpSerializer ?->acceptPhpIncompleteClass();
78
103
try {
@@ -99,4 +124,22 @@ private function removeMessages(string $failureTransportName, array $ids, Receiv
99
124
}
100
125
}
101
126
}
127
+
128
+ /**
129
+ * @param iterable<array-key, Envelope> $envelopes
130
+ */
131
+ private function removeEnvelopes (iterable $ envelopes , ReceiverInterface $ receiver , SymfonyStyle $ io , bool $ shouldDisplayMessages ): void
132
+ {
133
+ $ count = 0 ;
134
+ foreach ($ envelopes as $ envelope ) {
135
+ if ($ shouldDisplayMessages ) {
136
+ $ this ->displaySingleMessage ($ envelope , $ io );
137
+ }
138
+
139
+ $ receiver ->reject ($ envelope );
140
+ ++$ count ;
141
+ }
142
+
143
+ $ io ->note (sprintf ('%d messages were removed. ' , $ count ));
144
+ }
102
145
}
0 commit comments