src/EventListener/VacancyEventListener.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Component\Configuration\Util\Config;
  4. use App\Controller\Page\VacancyController;
  5. use App\Entity\Mail;
  6. use App\Entity\Option;
  7. use App\Entity\OptionValue;
  8. use App\Entity\Vacancy;
  9. use App\Event\VacancyEvent;
  10. use App\Sendgrid\MailerInterface;
  11. use App\Service\SiteService;
  12. use App\Util\StringToEntityUtil;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Flagception\Manager\FeatureManagerInterface;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. use Symfony\Component\Routing\RouterInterface;
  17. class VacancyEventListener extends FormSubmitEventListener
  18. {
  19.     /**
  20.      * @var string
  21.      */
  22.     public const FEATURED_LABEL '(top vacature)';
  23.     /**
  24.      * @var RouterInterface
  25.      */
  26.     private $router;
  27.     /**
  28.      * @var SiteService
  29.      */
  30.     private $siteService;
  31.     private EntityManagerInterface $entityManager;
  32.     private FeatureManagerInterface $featureManager;
  33.     public function __construct(
  34.         MailerInterface $mailer,
  35.         ParameterBagInterface $parameterBag,
  36.         StringToEntityUtil $stringToEntityUtil,
  37.         RouterInterface $router,
  38.         SiteService $siteService,
  39.         EntityManagerInterface $entityManager,
  40.         FeatureManagerInterface $featureManager,
  41.         Config $config
  42.     ) {
  43.         parent::__construct(
  44.             $mailer,
  45.             $parameterBag,
  46.             $stringToEntityUtil,
  47.             null,
  48.             $config
  49.         );
  50.         $this->router $router;
  51.         $this->siteService $siteService;
  52.         $this->entityManager $entityManager;
  53.         $this->featureManager $featureManager;
  54.     }
  55.     public function onVacancyCreatedWithoutAccount(VacancyEvent $event)
  56.     {
  57.         $vacancy $event->getVacancy();
  58.         $mailTemplate $this->getMail('site_vacancy_created_without_account');
  59.         if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_COVID_JOBALERT)) {
  60.             /** @var Option $covidOption */
  61.             $covidOption $this->entityManager->getRepository(Option::class)
  62.                 ->findOneBy(['internalName' => VacancyController::URL_PARAM_COVID_19])
  63.             ;
  64.             if ($covidOption) {
  65.                 /** @var OptionValue $covidOptionValue */
  66.                 $covidOptionValue $covidOption->getValues()->first();
  67.                 if ($covidOptionValue && $vacancy->getOptionValues()->contains($covidOptionValue)) {
  68.                     $mailTemplate $this->getMail('site_covid_vacancy_created_without_account');
  69.                 }
  70.             }
  71.         }
  72.         if ($mailTemplate) {
  73.             $this->sendNotification($mailTemplate$vacancy, ['order_type' => 'Vaste prijs (zonder account)']);
  74.         }
  75.     }
  76.     public function onVacancyEditedByUser(VacancyEvent $event)
  77.     {
  78.         $vacancy $event->getVacancy();
  79.         if ($mailTemplate $this->getMail('site_vacancy_edited_by_user')) {
  80.             $this->sendNotification($mailTemplate$vacancy);
  81.         }
  82.     }
  83.     public function onVacancyDeletedByUser(VacancyEvent $event)
  84.     {
  85.         $vacancy $event->getVacancy();
  86.         if ($mailTemplate $this->getMail('site_vacancy_deleted_by_user')) {
  87.             $this->sendNotification($mailTemplate$vacancy);
  88.         }
  89.     }
  90.     private function sendNotification(Mail $mailVacancy $vacancy, array $additionalPlaceholders = [])
  91.     {
  92.         $vacancyUrl $this->router->generate(
  93.             'vacancy_detail',
  94.             [
  95.                 'id' => $vacancy->getId(),
  96.                 'slug' => $vacancy->getSlug(),
  97.             ]
  98.         );
  99.         if ($vacancy->getSite()) {
  100.             $vacancyUrl $this->siteService->generateRoute(
  101.                 'vacancy_detail',
  102.                 $vacancy->getSite(),
  103.                 [
  104.                     'id' => $vacancy->getId(),
  105.                     'slug' => $vacancy->getSlug(),
  106.                 ]
  107.             );
  108.         }
  109.         $company $vacancy->getCompany();
  110.         $placeholders = [
  111.             'vacancy' => $vacancy,
  112.             'vacancy_title' => $vacancy->getTitle(),
  113.             'vacancy_site' => $vacancy->getSite() ? $vacancy->getSite()->getName() : null,
  114.             'vacancy_url' => $vacancyUrl,
  115.             'vacancy_featured' => $vacancy->isFeatured() ? self::FEATURED_LABEL '',
  116.             'vacancy_admin_url' => $this->router->generate('vacancy_edit', ['id' => $vacancy->getId()]),
  117.             'vacancy_reference_number' => $vacancy->getExternalReference(),
  118.             'contact_name' => $vacancy->getCompany() ? $vacancy->getCompany()->getContactPerson() : '',
  119.             'contact_phone' => $vacancy->getCompany() ? $vacancy->getCompany()->getPhone() : '',
  120.             'contact_email' => $vacancy->getCompany() ? $vacancy->getCompany()->getEmail() : '',
  121.             'company_name' => $company $company->getName() : '',
  122.             'company_department' => $company $company->getDepartment() : '',
  123.             'company_contact_person' => $company $company->getContactPerson() : '',
  124.             'company_email' => $company $company->getEmail() : '',
  125.             'company_address' => $company $company->getAddress() : '',
  126.             'company_zipcode' => $company $company->getZipcode() : '',
  127.             'company_city' => $company $company->getCity() : '',
  128.             'company_phone' => $company $company->getPhone() : '',
  129.             'company_invoice_name' => $company $company->getInvoiceCompanyName() : '',
  130.             'company_invoice_department' => $company $company->getInvoiceDepartment() : '',
  131.             'company_invoice_contact_person' => $company $company->getInvoiceContactPerson() : '',
  132.             'company_invoice_email' => $company $company->getInvoiceEmailAddress() : '',
  133.             'company_invoice_address' => $company $company->getInvoiceAddress() : '',
  134.             'company_invoice_zipcode' => $company $company->getInvoiceZipcode() : '',
  135.             'company_invoice_city' => $company $company->getInvoiceCity() : '',
  136.             'company_invoice_phone' => $company $company->getInvoicePhone() : '',
  137.             'publication_date' => $vacancy->getStartDate() ? $vacancy->getStartDate()->format('d-m-Y H:i') : '',
  138.             'expiration_date' => $vacancy->getEndDate() ? $vacancy->getEndDate()->format('d-m-Y H:i') : '',
  139.         ];
  140.         $placeholders array_merge($placeholders$additionalPlaceholders);
  141.         $this->mailer->mail(
  142.             $mail,
  143.             $this->getCompanyEmailSender(),
  144.             [$this->getFallbackEmailAddress()],
  145.             $placeholders
  146.         );
  147.     }
  148.     /**
  149.      * @return array|string
  150.      */
  151.     private function getFallbackEmailAddress()
  152.     {
  153.         if ($customRecipient $this->parameterBag->get('site_vacancy_notification_receiver')) {
  154.             return $customRecipient;
  155.         }
  156.         return $this->getCompanyEmailaddress();
  157.     }
  158. }