<?php
/*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential.
*
* @author Bilel AZRI <azri.bilel@gmail.com>
* @author Assma BEN SASSI <bensassiasma.bws@gmail.com>
*
* Bicking man (c) 2019-present.
*/
declare(strict_types=1);
namespace App\EventSubscriber\Mailer;
use App\Event\Course\CourseUserSubscriptionStatusChangedEvent;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Twig\Environment;
final class CourseUserSubscriptionStatusChangedSubscriber implements EventSubscriberInterface
{
private Environment $twig;
private string $senderEmail;
private string $senderName;
private MailerInterface $mailer;
public function __construct(Environment $twig, MailerInterface $mailer, string $senderEmail, string $senderName)
{
$this->twig = $twig;
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->mailer = $mailer;
}
public static function getSubscribedEvents(): array
{
return [CourseUserSubscriptionStatusChangedEvent::NAME => 'sendMailToUser'];
}
public function sendMailToUser(CourseUserSubscriptionStatusChangedEvent $event): void
{
$subscription = $event->getSubscription();
$emailContent = $this->twig->render('emails/user/course_subscription_status.html.twig', [
'subscription' => $subscription,
]);
$user = $subscription->getCreatedBy();
if (null !== $user) {
$email = (new TemplatedEmail())
->from(new Address($this->senderEmail, $this->senderName))
->to((string) $user->getEmail())
->subject('Votre inscription au BikingMan '.$subscription->getCourse()->translate('fr')->getname().' #'.$subscription->getCourse()->getNumber().' '.$subscription->getCourse()->getSeason())
->html($emailContent)
;
$this->mailer->send($email);
}
}
}