src/EventSubscriber/HolidayConfirmationEmailSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Employee;
  5. use App\Entity\Holiday;
  6. use App\Service\EmailService;
  7. use Carbon\Carbon;
  8. use Carbon\Factory;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Dotenv\Dotenv;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Mime\Address;
  16. final class HolidayConfirmationEmailSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityManagerInterface
  20.      */
  21.     private $entityManager;
  22.     /**
  23.      * @var EmailService
  24.      */
  25.     private $emailService;
  26.     /**
  27.      * @var Dotenv
  28.      */
  29.     private $dotenv;
  30.     /**
  31.      * HolidayConfirmationEmailSubscriber constructor.
  32.      * @param EntityManagerInterface $entityManager
  33.      * @param EmailService $emailService
  34.      */
  35.     public function __construct(EntityManagerInterface $entityManagerEmailService $emailService)
  36.     {
  37.         $this->dotenv = new Dotenv();
  38.         $this->dotenv->loadEnv(realpath(__DIR__ '/../..') . '/.env');
  39.         $this->entityManager $entityManager;
  40.         $this->emailService $emailService;
  41.     }
  42.     /**
  43.      * @inheritDoc
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::VIEW => ['sendConfirmationEmail'EventPriorities::POST_WRITE],
  49.         ];
  50.     }
  51.     public function sendConfirmationEmail(ViewEvent $viewEvent)
  52.     {
  53.         $holiday $viewEvent->getControllerResult();
  54.         $method $viewEvent->getRequest()->getMethod();
  55.         if (!$holiday instanceof Holiday || (Request::METHOD_PUT !== $method && Request::METHOD_POST !== $method)) {
  56.             return;
  57.         }
  58.         $status $holiday->getApproved();
  59.         /** @var Employee $employee */
  60.         $employee $holiday->getEmployee();
  61.         $emailEmployee = ($_ENV['APP_ENV'] === 'dev') ? $_ENV['DEFAULT_USER_EMAIL'] : $holiday->getEmployee()->getEmail();
  62.         $emailTeamManager $_ENV['DEFAULT_SUPERVISOR_EMAIL'];
  63.         $nameTeamManager $_ENV['DEFAULT_SUPERVISOR_NAME'];
  64.         $emailsSupervisors explode(",", ($_ENV['DEFAULT_SUPERVISORS_EMAILS']));
  65.         if ($holiday->getEmployee()->getTeamManager()) {
  66.             $emailTeamManager $holiday->getEmployee()->getTeamManager()->getEmail();
  67.             $nameTeamManager $holiday->getEmployee()->getTeamManager()->getName() . ' ' $holiday->getEmployee()->getTeamManager()->getLastName();
  68.             $emailsSupervisors[] = new Address($emailTeamManager$nameTeamManager);
  69.          }
  70.         $spanishDateFactory = new Factory([
  71.            'locale' => 'es_ES',
  72.            'timezone' => 'Europe/Madrid',
  73.         ]);
  74.         $fromDate $spanishDateFactory->make(Carbon::createFromFormat('Y-m-d',  $holiday->getFromDate()))->isoFormat('D MMMM YYYY');
  75.         $toDate $spanishDateFactory->make(Carbon::createFromFormat('Y-m-d',  $holiday->getToDate()))->isoFormat('D MMMM YYYY');
  76.         switch ($status) {
  77.             /* Solicitud vacaciones pendiente*/
  78.             case 0:
  79.                 /* Correo para el empleado */
  80.                 $this->emailService->sendEmail(
  81.                     'holiday-request-confirmation-email.html.twig',
  82.                     'Tu solicitud de vacaciones del '.$fromDate.' al '.$toDate,
  83.                     array($emailEmployee),
  84.                     $_ENV['INFO_EMAIL'],
  85.                     '',
  86.                     [
  87.                         'employeeName' => $holiday->getEmployee()->getName() . ' ' $holiday->getEmployee()->getLastName(),
  88.                         'employeeId' => $holiday->getEmployee()->getId(),
  89.                         'from' => $holiday->getFromDate(),
  90.                         'to' => $holiday->getToDate(),
  91.                         'daysConsumed' => $holiday->getDaysConsumed() > $holiday->getDaysConsumed() . ' días' $holiday->getDaysConsumed() . ' día',
  92.                         'url' => $_ENV['DEFAULT_URL'],
  93.                     ]
  94.                 );
  95.                 /* Correo para el jefe de equipo */
  96.                 $this->emailService->sendEmail(
  97.                     'holiday-request-confirmation-email-teamManager.html.twig',
  98.                     'Solicitud de vacaciones -- de '.$holiday->getEmployee()->getName().' '.$holiday->getEmployee()->getLastName(). ' del '.$fromDate.' al '.$toDate,
  99.                     $emailsSupervisors,
  100.                     $_ENV['INFO_EMAIL'],
  101. //                    $_ENV['NO_REPLY_MAIL'],
  102.                     '',
  103.                     [
  104.                         'employeeName' => $holiday->getEmployee()->getName() . ' ' $holiday->getEmployee()->getLastName(),
  105.                         'from' => $holiday->getFromDate(),
  106.                         'to' => $holiday->getToDate(),
  107.                         'daysConsumed' => $holiday->getDaysConsumed() > $holiday->getDaysConsumed() . ' días' $holiday->getDaysConsumed() . ' día',
  108.                         'url' => $_ENV['DEFAULT_URL'],
  109.                     ]
  110.                 );
  111.                 break;
  112.                 /* Solicitud de vacaciones aprobada*/
  113.             case 1:
  114. //                $employee->setCurrentDaysOff($employee->getCurrentDaysOff()-$holiday->getDaysConsumed());
  115.                 /* Correo para el empleado con las vacaciones aprobadas */
  116.                 $this->emailService->sendEmail(
  117.                     'holiday-email-approved.html.twig',
  118.                     'Vacaciones aprobadas del '.$fromDate.' al '.$toDate,
  119.                     array($emailEmployee),
  120.                     $_ENV['INFO_EMAIL'],
  121. //                    $_ENV['NO_REPLY_MAIL'],
  122.                     '',
  123.                     [
  124.                         'employeeName' => $holiday->getEmployee()->getName() . ' ' $holiday->getEmployee()->getLastName(),
  125.                         'employeeId' => $holiday->getEmployee()->getId(),
  126.                         'from' => $holiday->getFromDate(),
  127.                         'to' => $holiday->getToDate(),
  128.                         'daysConsumed' => $holiday->getDaysConsumed() > $holiday->getDaysConsumed() . ' días' $holiday->getDaysConsumed() . ' día',
  129.                         'nameTeamManager' => $nameTeamManager,
  130.                         'url' => $_ENV['DEFAULT_URL'],
  131.                     ]
  132.                 );
  133.                 break;
  134.                 /* Solicitud de las vacaciones rechazadas */
  135.             case 2:
  136.                 $this->emailService->sendEmail(
  137.                     'holiday-email-rejected.html.twig',
  138.                     'Solicitud de vacaciones rechazadas del '.$fromDate.' al '.$toDate,
  139.                     array($emailEmployee),
  140. //                    'faguilar@undanet.com',
  141.                     $_ENV['INFO_EMAIL'],
  142.                     '',
  143. //                    $_ENV['NO_REPLY_MAIL'],
  144.                     [
  145.                         'employeeName' => $holiday->getEmployee()->getName() . ' ' $holiday->getEmployee()->getLastName(),
  146.                         'employeeId' => $holiday->getEmployee()->getId(),
  147.                         'from' => $holiday->getFromDate(),
  148.                         'to' => $holiday->getToDate(),
  149.                         'daysConsumed' => $holiday->getDaysConsumed() > $holiday->getDaysConsumed() . ' días' $holiday->getDaysConsumed() . ' día',
  150.                         'nameTeamManager' => $nameTeamManager,
  151.                         'url' => $_ENV['DEFAULT_URL'],
  152.                     ]
  153.                 );
  154.                 break;
  155.             default:
  156.                 break;
  157.         }
  158.     }
  159. }