src/EventSubscriber/TimeOff/StatusChangeNotifier.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\TimeOff;
  3. use App\Entity\TimeOff;
  4. use App\Entity\Workflow\WorkflowTransitionLog;
  5. use App\Mail\Mailer;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Workflow\Event\Event;
  8. class StatusChangeNotifier implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var Mailer
  12.      */
  13.     private $mailer;
  14.     public function __construct(Mailer $mailer)
  15.     {
  16.         $this->mailer $mailer;
  17.     }
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             'workflow.timeoff_status.entered' => 'onEntered',
  25.         ];
  26.     }
  27.     public function onEntered(Event $event): void
  28.     {
  29.         if ($event->getTransition() && in_array($event->getTransition()->getName(), ['approve''reject''cancel'])) {
  30.             /** @var TimeOff $timeOff */
  31.             $timeOff $event->getSubject();
  32.             /** @var WorkflowTransitionLog $lastTransition */
  33.             $lastTransition $timeOff->getWorkflowTransitionLogs()->last();
  34.             $actor $lastTransition->getUser();
  35.             $user $timeOff->getRequestedBy();
  36.             if ($user === $actor) {
  37.                 // Don't alert ourselves that we canceled our time off request
  38.                 return;
  39.             }
  40.             $status $timeOff->getStatus();
  41.             $this->mailer->send('time-off/status-change'compact('user''actor''timeOff''status'), [$timeOff->getRequestedBy()->getEmail()], null,
  42.                 sprintf('Time off request %s - %s %s',
  43.                     null !== $timeOff->getDateFrom() ? $timeOff->getDateFrom()->format('M j, Y') : null,
  44.                     null !== $timeOff->getDateTo() ? $timeOff->getDateTo()->format('M j, Y') : null,
  45.                     $status)
  46.             );
  47.         }
  48.     }
  49. }