5521d172b6
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
78 lines
2.9 KiB
PHP
78 lines
2.9 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Services\Reports;
|
||
|
||
use App\Models\ReportJob;
|
||
use App\Services\Reports\Formatters\CsvFormatter;
|
||
use App\Services\Reports\Formatters\JsonFormatter;
|
||
use App\Services\Reports\Formatters\PdfStubFormatter;
|
||
use App\Services\Reports\Formatters\ReportFormatter;
|
||
use App\Services\Reports\Formatters\XlsxFormatter;
|
||
use App\Services\Reports\Providers\DealsExportProvider;
|
||
use App\Services\Reports\Providers\ManagersSummaryProvider;
|
||
use App\Services\Reports\Providers\ReportDataProvider;
|
||
use App\Services\Reports\Providers\SourcesSummaryProvider;
|
||
use InvalidArgumentException;
|
||
|
||
/**
|
||
* Резолвит ReportDataProvider по `type` и ReportFormatter по `format`.
|
||
*
|
||
* Этап 2 (текущий): 1 provider × 4 formatter = 4 комбинации
|
||
* (deals_export × csv|xlsx|json|pdf-stub).
|
||
*
|
||
* Этап 2b расширит до 4 × 4 = 16 (managers_summary, sources_summary,
|
||
* billing_summary). Для PDF на MVP — stub, fallback'ит в RuntimeException.
|
||
*/
|
||
class ReportGeneratorRegistry
|
||
{
|
||
public function __construct(
|
||
private readonly DealsExportProvider $dealsExport,
|
||
private readonly ManagersSummaryProvider $managersSummary,
|
||
private readonly SourcesSummaryProvider $sourcesSummary,
|
||
private readonly CsvFormatter $csv,
|
||
private readonly XlsxFormatter $xlsx,
|
||
private readonly JsonFormatter $json,
|
||
private readonly PdfStubFormatter $pdf,
|
||
) {}
|
||
|
||
public function provider(string $type): ReportDataProvider
|
||
{
|
||
return match ($type) {
|
||
'deals_export' => $this->dealsExport,
|
||
'managers_summary' => $this->managersSummary,
|
||
'sources_summary' => $this->sourcesSummary,
|
||
default => throw new InvalidArgumentException("Тип отчёта не реализован: {$type}"),
|
||
};
|
||
}
|
||
|
||
public function formatter(string $format): ReportFormatter
|
||
{
|
||
return match ($format) {
|
||
'csv' => $this->csv,
|
||
'xlsx' => $this->xlsx,
|
||
'json' => $this->json,
|
||
'pdf' => $this->pdf,
|
||
default => throw new InvalidArgumentException("Формат не поддерживается: {$format}"),
|
||
};
|
||
}
|
||
|
||
public function isSupported(string $type, string $format): bool
|
||
{
|
||
if (! in_array($type, ReportJob::TYPES, true) || ! in_array($format, ReportJob::FORMATS, true)) {
|
||
return false;
|
||
}
|
||
|
||
// Этап 2b: deals_export + managers_summary.
|
||
$supportedTypes = ['deals_export', 'managers_summary', 'sources_summary'];
|
||
if (! in_array($type, $supportedTypes, true)) {
|
||
return false;
|
||
}
|
||
|
||
// PDF — stub: validates, но генерация даёт failed-job (intended).
|
||
// Считаем «поддерживается» — пусть GenerateReportJob сам catch'ит RuntimeException.
|
||
return true;
|
||
}
|
||
}
|