<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Component\Event\Message\EventRegistrationToIntegration;
use App\Entity\Mail;
use App\Event\EventRegistrationEvent;
use App\Sendgrid\MailerInterface;
use App\Util\StringToEntityUtil;
use Gedmo\Translatable\TranslatableListener;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class EventRegistrationEventListener extends FormSubmitEventListener
{
public function __construct(
MailerInterface $mailer,
ParameterBagInterface $parameterBag,
StringToEntityUtil $stringToEntityUtil,
?TranslatableListener $translatableListener,
Config $config,
protected readonly MessageBusInterface $messageBus
) {
parent::__construct($mailer, $parameterBag, $stringToEntityUtil, $translatableListener, $config);
}
public function onRegistration(EventRegistrationEvent $event): void
{
$eventEntity = $event->getEvent();
$registration = $event->getRegistration();
$eventDateFrom = $eventEntity?->getDateFrom()?->format('d-m-Y H:i');
$eventDateTo = $eventEntity?->getDateTo()?->format('d-m-Y H:i');
$confirmationMail = $this->getMail('site_event_registration_confirmation_mail');
if (null !== $eventEntity?->getMail()) {
$confirmationMail = $eventEntity->getMail();
}
if ($confirmationMail instanceof Mail) {
$senderEmail = $this->parameterBag->get('site_event_registration_sender_mail');
if (!\is_string($senderEmail)) {
/** @var string $senderEmail */
$senderEmail = $this->parameterBag->get('site_company_email_address');
}
$senderName = $this->parameterBag->get('site_event_registration_sender_name');
if (!\is_string($senderName)) {
/** @var string $senderName */
$senderName = $this->parameterBag->get('site_company_name');
}
$this->mailer->mail(
$confirmationMail,
[$senderEmail => $senderName],
[$event->getEmail()],
[
'event' => $eventEntity,
'eventDateTo' => $eventDateTo,
'eventDateFrom' => $eventDateFrom,
'registration' => $registration,
]
);
}
$registrationMail = $this->getMail('site_event_registration_registration_mail');
if ($registrationMail instanceof Mail) {
$sendTo = $this->parameterBag->get('site_event_registration_email_address');
if (!\is_string($sendTo)) {
$sendTo = $this->parameterBag->get('site_company_email_address');
}
$this->mailer->mail(
$registrationMail,
$this->getCompanyEmailSender(),
[$sendTo],
[
'event' => $eventEntity,
'eventDateTo' => $eventDateTo,
'eventDateFrom' => $eventDateFrom,
'registration' => $registration,
]
);
}
if (
null !== $eventEntity?->getExternalReference()
&& \is_int($registration->getId())
&& \is_int($eventEntity->getId())
) {
$this->messageBus->dispatch(new EventRegistrationToIntegration($registration->getId(), $eventEntity->getId()));
}
}
public function onProductRegistration(EventRegistrationEvent $eventRegistrationEvent): void
{
$eventRegistration = $eventRegistrationEvent->getRegistration();
$visitorMail = $this->getMail('site_product_registration_visitor');
if ($visitorMail instanceof Mail) {
/** @var string $senderEmail */
$senderEmail = \is_string($this->parameterBag->get('site_product_registration_sender_mail')) ?
$this->parameterBag->get('site_product_registration_sender_mail')
: $this->parameterBag->get('site_company_email_address');
/** @var string $senderEmail */
$senderName = \is_string($this->parameterBag->get('site_product_registration_sender_name')) ?
$this->parameterBag->get('site_product_registration_sender_name')
: $this->parameterBag->get('site_company_name');
$this->mailer->mail(
$visitorMail,
[$senderEmail => $senderName],
[$eventRegistration->getEmail()],
[
'product' => $eventRegistration->getProduct(),
'registration' => $eventRegistration,
]
);
}
$siteOwnerMail = $this->getMail('site_product_registration_site_owner');
if ($siteOwnerMail instanceof Mail) {
$this->mailer->mail(
$siteOwnerMail,
$this->getCompanyEmailSender(),
[$this->parameterBag->get('site_company_email_address')],
[
'product' => $eventRegistration->getProduct(),
'registration' => $eventRegistration,
]
);
}
}
}