44
55namespace Phauthentic \CognitiveCodeAnalysis \Command ;
66
7- use Exception ;
87use Phauthentic \CognitiveCodeAnalysis \Business \MetricsFacade ;
8+ use Phauthentic \CognitiveCodeAnalysis \Command \Handler \ChurnReportHandler ;
99use Phauthentic \CognitiveCodeAnalysis \Command \Presentation \ChurnTextRenderer ;
1010use Symfony \Component \Console \Attribute \AsCommand ;
1111use Symfony \Component \Console \Command \Command ;
1818 */
1919#[AsCommand(
2020 name: 'churn ' ,
21- description: ''
21+ description: 'Calculates the churn based on version control history. ' ,
2222)]
2323class ChurnCommand extends Command
2424{
2525 private const ARGUMENT_PATH = 'path ' ;
26-
2726 public const OPTION_CONFIG_FILE = 'config ' ;
2827 public const OPTION_VCS = 'vcs ' ;
2928 public const OPTION_SINCE = 'since ' ;
@@ -36,7 +35,8 @@ class ChurnCommand extends Command
3635 */
3736 public function __construct (
3837 private MetricsFacade $ metricsFacade ,
39- private ChurnTextRenderer $ renderer
38+ private ChurnTextRenderer $ renderer ,
39+ private ChurnReportHandler $ reportHandler
4040 ) {
4141 parent ::__construct ();
4242 }
@@ -109,8 +109,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109109 since: $ input ->getOption (self ::OPTION_SINCE ),
110110 );
111111
112- if ($ this ->shouldGenerateReport ($ input )) {
113- return $ this ->generateReport ($ classes , $ input , $ output );
112+ $ reportType = $ input ->getOption (self ::OPTION_REPORT_TYPE );
113+ $ reportFile = $ input ->getOption (self ::OPTION_REPORT_FILE );
114+
115+ if ($ reportType !== null || $ reportFile !== null ) {
116+ return $ this ->reportHandler ->handle ($ classes , $ reportType , $ reportFile );
114117 }
115118
116119 $ this ->renderer ->renderChurnTable (
@@ -119,36 +122,4 @@ classes: $classes
119122
120123 return self ::SUCCESS ;
121124 }
122-
123- /**
124- * @param array<string, array<string, mixed>> $classes
125- * @param InputInterface $input
126- * @param OutputInterface $output
127- * @return int
128- */
129- private function generateReport (array $ classes , InputInterface $ input , OutputInterface $ output ): int
130- {
131- try {
132- $ this ->metricsFacade ->exportChurnReport (
133- classes: $ classes ,
134- reportType: $ input ->getOption (self ::OPTION_REPORT_TYPE ),
135- filename: $ input ->getOption (self ::OPTION_REPORT_FILE )
136- );
137-
138- return self ::SUCCESS ;
139- } catch (Exception $ exception ) {
140- $ output ->writeln (sprintf (
141- '<error>Error generating report: %s</error> ' ,
142- $ exception ->getMessage ()
143- ));
144-
145- return self ::FAILURE ;
146- }
147- }
148-
149- private function shouldGenerateReport (InputInterface $ input ): bool
150- {
151- return $ input ->getOption (self ::OPTION_REPORT_FILE )
152- && $ input ->getOption (self ::OPTION_REPORT_TYPE );
153- }
154125}
0 commit comments