<?php
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Entity\Company;
use App\Entity\Mail;
use App\Entity\Vacancy;
use App\Entity\VacancyMoreInfo;
use App\Event\VacancyMoreInfoEvent;
use App\Sendgrid\MailerInterface;
use App\Service\SiteService;
use App\Util\StringToEntityUtil;
use Gedmo\Translatable\TranslatableListener;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class VacancyMoreInfoEventListener extends FormSubmitEventListener
{
/**
* @var SiteService
*/
private $siteService;
/**
* VacancyMoreInfoEventListener constructor.
*/
public function __construct(
ParameterBagInterface $parameterBag,
MailerInterface $mailer,
StringToEntityUtil $stringToEntityUtil,
SiteService $siteService,
?TranslatableListener $translatableListener,
Config $config
) {
$this->siteService = $siteService;
parent::__construct($mailer, $parameterBag, $stringToEntityUtil, $translatableListener, $config);
}
public function infoFormSubmitted(VacancyMoreInfoEvent $vacancyMoreInfoEvent)
{
$senderMailParam = 'site_vacancy_more_info_sender_mail';
$companyMailParam = 'site_vacancy_more_info_company_mail';
/** @var VacancyMoreInfo $vacancyMoreInfoRequest */
$vacancyMoreInfoRequest = $vacancyMoreInfoEvent->getVacancyMoreInfoRequest();
$senderMail = $this->getMail($senderMailParam);
if ($senderMail &&
$vacancyMoreInfoRequest->getEmail()
) {
$this->mailer->mail(
$senderMail,
$this->getCompanyEmailSender(),
[$vacancyMoreInfoRequest->getEmail()],
['request' => $vacancyMoreInfoRequest, 'site' => $this->siteService->getSite()]
);
}
if (($companyMail = $this->getMail($companyMailParam)) instanceof Mail) {
$companyEmailAddress = $this->parameterBag->get('site_company_email_address');
if ($vacancyMoreInfoRequest->getVacancy() instanceof Vacancy) {
if ($vacancyMoreInfoRequest->getVacancy()->getCompany() instanceof Company) {
$companyEmailAddress = $vacancyMoreInfoRequest->getVacancy()->getCompany()->getApplicantEmail();
if ([] === $companyEmailAddress) {
$companyEmailAddress = $vacancyMoreInfoRequest->getVacancy()
->getCompany()
->getEmail();
}
}
if (\is_string($vacancyMoreInfoRequest->getVacancy()->getApplicantEmail())) {
$companyEmailAddress = $vacancyMoreInfoRequest->getVacancy()->getApplicantEmail();
}
}
if (\is_string($companyEmailAddress) || \is_array($companyEmailAddress)) {
$this->mailer->mail(
$companyMail,
$this->getCompanyEmailSender(),
\is_string($companyEmailAddress) ? [$companyEmailAddress] : $companyEmailAddress,
['request' => $vacancyMoreInfoRequest, 'site' => $this->siteService->getSite()]
);
}
}
}
}