<?php
declare(strict_types=1);
namespace Domain\EventSubscriber;
use Domain\Event\ReactionAddedToLogEntry;
use Domain\Repository\UserRepositoryInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
final class ReactionAddedToLogEntryEventSubscriber implements EventSubscriberInterface
{
private UserRepositoryInterface $userRepository;
private MailerInterface $mailer;
public function __construct(UserRepositoryInterface $userRepository, MailerInterface $mailer)
{
$this->userRepository = $userRepository;
$this->mailer = $mailer;
}
public static function getSubscribedEvents(): array
{
return [
ReactionAddedToLogEntry::class => 'handle',
];
}
public function handle(ReactionAddedToLogEntry $event): void
{
$logEntry = $event->logEntry();
$companyId = null;
if (!$event->reactionAddedBy()->isClient()) {
return;
}
$recipientRole = 'ROLE_CFC_MANAGER';
$recipients = array_map(static function (array $recipient): Address {
return new Address($recipient['email'], $recipient['fullName']);
}, $this->userRepository->byRole($recipientRole, $companyId)->toArray());
if (\count($recipients) < 1) {
return;
}
$this->mailer->send((new TemplatedEmail())
->from(new Address('platform@cfc-platform.com', 'CFC Platform'))
->to(...$recipients)
->subject('Er is een reactie toegevoegd aan een logboek melding')
->htmlTemplate('logbook/new_reaction_on_logEntry_email.html.twig')
->context([
'createdByClient' => $event->reactionAddedBy()->isClient(),
'companyName' => $logEntry->getCompany()->getName(),
'companyId' => $logEntry->getCompany()->getId(),
'logEntryId' => $logEntry->getId(),
]));
}
}