<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Employee;
use App\Entity\Holiday;
use App\Service\EmailService;
use Carbon\Carbon;
use Carbon\Factory;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mime\Address;
final class HolidayConfirmationEmailSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var EmailService
*/
private $emailService;
/**
* @var Dotenv
*/
private $dotenv;
/**
* HolidayConfirmationEmailSubscriber constructor.
* @param EntityManagerInterface $entityManager
* @param EmailService $emailService
*/
public function __construct(EntityManagerInterface $entityManager, EmailService $emailService)
{
$this->dotenv = new Dotenv();
$this->dotenv->loadEnv(realpath(__DIR__ . '/../..') . '/.env');
$this->entityManager = $entityManager;
$this->emailService = $emailService;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['sendConfirmationEmail', EventPriorities::POST_WRITE],
];
}
public function sendConfirmationEmail(ViewEvent $viewEvent)
{
$holiday = $viewEvent->getControllerResult();
$method = $viewEvent->getRequest()->getMethod();
if (!$holiday instanceof Holiday || (Request::METHOD_PUT !== $method && Request::METHOD_POST !== $method)) {
return;
}
$status = $holiday->getApproved();
/** @var Employee $employee */
$employee = $holiday->getEmployee();
$emailEmployee = ($_ENV['APP_ENV'] === 'dev') ? $_ENV['DEFAULT_USER_EMAIL'] : $holiday->getEmployee()->getEmail();
$emailTeamManager = $_ENV['DEFAULT_SUPERVISOR_EMAIL'];
$nameTeamManager = $_ENV['DEFAULT_SUPERVISOR_NAME'];
$emailsSupervisors = explode(",", ($_ENV['DEFAULT_SUPERVISORS_EMAILS']));
if ($holiday->getEmployee()->getTeamManager()) {
$emailTeamManager = $holiday->getEmployee()->getTeamManager()->getEmail();
$nameTeamManager = $holiday->getEmployee()->getTeamManager()->getName() . ' ' . $holiday->getEmployee()->getTeamManager()->getLastName();
$emailsSupervisors[] = new Address($emailTeamManager, $nameTeamManager);
}
$spanishDateFactory = new Factory([
'locale' => 'es_ES',
'timezone' => 'Europe/Madrid',
]);
$fromDate = $spanishDateFactory->make(Carbon::createFromFormat('Y-m-d', $holiday->getFromDate()))->isoFormat('D MMMM YYYY');
$toDate = $spanishDateFactory->make(Carbon::createFromFormat('Y-m-d', $holiday->getToDate()))->isoFormat('D MMMM YYYY');
switch ($status) {
/* Solicitud vacaciones pendiente*/
case 0:
/* Correo para el empleado */
$this->emailService->sendEmail(
'holiday-request-confirmation-email.html.twig',
'Tu solicitud de vacaciones del '.$fromDate.' al '.$toDate,
array($emailEmployee),
$_ENV['INFO_EMAIL'],
'',
[
'employeeName' => $holiday->getEmployee()->getName() . ' ' . $holiday->getEmployee()->getLastName(),
'employeeId' => $holiday->getEmployee()->getId(),
'from' => $holiday->getFromDate(),
'to' => $holiday->getToDate(),
'daysConsumed' => $holiday->getDaysConsumed() > 1 ? $holiday->getDaysConsumed() . ' días' : $holiday->getDaysConsumed() . ' día',
'url' => $_ENV['DEFAULT_URL'],
]
);
/* Correo para el jefe de equipo */
$this->emailService->sendEmail(
'holiday-request-confirmation-email-teamManager.html.twig',
'Solicitud de vacaciones -- de '.$holiday->getEmployee()->getName().' '.$holiday->getEmployee()->getLastName(). ' del '.$fromDate.' al '.$toDate,
$emailsSupervisors,
$_ENV['INFO_EMAIL'],
// $_ENV['NO_REPLY_MAIL'],
'',
[
'employeeName' => $holiday->getEmployee()->getName() . ' ' . $holiday->getEmployee()->getLastName(),
'from' => $holiday->getFromDate(),
'to' => $holiday->getToDate(),
'daysConsumed' => $holiday->getDaysConsumed() > 1 ? $holiday->getDaysConsumed() . ' días' : $holiday->getDaysConsumed() . ' día',
'url' => $_ENV['DEFAULT_URL'],
]
);
break;
/* Solicitud de vacaciones aprobada*/
case 1:
// $employee->setCurrentDaysOff($employee->getCurrentDaysOff()-$holiday->getDaysConsumed());
/* Correo para el empleado con las vacaciones aprobadas */
$this->emailService->sendEmail(
'holiday-email-approved.html.twig',
'Vacaciones aprobadas del '.$fromDate.' al '.$toDate,
array($emailEmployee),
$_ENV['INFO_EMAIL'],
// $_ENV['NO_REPLY_MAIL'],
'',
[
'employeeName' => $holiday->getEmployee()->getName() . ' ' . $holiday->getEmployee()->getLastName(),
'employeeId' => $holiday->getEmployee()->getId(),
'from' => $holiday->getFromDate(),
'to' => $holiday->getToDate(),
'daysConsumed' => $holiday->getDaysConsumed() > 1 ? $holiday->getDaysConsumed() . ' días' : $holiday->getDaysConsumed() . ' día',
'nameTeamManager' => $nameTeamManager,
'url' => $_ENV['DEFAULT_URL'],
]
);
break;
/* Solicitud de las vacaciones rechazadas */
case 2:
$this->emailService->sendEmail(
'holiday-email-rejected.html.twig',
'Solicitud de vacaciones rechazadas del '.$fromDate.' al '.$toDate,
array($emailEmployee),
// 'faguilar@undanet.com',
$_ENV['INFO_EMAIL'],
'',
// $_ENV['NO_REPLY_MAIL'],
[
'employeeName' => $holiday->getEmployee()->getName() . ' ' . $holiday->getEmployee()->getLastName(),
'employeeId' => $holiday->getEmployee()->getId(),
'from' => $holiday->getFromDate(),
'to' => $holiday->getToDate(),
'daysConsumed' => $holiday->getDaysConsumed() > 1 ? $holiday->getDaysConsumed() . ' días' : $holiday->getDaysConsumed() . ' día',
'nameTeamManager' => $nameTeamManager,
'url' => $_ENV['DEFAULT_URL'],
]
);
break;
default:
break;
}
}
}