<?php
namespace App\EventListener;
use App\Event\CreditsSubtractedEvent;
use App\Event\YearslotVacancyAddedEvent;
use App\Sendgrid\Mailer;
use App\Util\StringToEntityUtil;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreditEventSubscriber implements EventSubscriberInterface
{
/**
* @var Mailer
*/
protected $mailer;
/**
* @var ParameterBagInterface
*/
protected $parameterBag;
/**
* @var StringToEntityUtil
*/
protected $stringToEntityUtil;
/**
* CreditEventSubscriber constructor.
*/
public function __construct(
Mailer $mailer,
ParameterBagInterface $parameterBag,
StringToEntityUtil $stringToEntityUtil
) {
$this->mailer = $mailer;
$this->parameterBag = $parameterBag;
$this->stringToEntityUtil = $stringToEntityUtil;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
CreditsSubtractedEvent::class => 'onCreditsSubtracted',
YearslotVacancyAddedEvent::class => 'onYearslotVacancyAddition',
];
}
public function onCreditsSubtracted(CreditsSubtractedEvent $event)
{
$this->mailCompany($event);
$this->bottomThresholdMail($event);
}
public function onYearslotVacancyAddition(YearslotVacancyAddedEvent $event)
{
$mailId = $this->parameterBag->get('site_year_slot_mail_vacancy_added');
if (
!$event->shouldMail() ||
!$mailId ||
!$mail = $this->stringToEntityUtil->stringToEntity($mailId)
) {
return;
}
$this->mailer->mail(
$mail,
$this->parameterBag->get('site_company_email_address'),
[
$this->parameterBag->get('site_company_email_address'),
$event->getVacancy()->getCompany()->getEmail(),
],
[
'vacancy' => $event->getVacancy(),
'vacancy_title' => $event->getVacancy()->getTitle(),
'vacancy_site' => $event->getVacancy()->getSite() ? $event->getVacancy()->getSite()->getName() : null,
'yearslot' => $event->getYearslot(),
]
);
}
private function mailCompany(CreditsSubtractedEvent $event)
{
$mailId = $this->parameterBag->get('site_credits_subtracted_by_vacancy_mail');
if (
!$event->shouldMail() ||
!$mailId ||
!$mail = $this->stringToEntityUtil->stringToEntity($mailId)
) {
return;
}
$company = $event->getVacancy()->getCompany();
$this->mailer->mail(
$mail,
[$this->parameterBag->get('site_company_email_address') => $this->parameterBag->get('site_company_name')],
[
$this->parameterBag->get('site_company_email_address'),
$event->getVacancy()->getCompany()->getEmail(),
],
[
'vacancy' => $event->getVacancy(),
'vacancy_featured' => $event->getVacancy()->isFeatured() ? VacancyEventListener::FEATURED_LABEL : '',
'vacancy_title' => $event->getVacancy()->getTitle(),
'vacancy_site' => $event->getVacancy()->getSite() ? $event->getVacancy()->getSite()->getName() : null,
'vacancy_reference_number' => $event->getVacancy()->getExternalReference(),
'credits_subtracted' => $event->getCreditAmount(),
'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' => $event->getVacancy()->getStartDate() ? $event->getVacancy()->getStartDate()->format('d-m-Y H:i') : '',
'expiration_date' => $event->getVacancy()->getEndDate() ? $event->getVacancy()->getEndDate()->format('d-m-Y H:i') : '',
'order_type' => 'Credit',
]
);
}
private function bottomThresholdMail(CreditsSubtractedEvent $event)
{
if (
empty($this->parameterBag->get('site_credits_bottom_threshold_mail')) ||
$event->getVacancy()->getCompany()->getCredits() !== $this->parameterBag->get('site_credits_bottom_threshold')
) {
return;
}
$notificationMail = $this->stringToEntityUtil->stringToEntity(
$this->parameterBag->get('site_credits_bottom_threshold_mail')
);
$this->mailer->mail(
$notificationMail,
[$this->parameterBag->get('site_company_email_address') => $this->parameterBag->get('site_company_name')],
[$this->parameterBag->get('site_company_email_address')],
[
'vacancy' => $event->getVacancy(),
'company' => $event->getVacancy()->getCompany(),
'company_name' => $event->getVacancy()->getCompany() ? $event->getVacancy()->getCompany()->getName() : '',
]
);
}
}