<?php
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Event\CallMeBackSubmitEvent;
use App\Sendgrid\MailerInterface;
use App\Util\StringToEntityUtil;
use Gedmo\Translatable\TranslatableListener;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class CallMeBackListener extends FormSubmitEventListener
{
public const DEFAULT_TEMPLATE_PROPERTIES = [
'recruiter' => '',
'vacancy' => '',
'vacancy_id' => '',
'vacancy_external_id' => '',
'vacancy_displayable_external_id' => '',
'vacancy_url' => '',
];
private RouterInterface $router;
public function __construct(
MailerInterface $mailer,
ParameterBagInterface $parameterBag,
StringToEntityUtil $stringToEntityUtil,
?TranslatableListener $translatableListener,
Config $config,
RouterInterface $router
) {
parent::__construct($mailer, $parameterBag, $stringToEntityUtil, $translatableListener, $config);
$this->router = $router;
}
public function postRequestSubmit(CallMeBackSubmitEvent $event)
{
$contact = $event->getRequest();
if ($siteOwnerMail = $this->getMail('site_call_me_back_mail_site_owner')) {
$templateProperties = self::DEFAULT_TEMPLATE_PROPERTIES;
$templateProperties['request'] = $contact;
$recipient = $this->getCompanyEmailaddress();
if ($this->config->get('site_call_me_back_custom_system_recipient')) {
$recipient = $this->config->get('site_call_me_back_custom_system_recipient');
}
if ($contact->getRecruiter() && $contact->getRecruiter()->getEmail()) {
$recipient = $contact->getRecruiter()->getEmail();
$templateProperties['recruiter'] = $contact->getRecruiter()->getName();
}
if (!empty($contact->getVacancy())
&& !empty($contact->getVacancy()->getRecruiter())
&& $recruiter = $contact->getVacancy()->getRecruiter()) {
$recipient = $recruiter->getEmail();
$templateProperties['vacancy'] = $contact->getVacancy()->getTitle();
$templateProperties['vacancy_id'] = $contact->getVacancy()->getId();
$templateProperties['vacancy_external_id'] = $contact->getVacancy()->getExternalId();
$templateProperties['vacancy_displayable_external_id'] = $contact->getVacancy()->getDisplayableExternalId();
$templateProperties['vacancy_url'] = $this->router->generate(
'vacancy_detail',
['id' => $contact->getVacancy()->getId(), 'slug' => $contact->getVacancy()->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
);
// Set recruiter from related vacancy, if call me back request has related vacancy.
$templateProperties['recruiter'] = $recruiter->getName();
}
$this->mailer->mail(
$siteOwnerMail,
$this->getCompanyEmailSender(),
[$recipient],
$templateProperties
);
}
}
}