29
29
#[AsCommand(name: 'messenger:stats ' , description: 'Show the message count for one or more transports ' )]
30
30
class StatsCommand extends Command
31
31
{
32
- public const OUTPUT_FORMAT_TEXT = 'text ' ;
33
-
34
- public const OUTPUT_FORMAT_JSON = 'json ' ;
35
-
36
- public const ALL_OUTPUT_FORMATS = [self ::OUTPUT_FORMAT_TEXT , self ::OUTPUT_FORMAT_JSON ];
37
-
38
32
public function __construct (
39
33
private ContainerInterface $ transportLocator ,
40
34
private array $ transportNames = [],
@@ -44,10 +38,10 @@ public function __construct(
44
38
45
39
protected function configure (): void
46
40
{
47
- $ outputFormats = implode (', ' , self :: ALL_OUTPUT_FORMATS );
41
+ $ outputFormats = implode (', ' , $ this -> getAvailableFormatOptions () );
48
42
$ this
49
43
->addArgument ('transport_names ' , InputArgument::IS_ARRAY | InputArgument::OPTIONAL , 'List of transports \' names ' )
50
- ->addOption ('format ' , '' , InputOption::VALUE_REQUIRED , 'The output format, e.g.: ' .$ outputFormats , self :: OUTPUT_FORMAT_TEXT , self :: ALL_OUTPUT_FORMATS )
44
+ ->addOption ('format ' , '' , InputOption::VALUE_REQUIRED , 'The output format, e.g.: ' .$ outputFormats , ' text ' , $ this -> getAvailableFormatOptions () )
51
45
->setHelp (<<<EOF
52
46
The <info>%command.name%</info> command counts the messages for all the transports:
53
47
@@ -70,11 +64,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
70
64
{
71
65
$ io = new SymfonyStyle ($ input , $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output );
72
66
73
- $ format = match ($ input ->getOption ('format ' )) {
74
- self ::OUTPUT_FORMAT_TEXT ,
75
- self ::OUTPUT_FORMAT_JSON => $ input ->getOption ('format ' ),
76
- default => throw new InvalidArgumentException ('Invalid output format. ' ),
77
- };
67
+ $ format = $ input ->getOption ('format ' );
68
+ if (!\in_array ($ format , $ this ->getAvailableFormatOptions (), true )) {
69
+ throw new InvalidArgumentException ('Invalid output format. ' );
70
+ }
78
71
79
72
$ transportNames = $ this ->transportNames ;
80
73
if ($ input ->getArgument ('transport_names ' )) {
@@ -101,8 +94,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
101
94
}
102
95
103
96
match ($ format ) {
104
- self :: OUTPUT_FORMAT_TEXT => $ this ->outputText ($ io , $ outputTable , $ uncountableTransports ),
105
- self :: OUTPUT_FORMAT_JSON => $ this ->outputJson ($ io , $ outputTable , $ uncountableTransports ),
97
+ ' text ' => $ this ->outputText ($ io , $ outputTable , $ uncountableTransports ),
98
+ ' json ' => $ this ->outputJson ($ io , $ outputTable , $ uncountableTransports ),
106
99
};
107
100
108
101
return 0 ;
@@ -134,8 +127,14 @@ private function outputJson(SymfonyStyle $io, array $outputTable, array $uncount
134
127
private function formatSupportsWarnings (string $ format ): bool
135
128
{
136
129
return match ($ format ) {
137
- self :: OUTPUT_FORMAT_TEXT => true ,
138
- self :: OUTPUT_FORMAT_JSON => false ,
130
+ ' text ' => true ,
131
+ ' json ' => false ,
139
132
};
140
133
}
134
+
135
+ /** @return string[] */
136
+ private function getAvailableFormatOptions (): array
137
+ {
138
+ return ['text ' , 'json ' ];
139
+ }
141
140
}
0 commit comments