<?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\Entity\Document\Document;
use App\Entity\Document\DocumentModel;
use App\Enum\DocumentModelTypeEnum;
use App\Enum\DocumentStatusEnum;
use App\Enum\DocumentTypeEnum;
use App\Event\Document\DocumentStatusChangedEvent;
use App\Service\UserService;
use Doctrine\ORM\EntityManagerInterface;
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 DocumentStatusChangedSubscriber implements EventSubscriberInterface
{
private Environment $twig;
private string $senderEmail;
private string $senderName;
private MailerInterface $mailer;
private EntityManagerInterface $entityManager;
private UserService $userService;
public function __construct(Environment $twig, MailerInterface $mailer, EntityManagerInterface $entityManager, UserService $userService,
string $senderEmail, string $senderName)
{
$this->twig = $twig;
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->mailer = $mailer;
$this->entityManager = $entityManager;
$this->userService = $userService;
}
public static function getSubscribedEvents(): array
{
return [DocumentStatusChangedEvent::NAME => 'doProcess'];
}
public function doProcess(DocumentStatusChangedEvent $event): void
{
$this->updateUserStatus($event);
$this->updateCourseUserSubscriptionStatus($event);
$this->sendMailToUser($event);
}
public static function getNotifSubscriptionDocEvents()
{
return [NotifDocumentStatusChangedEvent::NAME => 'notifSubscriptionDoc'];
}
public function notifSubscriptionDoc(DocumentStatusChangedEvent $event): void
{
$user = $event->getUser();
$emailContent = $this->twig->render('emails/user/user_register.html.twig', [
'user' => $user,
]);
$email = (new TemplatedEmail())
->from(new Address($this->senderEmail, $this->senderName))
->to((string) $user->getEmail())
->subject('Bienvenu à BikingMan')
->html($emailContent)
;
$this->mailer->send($email);
}
private function updateUserStatus(DocumentStatusChangedEvent $event): void
{
$document = $event->getDocument();
if (DocumentTypeEnum::GLOBAL !== $document->getType() && DocumentTypeEnum::USER !== $document->getType()) {
return;
}
$user = $document->getParticipant();
$documentModels = $this->entityManager->getRepository(DocumentModel::class)->findBy(['type' => DocumentModelTypeEnum::USER]);
/** @var DocumentModel $documentModel */
foreach ($documentModels as $documentModel) {
$userDocuments = $this->entityManager->getRepository(Document::class)->findBy([
'createdBy' => $user->getId(),
'model' => $documentModel->getId(),
'type' => DocumentTypeEnum::USER,
'LastDoc' => true,
]);
if (count($userDocuments) < 1) {
return;
}
/** @var Document $userDocument */
foreach ($userDocuments as $userDocument) {
if (DocumentStatusEnum::APPROVED !== $userDocument->getStatus()) {
return;
}
}
}
$user->setProgressiveStatus(100);
$this->entityManager->flush();
$this->userService->createCourseSubscription($user);
$this->userService->createCourseSubscriptionNotification($user);
}
private function updateCourseUserSubscriptionStatus(DocumentStatusChangedEvent $event): void
{
$document = $event->getDocument();
if (DocumentTypeEnum::GLOBAL !== $document->getType() && DocumentTypeEnum::EVENT !== $document->getType()) {
return;
}
$user = $document->getParticipant();
$courseUserSubscription = $document->getCourseUserSubscription();
if (null != $courseUserSubscription) {
$course = $courseUserSubscription->getCourse();
$documentModels = $this->entityManager->getRepository(DocumentModel::class)->createQueryBuilder('dm')
->select('dm')
->addSelect('c')
->join('dm.courses', 'c')
->where('dm.type = :type')
->andWhere('c.id = :course')
->setParameter('type', DocumentModelTypeEnum::EVENT)
->setParameter('course', $course->getId())
->getQuery()
->getResult();
$courseUserSubscription->setProgressiveStatus($courseUserSubscription->getStatus() + 40);
} else {
$course = null;
$documentModels = $this->entityManager->getRepository(DocumentModel::class)->createQueryBuilder('dm')
->select('dm')
->addSelect('c')
->join('dm.courses', 'c')
->where('dm.type = :type')
->setParameter('type', DocumentModelTypeEnum::EVENT)
->getQuery()
->getResult();
}
/** @var DocumentModel $documentModel */
foreach ($documentModels as $documentModel) {
$userCourseDocuments = $this->entityManager->getRepository(Document::class)->findBy([
'createdBy' => $user->getId(),
'model' => $documentModel->getId(),
'type' => DocumentTypeEnum::EVENT,
]);
if (count($userCourseDocuments) < 1) {
return;
}
/** @var Document $userCourseDocument */
foreach ($userCourseDocuments as $userCourseDocument) {
if (DocumentStatusEnum::APPROVED !== $userCourseDocument->getStatus()) {
return;
}
}
}
}
private function sendMailToUser(DocumentStatusChangedEvent $event): void
{
$document = $event->getDocument();
$courseUserSubscription = $document->getCourseUserSubscription();
$emailContent = $this->twig->render('emails/user/document_status.html.twig', [
'document' => $document,
]);
$user = $document->getParticipant();
if (null !== $user) {
if($courseUserSubscription != null){
$email = (new TemplatedEmail())
->from(new Address('app@bikingman.com', 'BikingMan'))
->to((string) $user->getEmail())
->subject('Vos documents pour le BikingMan '.$courseUserSubscription->getCourse()->translate('fr')->getname().' #'.$courseUserSubscription->getCourse()->getNumber().' '.$courseUserSubscription->getCourse()->getSeason())
->html($emailContent);
}else{
$email = (new TemplatedEmail())
->from(new Address('app@bikingman.com', 'BikingMan'))
->to((string) $user->getEmail())
->subject('Vos documents BikingMan')
->html($emailContent);
}
$this->mailer->send($email);
}
}
}