src/EventSubscriber/AlertForecastReportEmailEventSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\C2IntegrationBundle\Service\C2Service;
  4. use App\Service\ReportModerationService;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Pimcore\Log\ApplicationLogger;
  7. use Pimcore\Model\DataObject\Report;
  8. use Symfony\Component\Templating\EngineInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class AlertForecastReportEmailEventSubscriber
  11. {
  12.     private C2Service $c2Service;
  13.     public function __construct(
  14.         private EngineInterface $twig,
  15.         private TranslatorInterface $translator,
  16.         private ApplicationLogger $logger,
  17.     ) {
  18.         $this->c2Service = new C2Service();
  19.     }
  20.     public function onAlertForecastReportPublished(DataObjectEvent $event): void
  21.     {
  22.         $report $event->getObject();
  23.         if (!$this->shouldSendEmail($report)) {
  24.             return;
  25.         }
  26.         $emails $this->collectRecipientEmails($report);
  27.         if ($emails === []) {
  28.             return;
  29.         }
  30.         $baseUrl rtrim((string) ($_ENV['PUBLIC_URL'] ?? NCM_PUBLIC_PORTAL_URL), '/');
  31.         $reportId $report->getId();
  32.         $publicReportUrlEn $baseUrl '/en/weather/weather-reports/' $reportId;
  33.         $publicReportUrlAr $baseUrl '/ar/weather/weather-reports/' $reportId;
  34.         $reportTypeName trim((string) ($report->getReportType()?->getName('ar') ?? ''));
  35.         if ($reportTypeName === '') {
  36.             $reportTypeName trim((string) ($report->getReportType()?->getName('en') ?? ''));
  37.         }
  38.         if ($reportTypeName === '') {
  39.             $reportTypeName 'تقرير تنبؤ وتوقعات';
  40.         }
  41.         $subject $this->translator->trans('alert_forecast_report_email_subject', [], 'messages''ar');
  42.         $templateId $_ENV['ALERT_FORECAST_REPORT_TEMPLATE'] ?? null;
  43.         if (empty($templateId)) {
  44.             $this->logger->error('Alert forecast report email skipped: ALERT_FORECAST_REPORT_TEMPLATE is not configured');
  45.             return;
  46.         }
  47.         foreach ($emails as $email) {
  48.             try {
  49.                 $html $this->twig->render('email/alert_forecast_report_email.html.twig', [
  50.                     'titleEn' => trim((string) $report->getReportTitle('en')),
  51.                     'titleAr' => trim((string) $report->getReportTitle('ar')),
  52.                     'reportTypeName' => $reportTypeName,
  53.                     'publicReportUrlEn' => $publicReportUrlEn,
  54.                     'publicReportUrlAr' => $publicReportUrlAr,
  55.                     'intro' => $this->translator->trans('alert_forecast_report_email_intro', [
  56.                         '%reportType%' => "تقرير الطقس التنبؤي",
  57.                     ], 'messages''ar'),
  58.                     'viewReportEn' => $this->translator->trans('alert_forecast_report_email_view_en', [], 'messages''ar'),
  59.                     'viewReportAr' => $this->translator->trans('alert_forecast_report_email_view_ar', [], 'messages''ar'),
  60.                 ]);
  61.                 $this->c2Service->sendCustomReportEmail(
  62.                     $templateId,
  63.                     $report->getId(),
  64.                     $email,
  65.                     $html,
  66.                     $subject,
  67.                     []
  68.                 );
  69.             } catch (\Throwable $e) {
  70.                 $this->logger->error('Alert forecast report email failed for ' $email ': ' $e->getMessage());
  71.             }
  72.         }
  73.     }
  74.     /**
  75.      * @return string[]
  76.      */
  77.     private function collectRecipientEmails(Report $report): array
  78.     {
  79.         $raw trim((string) $report->getEmails());
  80.         if ($raw === '') {
  81.             return [];
  82.         }
  83.         $emails = [];
  84.         foreach (explode(','$raw) as $email) {
  85.             $email trim($email);
  86.             if ($email !== '' && filter_var($emailFILTER_VALIDATE_EMAIL)) {
  87.                 $emails[] = $email;
  88.             }
  89.         }
  90.         return array_values(array_unique($emails));
  91.     }
  92.     private function shouldSendEmail(mixed $report): bool
  93.     {
  94.         return ($report instanceof Report) &&
  95.             ReportModerationService::isReportApproved($report) &&
  96.             $report->isPublished(true) &&
  97.             $report->getForEmailChannel() === true &&
  98.             $report->getReportType()?->getReportKey() === ALERT_FORECAST_REPORT_TYPE_KEY &&
  99.             trim((string) $report->getEmails()) !== '';
  100.     }
  101. }