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.         $sent false;
  48.         foreach ($emails as $email) {
  49.             try {
  50.                 $html $this->twig->render('email/alert_forecast_report_email.html.twig', [
  51.                     'titleEn' => trim((string) $report->getReportTitle('en')),
  52.                     'titleAr' => trim((string) $report->getReportTitle('ar')),
  53.                     'reportTypeName' => $reportTypeName,
  54.                     'publicReportUrlEn' => $publicReportUrlEn,
  55.                     'publicReportUrlAr' => $publicReportUrlAr,
  56.                     'intro' => $this->translator->trans('alert_forecast_report_email_intro', [
  57.                         '%reportType%' => "تقرير الطقس التنبؤي",
  58.                     ], 'messages''ar'),
  59.                     'viewReportEn' => $this->translator->trans('alert_forecast_report_email_view_en', [], 'messages''ar'),
  60.                     'viewReportAr' => $this->translator->trans('alert_forecast_report_email_view_ar', [], 'messages''ar'),
  61.                 ]);
  62.                 $this->c2Service->sendCustomReportEmail(
  63.                     $templateId,
  64.                     $report->getId(),
  65.                     $email,
  66.                     $html,
  67.                     $subject,
  68.                     []
  69.                 );
  70.                 $sent true;
  71.             } catch (\Throwable $e) {
  72.                 $this->logger->error('Alert forecast report email failed for ' $email ': ' $e->getMessage());
  73.             }
  74.         }
  75.         // The email subscriber owns the delivery stamp for alert-forecast reports, so a
  76.         // subsequent re-save (incl. the twitter subscriber persisting its tweet id) does not
  77.         // re-send these emails. An approved edit bumps the version and re-opens the gate.
  78.         if ($sent) {
  79.             ReportModerationService::markVersionDelivered($report);
  80.         }
  81.     }
  82.     /**
  83.      * @return string[]
  84.      */
  85.     private function collectRecipientEmails(Report $report): array
  86.     {
  87.         $raw trim((string) $report->getEmails());
  88.         if ($raw === '') {
  89.             return [];
  90.         }
  91.         $emails = [];
  92.         foreach (explode(','$raw) as $email) {
  93.             $email trim($email);
  94.             if ($email !== '' && filter_var($emailFILTER_VALIDATE_EMAIL)) {
  95.                 $emails[] = $email;
  96.             }
  97.         }
  98.         return array_values(array_unique($emails));
  99.     }
  100.     private function shouldSendEmail(mixed $report): bool
  101.     {
  102.         return ($report instanceof Report) &&
  103.             $report->getDraftOf() === null &&
  104.             !ReportModerationService::hasDeliveredCurrentVersion($report) &&
  105.             ReportModerationService::isReportApproved($report) &&
  106.             $report->isPublished(true) &&
  107.             $report->getForEmailChannel() === true &&
  108.             $report->getReportType()?->getReportKey() === ALERT_FORECAST_REPORT_TYPE_KEY &&
  109.             trim((string) $report->getEmails()) !== '';
  110.     }
  111. }