src/EventSubscriber/ScheduleCompleteSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Job;
  5. use App\Entity\Schedule;
  6. use App\Entity\User;
  7. use App\Service\Message;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use JetBrains\PhpStorm\ArrayShape;
  10. use Monolog\DateTimeImmutable;
  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\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use function in_array;
  17. final class ScheduleCompleteSubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var string The relative path inside templates/messages without any extensions */
  20.     private const TEMPLATE 'schedule/complete';
  21.     /** @var int The user ID of the admin user to notify */
  22.     private const ADMIN_TO_NOTIFY 1;
  23.     public function __construct(
  24.         private readonly TokenStorageInterface $tokenStorage,
  25.         private readonly ManagerRegistry $managerRegistry,
  26.         private readonly Message $messageService,
  27.     )
  28.     {
  29.     }
  30.     #[ArrayShape([KernelEvents::VIEW => "array"])]
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             KernelEvents::VIEW => ['sendMessage'EventPriorities::POST_WRITE],
  35.         ];
  36.     }
  37.     public function sendMessage(ViewEvent $event): void
  38.     {
  39.         $job $event->getControllerResult();
  40.         $method $event->getRequest()->getMethod();
  41.         # PUT and PATCH methods are used to update instances, and we are only interested in updated jobs
  42.         if (!$job instanceof Job || !in_array($method, [Request::METHOD_PUTRequest::METHOD_PATCH])) {
  43.             return;
  44.         }
  45.         $previousData $event->getRequest()->get('previous_data');
  46.         # If the status hasn't changed, we're not interested
  47.         if ($previousData->getStatus() === $job->getStatus()) {
  48.             return;
  49.         }
  50.         # If the status hasn't changed to "complete", we're not interested
  51.         if ($job->getStatus() !== Job::JOB_STATUS_COMPLETE) {
  52.             return;
  53.         }
  54.         /** @var User $currentUser */
  55.         $currentUser $this->tokenStorage->getToken()->getUser();
  56.         /** @var iterable<Schedule> $schedules */
  57.         $schedules $this->managerRegistry->getRepository(Schedule::class)
  58.             ->findBy([
  59.                 'employee' => $currentUser->getEmployee(),
  60.                 'date' => new DateTimeImmutable('today'),
  61.             ]);
  62.         $completed 0;
  63.         $total 0;
  64.         foreach ($schedules as $schedule) {
  65.             $total++;
  66.             if ($schedule->getJob()->getStatus() === Job::JOB_STATUS_COMPLETE) {
  67.                 $completed++;
  68.             }
  69.         }
  70.         # If the employee has not completed all of the jobs today, we're not interested
  71.         if ($completed !== $total) {
  72.             return;
  73.         }
  74.         // @todo find a better way of choosing which admin(s) to notify
  75.         $admin $this->managerRegistry->getRepository(User::class)
  76.             ->find(self::ADMIN_TO_NOTIFY);
  77.         $message $this->messageService
  78.             ->withRecipient($admin)
  79.             ->withSubject(sprintf('%s has completed today\'s jobs.'$currentUser->getFirstName()))
  80.             ->withTemplate(self::TEMPLATE)
  81.             ->withTemplateData([
  82.                 'employeeName' => $currentUser->getFirstName(),
  83.             ]);
  84.         if ($currentUser instanceof User) {
  85.             $message $message->withCurrentUser($currentUser);
  86.         }
  87.         $message->send();
  88.     }
  89. }