src/EventSubscriber/ReportLoadedSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\ReportLoadedEvent;
  4. use App\Service\Order\OrderService;
  5. use App\Service\Report\EmailReportService;
  6. use App\Service\Report\ReportService;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ReportLoadedSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private OrderService $orderService,
  13.         private ReportService $reportService,
  14.         private EmailReportService $emailReportService,
  15.         private LoggerInterface $dbLogger
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ReportLoadedEvent::class => 'onReportLoaded',
  22.         ];
  23.     }
  24.     public function onReportLoaded(ReportLoadedEvent $event): void
  25.     {
  26.         $order $event->getReport()->getOrder();
  27.         if ($order === null) {
  28.             throw new \RuntimeException('ReportLoadedEvent has no Order');
  29.         }
  30.         $allLoaded $this->orderService->checkAreReportsLoaded($order);
  31.         if ($allLoaded) {
  32.             try {
  33.                 $reportPath $this->reportService->makeReport($order);
  34.                 $this->emailReportService->sendEmailReport($reportPath$order);
  35.             } catch (\Throwable $e) {
  36.                 $this->dbLogger->error($e->getMessage(), ['order' => $order->toLog()]);
  37.             }
  38.         }
  39.     }
  40. }