Перевод на доменную архитектуру

This commit is contained in:
brusnitsyn
2026-04-26 23:37:50 +09:00
parent 75ca01ffd8
commit f107ebd167
70 changed files with 4656 additions and 2070 deletions

View 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;
}
}