src/Domain/EventSubscriber/LogEntryCreatedEventSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Domain\EventSubscriber;
  4. use Domain\Event\LogEntryCreated;
  5. use Domain\Repository\UserRepositoryInterface;
  6. use Infrastructure\Persistence\Doctrine\Entity\LogEntry;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Address;
  11. final class LogEntryCreatedEventSubscriber implements EventSubscriberInterface
  12. {
  13.     private UserRepositoryInterface $userRepository;
  14.     private MailerInterface $mailer;
  15.     public function __construct(
  16.         UserRepositoryInterface $userRepository,
  17.         MailerInterface $mailer
  18.     ) {
  19.         $this->userRepository $userRepository;
  20.         $this->mailer $mailer;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             LogEntryCreated::class => 'handle',
  26.         ];
  27.     }
  28.     public function handle(LogEntryCreated $event): void
  29.     {
  30.         $logEntry $event->logEntry();
  31.         $recipients = [];
  32.         $logEntryCreator $logEntry->getUser();
  33.         if ($logEntryCreator->isManager() || $logEntryCreator->isCleaner()) {
  34.             return;
  35.         }
  36.         if ($logEntryCreator->isClient()) {
  37.             $recipientRole 'ROLE_CFC_MANAGER';
  38.             $recipients $this->getAddresses($recipientRole);
  39.         }
  40.         $this->communicateToRecipients($recipients$logEntry);
  41.     }
  42.     private function communicateToRecipients(array $recipientsLogEntry $logEntry): void
  43.     {
  44.         if (empty($recipients)) {
  45.             return;
  46.         }
  47.         // dd($recipients);
  48.         $this->mailer->send((new TemplatedEmail())
  49.             ->from(new Address('platform@cfc-platform.com''CFC Platform'))
  50.             ->to(...$recipients)
  51.             ->subject('Er is een nieuwe logboek item op CFC Platform')
  52.             ->htmlTemplate('logbook/new_logEntry_email.html.twig')
  53.             ->context([
  54.                 'createdByClient' => $logEntry->getUser()->isClient(),
  55.                 'companyName' => $logEntry->getCompany()->getName(),
  56.                 'companyId' => $logEntry->getCompany()->getId(),
  57.                 'logEntryId' => $logEntry->getId(),
  58.             ]));
  59.     }
  60.     /**
  61.      * @return Address[]
  62.      */
  63.     private function getAddresses(string $recipientRole, ?int $companyId null): array
  64.     {
  65.         return array_map(static function (array $recipient): Address {
  66.             return new Address($recipient['email'], $recipient['fullName']);
  67.         }, $this->userRepository->byRole($recipientRole$companyId)->toArray());
  68.     }
  69. }