Перевод на доменную архитектуру
This commit is contained in:
72
app/Application/Reports/CompareLegacyAndNewReportUseCase.php
Normal file
72
app/Application/Reports/CompareLegacyAndNewReportUseCase.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user