src/Domain/EventSubscriber/ReactionAddedToLogEntryEventSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Domain\EventSubscriber;
  4. use Domain\Event\ReactionAddedToLogEntry;
  5. use Domain\Repository\UserRepositoryInterface;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Address;
  10. final class ReactionAddedToLogEntryEventSubscriber implements EventSubscriberInterface
  11. {
  12.     private UserRepositoryInterface $userRepository;
  13.     private MailerInterface $mailer;
  14.     public function __construct(UserRepositoryInterface $userRepositoryMailerInterface $mailer)
  15.     {
  16.         $this->userRepository $userRepository;
  17.         $this->mailer $mailer;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ReactionAddedToLogEntry::class => 'handle',
  23.         ];
  24.     }
  25.     public function handle(ReactionAddedToLogEntry $event): void
  26.     {
  27.         $logEntry $event->logEntry();
  28.         $companyId null;
  29.         if (!$event->reactionAddedBy()->isClient()) {
  30.             return;
  31.         }
  32.         $recipientRole 'ROLE_CFC_MANAGER';
  33.         $recipients array_map(static function (array $recipient): Address {
  34.             return new Address($recipient['email'], $recipient['fullName']);
  35.         }, $this->userRepository->byRole($recipientRole$companyId)->toArray());
  36.         if (\count($recipients) < 1) {
  37.             return;
  38.         }
  39.         $this->mailer->send((new TemplatedEmail())
  40.             ->from(new Address('platform@cfc-platform.com''CFC Platform'))
  41.             ->to(...$recipients)
  42.             ->subject('Er is een reactie toegevoegd aan een logboek melding')
  43.             ->htmlTemplate('logbook/new_reaction_on_logEntry_email.html.twig')
  44.             ->context([
  45.                 'createdByClient' => $event->reactionAddedBy()->isClient(),
  46.                 'companyName' => $logEntry->getCompany()->getName(),
  47.                 'companyId' => $logEntry->getCompany()->getId(),
  48.                 'logEntryId' => $logEntry->getId(),
  49.             ]));
  50.     }
  51. }