-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCognitiveMetricsReportHandler.php
More file actions
89 lines (74 loc) · 2.53 KB
/
Copy pathCognitiveMetricsReportHandler.php
File metadata and controls
89 lines (74 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Phauthentic\CognitiveCodeAnalysis\Command\Handler;
use Exception;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection;
use Phauthentic\CognitiveCodeAnalysis\Business\MetricsFacade;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
class CognitiveMetricsReportHandler
{
public function __construct(
private MetricsFacade $metricsFacade,
private OutputInterface $output
) {
}
/**
* Handles report option validation and report generation.
*/
public function handle(
CognitiveMetricsCollection $metricsCollection,
?string $reportType,
?string $reportFile,
): int {
if ($this->hasIncompleteReportOptions($reportType, $reportFile)) {
$this->output->writeln('<error>Both report type and file must be provided.</error>');
return Command::FAILURE;
}
if (!$this->isValidReportType($reportType)) {
return $this->handleInvalidReporType($reportType);
}
try {
$this->metricsFacade->exportMetricsReport(
metricsCollection: $metricsCollection,
reportType: (string)$reportType,
filename: (string)$reportFile
);
return Command::SUCCESS;
} catch (Exception $exception) {
return $this->handleExceptions($exception);
}
}
private function hasIncompleteReportOptions(?string $reportType, ?string $reportFile): bool
{
return ($reportType === null && $reportFile !== null) || ($reportType !== null && $reportFile === null);
}
private function isValidReportType(?string $reportType): bool
{
return in_array($reportType, ['json', 'csv', 'html', 'markdown']);
}
/**
* @param Exception $exception
* @return int
*/
private function handleExceptions(Exception $exception): int
{
$this->output->writeln(sprintf(
'<error>Error generating report: %s</error>',
$exception->getMessage()
));
return Command::FAILURE;
}
/**
* @param string|null $reportType
* @return int
*/
public function handleInvalidReporType(?string $reportType): int
{
$this->output->writeln(sprintf(
'<error>Invalid report type `%s` provided. Only `json`, `csv`, `html`, and `markdown` are accepted.</error>',
$reportType
));
return Command::FAILURE;
}
}