2222use CakeDC \QueueMonitor \Exception \QueueMonitorException ;
2323use CakeDC \QueueMonitor \Service \EnqueueClientService ;
2424use Psr \Log \LogLevel ;
25+ use function Cake \I18n \__ ;
26+ use function Cake \Collection \collection ;
2527
2628/**
2729 * Purge command.
@@ -66,6 +68,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
6668 ->addArgument ('queue-config ' , [
6769 'help ' => __ ('Queue configuration key ' ),
6870 ])
71+ ->addOption ('all ' , [
72+ 'help ' => __ ('All messages will be purged ' ),
73+ 'short ' => 'a ' ,
74+ 'boolean ' => true ,
75+ 'default ' => false ,
76+ ])
6977 ->addOption ('yes ' , [
7078 'short ' => 'y ' ,
7179 'boolean ' => true ,
@@ -87,44 +95,54 @@ public function execute(Arguments $args, ConsoleIo $io)
8795
8896 return self ::CODE_SUCCESS ;
8997 }
90- $ queueConfig = $ args ->getArgument ('queue-config ' );
91-
92- if (!$ this ->validateQueueConfig ($ queueConfig )) {
93- $ io ->error (__ ('Queue configuration key is invalid ' ));
94- $ configuredQueues = $ this ->getConfiguredQueues ();
95- if ($ configuredQueues ) {
96- $ io ->error (__ ('Valid configuration keys are: {0} ' , implode (', ' , $ configuredQueues )));
97- } else {
98- $ io ->error (__ ('There are no queue configurations ' ));
99- }
100- $ this ->displayHelp ($ this ->getOptionParser (), $ args , $ io );
10198
102- return self ::CODE_ERROR ;
103- }
99+ if ($ args ->getOption ('all ' )) {
100+ $ this ->checkConfirmation (
101+ __ ('Are you sure you want to purge messages from all queues? ' ),
102+ $ args ,
103+ $ io
104+ );
104105
105- if (!$ args ->getOption ('yes ' )) {
106- $ confirmation = $ io ->askChoice (
106+ collection ($ this ->getConfiguredQueues ())->each (function (string $ queueConfig ) use ($ io ): void {
107+ try {
108+ $ this ->enqueueClientService ->purgeQueue ($ queueConfig );
109+ $ io ->success (__ ('Queue `{0}` purged successfully ' , $ queueConfig ));
110+ } catch (QueueMonitorException $ e ) {
111+ $ io ->error (__ ('Unable to purge queue `{0}`, reason: {1} ' , $ queueConfig , $ e ->getMessage ()));
112+ }
113+ });
114+ } else {
115+ $ queueConfig = $ args ->getArgument ('queue-config ' );
116+
117+ if (!$ this ->validateQueueConfig ($ queueConfig )) {
118+ $ io ->error (__ ('Queue configuration key is invalid ' ));
119+ $ configuredQueues = $ this ->getConfiguredQueues ();
120+ if ($ configuredQueues ) {
121+ $ io ->error (__ ('Valid configuration keys are: {0} ' , implode (', ' , $ configuredQueues )));
122+ } else {
123+ $ io ->error (__ ('There are no queue configurations ' ));
124+ }
125+ $ this ->displayHelp ($ this ->getOptionParser (), $ args , $ io );
126+
127+ return self ::CODE_ERROR ;
128+ }
129+
130+ $ this ->checkConfirmation (
107131 __ ('Are you sure you want to purge messages from specified queue? ' ),
108- [
109- __ ('yes ' ),
110- __ ('no ' )
111- ],
112- __ ('no ' )
132+ $ args ,
133+ $ io
113134 );
114135
115- if ($ confirmation === __ ('no ' )) {
116- $ io ->abort (__ ('Aborting ' ));
117- }
118- }
119- try {
120- $ this ->enqueueClientService ->purgeQueue ($ queueConfig );
121- $ io ->success (__ ('Queue purged successfully ' ));
136+ try {
137+ $ this ->enqueueClientService ->purgeQueue ($ queueConfig );
138+ $ io ->success (__ ('Queue `{0}` purged successfully ' , $ queueConfig ));
122139
123- return self ::CODE_SUCCESS ;
124- } catch (QueueMonitorException $ e ) {
125- $ io ->error (__ ('Unable to purge queue, reason: {0} ' , $ e ->getMessage ()));
140+ return self ::CODE_SUCCESS ;
141+ } catch (QueueMonitorException $ e ) {
142+ $ io ->error (__ ('Unable to purge queue `{0}` , reason: {1} ' , $ queueConfig , $ e ->getMessage ()));
126143
127- return self ::CODE_ERROR ;
144+ return self ::CODE_ERROR ;
145+ }
128146 }
129147 }
130148
@@ -153,4 +171,25 @@ private function getConfiguredQueues(): array
153171 {
154172 return array_keys (Configure::read ('Queue ' , []));
155173 }
174+
175+ /**
176+ * Check confirmation
177+ */
178+ private function checkConfirmation (string $ prompt , Arguments $ args , ConsoleIo $ io ): void
179+ {
180+ if (!$ args ->getOption ('yes ' )) {
181+ $ confirmation = $ io ->askChoice (
182+ $ prompt ,
183+ [
184+ __ ('yes ' ),
185+ __ ('no ' )
186+ ],
187+ __ ('no ' )
188+ );
189+
190+ if ($ confirmation === __ ('no ' )) {
191+ $ io ->abort (__ ('Aborting ' ));
192+ }
193+ }
194+ }
156195}
0 commit comments