<?php
namespace App\EventSubscriber;
use App\C2IntegrationBundle\Service\C2Service;
use App\Service\ReportModerationService;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Model\DataObject\Report;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class AlertForecastReportEmailEventSubscriber
{
private C2Service $c2Service;
public function __construct(
private EngineInterface $twig,
private TranslatorInterface $translator,
private ApplicationLogger $logger,
) {
$this->c2Service = new C2Service();
}
public function onAlertForecastReportPublished(DataObjectEvent $event): void
{
$report = $event->getObject();
if (!$this->shouldSendEmail($report)) {
return;
}
$emails = $this->collectRecipientEmails($report);
if ($emails === []) {
return;
}
$baseUrl = rtrim((string) ($_ENV['PUBLIC_URL'] ?? NCM_PUBLIC_PORTAL_URL), '/');
$reportId = $report->getId();
$publicReportUrlEn = $baseUrl . '/en/weather/weather-reports/' . $reportId;
$publicReportUrlAr = $baseUrl . '/ar/weather/weather-reports/' . $reportId;
$reportTypeName = trim((string) ($report->getReportType()?->getName('ar') ?? ''));
if ($reportTypeName === '') {
$reportTypeName = trim((string) ($report->getReportType()?->getName('en') ?? ''));
}
if ($reportTypeName === '') {
$reportTypeName = 'تقرير تنبؤ وتوقعات';
}
$subject = $this->translator->trans('alert_forecast_report_email_subject', [], 'messages', 'ar');
$templateId = $_ENV['ALERT_FORECAST_REPORT_TEMPLATE'] ?? null;
if (empty($templateId)) {
$this->logger->error('Alert forecast report email skipped: ALERT_FORECAST_REPORT_TEMPLATE is not configured');
return;
}
foreach ($emails as $email) {
try {
$html = $this->twig->render('email/alert_forecast_report_email.html.twig', [
'titleEn' => trim((string) $report->getReportTitle('en')),
'titleAr' => trim((string) $report->getReportTitle('ar')),
'reportTypeName' => $reportTypeName,
'publicReportUrlEn' => $publicReportUrlEn,
'publicReportUrlAr' => $publicReportUrlAr,
'intro' => $this->translator->trans('alert_forecast_report_email_intro', [
'%reportType%' => "تقرير الطقس التنبؤي",
], 'messages', 'ar'),
'viewReportEn' => $this->translator->trans('alert_forecast_report_email_view_en', [], 'messages', 'ar'),
'viewReportAr' => $this->translator->trans('alert_forecast_report_email_view_ar', [], 'messages', 'ar'),
]);
$this->c2Service->sendCustomReportEmail(
$templateId,
$report->getId(),
$email,
$html,
$subject,
[]
);
} catch (\Throwable $e) {
$this->logger->error('Alert forecast report email failed for ' . $email . ': ' . $e->getMessage());
}
}
}
/**
* @return string[]
*/
private function collectRecipientEmails(Report $report): array
{
$raw = trim((string) $report->getEmails());
if ($raw === '') {
return [];
}
$emails = [];
foreach (explode(',', $raw) as $email) {
$email = trim($email);
if ($email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emails[] = $email;
}
}
return array_values(array_unique($emails));
}
private function shouldSendEmail(mixed $report): bool
{
return ($report instanceof Report) &&
ReportModerationService::isReportApproved($report) &&
$report->isPublished(true) &&
$report->getForEmailChannel() === true &&
$report->getReportType()?->getReportKey() === ALERT_FORECAST_REPORT_TYPE_KEY &&
trim((string) $report->getEmails()) !== '';
}
}