<?php
namespace App\EventSubscriber;
use App\Event\ReportLoadedEvent;
use App\Service\Order\OrderService;
use App\Service\Report\EmailReportService;
use App\Service\Report\ReportService;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ReportLoadedSubscriber implements EventSubscriberInterface
{
public function __construct(
private OrderService $orderService,
private ReportService $reportService,
private EmailReportService $emailReportService,
private LoggerInterface $dbLogger
) {
}
public static function getSubscribedEvents(): array
{
return [
ReportLoadedEvent::class => 'onReportLoaded',
];
}
public function onReportLoaded(ReportLoadedEvent $event): void
{
$order = $event->getReport()->getOrder();
if ($order === null) {
throw new \RuntimeException('ReportLoadedEvent has no Order');
}
$allLoaded = $this->orderService->checkAreReportsLoaded($order);
if ($allLoaded) {
try {
$reportPath = $this->reportService->makeReport($order);
$this->emailReportService->sendEmailReport($reportPath, $order);
} catch (\Throwable $e) {
$this->dbLogger->error($e->getMessage(), ['order' => $order->toLog()]);
}
}
}
}