73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Application\Reports;
|
|
|
|
use App\Application\Reports\DTO\GenerateReportInput;
|
|
use App\Application\Reports\DTO\ReportComparisonResult;
|
|
use App\Domain\Reports\Contracts\ReportRepository;
|
|
use App\Infrastructure\Reports\Adapters\LegacyReportServiceAdapter;
|
|
use RuntimeException;
|
|
|
|
final readonly class CompareLegacyAndNewReportUseCase
|
|
{
|
|
public function __construct(
|
|
private ReportRepository $reportRepository,
|
|
private LegacyReportServiceAdapter $legacyAdapter,
|
|
) {}
|
|
|
|
public function handle(GenerateReportInput $input): ReportComparisonResult
|
|
{
|
|
$startedAt = microtime(true);
|
|
$expected = $this->legacyAdapter->buildSnapshotFromInput($input);
|
|
|
|
if ($input->persistedReportId === null) {
|
|
throw new RuntimeException('persistedReportId is required for report comparison.');
|
|
}
|
|
|
|
$actual = $this->reportRepository->findSnapshot($input->persistedReportId);
|
|
|
|
if ($actual === null) {
|
|
throw new RuntimeException('Saved report snapshot was not found for comparison.');
|
|
}
|
|
|
|
$diff = $this->buildDiff($expected->toComparableArray(), $actual->toComparableArray());
|
|
$status = empty($diff) ? 'matched' : 'diff';
|
|
|
|
return new ReportComparisonResult(
|
|
reportType: $input->reportType,
|
|
path: 'new',
|
|
status: $status,
|
|
diff: $diff,
|
|
departmentId: $input->departmentId,
|
|
userId: $input->userId,
|
|
periodStart: $input->periodStart->format('Y-m-d H:i:s'),
|
|
periodEnd: $input->periodEnd->format('Y-m-d H:i:s'),
|
|
reportId: $input->persistedReportId,
|
|
durationMs: round((microtime(true) - $startedAt) * 1000, 2),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $expected
|
|
* @param array<string, mixed> $actual
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function buildDiff(array $expected, array $actual): array
|
|
{
|
|
$diff = [];
|
|
|
|
foreach ($expected as $key => $value) {
|
|
$actualValue = $actual[$key] ?? null;
|
|
|
|
if ($actualValue !== $value) {
|
|
$diff[$key] = [
|
|
'expected' => $value,
|
|
'actual' => $actualValue,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $diff;
|
|
}
|
|
}
|