<?php
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Event\RegionContactSubmitEvent;
use App\Sendgrid\MailerInterface;
use App\Service\RegionContactService;
use App\Util\StringToEntityUtil;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class RegionContactListener extends FormSubmitEventListener
{
private RegionContactService $service;
public function __construct(
MailerInterface $mailer,
ParameterBagInterface $parameterBag,
StringToEntityUtil $stringToEntityUtil,
RegionContactService $service,
Config $config
) {
parent::__construct($mailer, $parameterBag, $stringToEntityUtil, null, $config);
$this->service = $service;
}
public function postContactSubmit(RegionContactSubmitEvent $event)
{
$contact = $event->getContact();
$region = $contact->getCompany() ? $this->service->fetchFilteredCompanyName($contact->getCompany()->getName()) : '';
if ($siteOwnerMail = $this->getMail('site_region_contact_mail_site_owner')) {
// Set default recipient
$recipient = $this->parameterBag->get('site_company_email_address');
// Check if company was provided in the Region Contact form and set the email address if so.
if ($contact->getCompany()) {
$recipient = $contact->getCompany()->getEmail() ?? $recipient;
}
// Last override, site_region_contact_single_recipient can be empty, then the company check above should be used
$recipient = !empty($this->parameterBag->get('site_region_contact_single_recipient')) ?
$this->parameterBag->get('site_region_contact_single_recipient') : $recipient;
$this->mailer->mail(
$siteOwnerMail,
$this->getCompanyEmailSender(),
[$recipient],
['contact' => $contact, 'region' => $region]
);
}
if ($visitorMail = $this->getMail('site_region_contact_mail_visitor')) {
$this->mailer->mail(
$visitorMail,
$this->getCompanyEmailSender(),
[$contact->getEmail()],
['contact' => $contact, 'region' => $region]
);
}
}
}