<?php
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Controller\Page\VacancyController;
use App\Entity\Mail;
use App\Entity\Option;
use App\Entity\OptionValue;
use App\Entity\Vacancy;
use App\Event\VacancyEvent;
use App\Sendgrid\MailerInterface;
use App\Service\SiteService;
use App\Util\StringToEntityUtil;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\RouterInterface;
class VacancyEventListener extends FormSubmitEventListener
{
/**
* @var string
*/
public const FEATURED_LABEL = '(top vacature)';
/**
* @var RouterInterface
*/
private $router;
/**
* @var SiteService
*/
private $siteService;
private EntityManagerInterface $entityManager;
private FeatureManagerInterface $featureManager;
public function __construct(
MailerInterface $mailer,
ParameterBagInterface $parameterBag,
StringToEntityUtil $stringToEntityUtil,
RouterInterface $router,
SiteService $siteService,
EntityManagerInterface $entityManager,
FeatureManagerInterface $featureManager,
Config $config
) {
parent::__construct(
$mailer,
$parameterBag,
$stringToEntityUtil,
null,
$config
);
$this->router = $router;
$this->siteService = $siteService;
$this->entityManager = $entityManager;
$this->featureManager = $featureManager;
}
public function onVacancyCreatedWithoutAccount(VacancyEvent $event)
{
$vacancy = $event->getVacancy();
$mailTemplate = $this->getMail('site_vacancy_created_without_account');
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_COVID_JOBALERT)) {
/** @var Option $covidOption */
$covidOption = $this->entityManager->getRepository(Option::class)
->findOneBy(['internalName' => VacancyController::URL_PARAM_COVID_19])
;
if ($covidOption) {
/** @var OptionValue $covidOptionValue */
$covidOptionValue = $covidOption->getValues()->first();
if ($covidOptionValue && $vacancy->getOptionValues()->contains($covidOptionValue)) {
$mailTemplate = $this->getMail('site_covid_vacancy_created_without_account');
}
}
}
if ($mailTemplate) {
$this->sendNotification($mailTemplate, $vacancy, ['order_type' => 'Vaste prijs (zonder account)']);
}
}
public function onVacancyEditedByUser(VacancyEvent $event)
{
$vacancy = $event->getVacancy();
if ($mailTemplate = $this->getMail('site_vacancy_edited_by_user')) {
$this->sendNotification($mailTemplate, $vacancy);
}
}
public function onVacancyDeletedByUser(VacancyEvent $event)
{
$vacancy = $event->getVacancy();
if ($mailTemplate = $this->getMail('site_vacancy_deleted_by_user')) {
$this->sendNotification($mailTemplate, $vacancy);
}
}
private function sendNotification(Mail $mail, Vacancy $vacancy, array $additionalPlaceholders = [])
{
$vacancyUrl = $this->router->generate(
'vacancy_detail',
[
'id' => $vacancy->getId(),
'slug' => $vacancy->getSlug(),
]
);
if ($vacancy->getSite()) {
$vacancyUrl = $this->siteService->generateRoute(
'vacancy_detail',
$vacancy->getSite(),
[
'id' => $vacancy->getId(),
'slug' => $vacancy->getSlug(),
]
);
}
$company = $vacancy->getCompany();
$placeholders = [
'vacancy' => $vacancy,
'vacancy_title' => $vacancy->getTitle(),
'vacancy_site' => $vacancy->getSite() ? $vacancy->getSite()->getName() : null,
'vacancy_url' => $vacancyUrl,
'vacancy_featured' => $vacancy->isFeatured() ? self::FEATURED_LABEL : '',
'vacancy_admin_url' => $this->router->generate('vacancy_edit', ['id' => $vacancy->getId()]),
'vacancy_reference_number' => $vacancy->getExternalReference(),
'contact_name' => $vacancy->getCompany() ? $vacancy->getCompany()->getContactPerson() : '',
'contact_phone' => $vacancy->getCompany() ? $vacancy->getCompany()->getPhone() : '',
'contact_email' => $vacancy->getCompany() ? $vacancy->getCompany()->getEmail() : '',
'company_name' => $company ? $company->getName() : '',
'company_department' => $company ? $company->getDepartment() : '',
'company_contact_person' => $company ? $company->getContactPerson() : '',
'company_email' => $company ? $company->getEmail() : '',
'company_address' => $company ? $company->getAddress() : '',
'company_zipcode' => $company ? $company->getZipcode() : '',
'company_city' => $company ? $company->getCity() : '',
'company_phone' => $company ? $company->getPhone() : '',
'company_invoice_name' => $company ? $company->getInvoiceCompanyName() : '',
'company_invoice_department' => $company ? $company->getInvoiceDepartment() : '',
'company_invoice_contact_person' => $company ? $company->getInvoiceContactPerson() : '',
'company_invoice_email' => $company ? $company->getInvoiceEmailAddress() : '',
'company_invoice_address' => $company ? $company->getInvoiceAddress() : '',
'company_invoice_zipcode' => $company ? $company->getInvoiceZipcode() : '',
'company_invoice_city' => $company ? $company->getInvoiceCity() : '',
'company_invoice_phone' => $company ? $company->getInvoicePhone() : '',
'publication_date' => $vacancy->getStartDate() ? $vacancy->getStartDate()->format('d-m-Y H:i') : '',
'expiration_date' => $vacancy->getEndDate() ? $vacancy->getEndDate()->format('d-m-Y H:i') : '',
];
$placeholders = array_merge($placeholders, $additionalPlaceholders);
$this->mailer->mail(
$mail,
$this->getCompanyEmailSender(),
[$this->getFallbackEmailAddress()],
$placeholders
);
}
/**
* @return array|string
*/
private function getFallbackEmailAddress()
{
if ($customRecipient = $this->parameterBag->get('site_vacancy_notification_receiver')) {
return $customRecipient;
}
return $this->getCompanyEmailaddress();
}
}