src/EventSubscriber/EmailNotificationSubscriber.php line 236

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Formation;
  4. use App\Entity\User;
  5. use App\Events\Events;
  6. use App\Service\email\EmailHandlerSendGrid;
  7. use App\Service\JwtHandler;
  8. use App\Service\Utils\Constante\AbonnementConst;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Twig\Environment;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Twig\Error\LoaderError;
  15. use Twig\Error\RuntimeError;
  16. use Twig\Error\SyntaxError;
  17. class EmailNotificationSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var EmailHandlerSendGrid
  21.      */
  22.     private $emailHandler;
  23.     /**
  24.      * @var JwtHandler
  25.      */
  26.     private $jwt;
  27.     /**
  28.      * @var UrlGeneratorInterface
  29.      */
  30.     private $urlGenerator;
  31.     /**
  32.      * @var Environment
  33.      */
  34.     private $twig;
  35.     /**
  36.      * @var User
  37.      */
  38.     private  $user;
  39.     /**
  40.      * EmailNotificationSubscriber constructor.
  41.      */
  42.     public function __construct(EmailHandlerSendGrid $emailHandlerJwtHandler $jwtUrlGeneratorInterface $urlGeneratorEnvironment $twigTokenStorageInterface $tokenStorage)
  43.     {
  44.         $this->emailHandler $emailHandler;
  45.         $this->jwt $jwt;
  46.         $this->urlGenerator $urlGenerator;
  47.         $this->twig $twig;
  48.         $this->user $tokenStorage->getToken() ?  $tokenStorage->getToken()->getUser() : null;
  49.     }
  50.     public static function getSubscribedEvents()
  51.     {
  52.         return [
  53.             Events::EMAIL_FORGOT_PWD => 'onForgotPwd',
  54.             Events::EMAIL_NEW_ACCOUNT => 'onNewAccount',
  55.             Events::FORMATION_REGISTRATION => 'onFormationRegistration',
  56.             Events::FORMATION_REMINDER => 'onFormationReminder',
  57.             Events::FORMATION_ADMIN_REMINDER => 'onFormationAdminReminder',
  58.             Events::FORMATION_RELANCE => 'onFormationRelance',
  59.             Events::EMAIL_CONTACT => 'onContact',
  60.             Events::COMPANY_STATUS_CHANGE => 'onCompanyStatusChange',
  61.             Events::SUBSCRIPTION_END_ADMIN => 'onSubscriptionEndAdmin',
  62.             Events::SUBSCRIPTION_END_RH => 'onSubscriptionEndRh',
  63.             Events::SUBSCRIPTION_EXPIRED_RH => 'onSubscriptionExpired',
  64.             Events::NOTIFICATION_NEW => 'onNotificationNew',
  65.             Events::NEW_ACCOUNT_RELANCE => 'onNewAccountRelance',
  66.             Events::EMAIL_COMPLETION => 'onCompletionLessThan',
  67.         ];
  68.     }
  69.     public function onForgotPwd(GenericEvent $event)
  70.     {
  71.         $data $event->getSubject();
  72.         /* @var $user User*/
  73.         $user $data['user'];
  74.         $context $data['context'];
  75.         //create token
  76.         $jwt $this->jwt->createToken(['uid' => $user->getId()]);
  77.         $path $context === "reset" "reset-password" "create-account";
  78.         $link $this->urlGenerator->generate($path, ['user' => $user->getId(), 'token' => $jwt], UrlGeneratorInterface::ABSOLUTE_URL);
  79.         $html $this->twig->render('email/new/reset.account.html.twig', [
  80.             'user' => $user,
  81.             'link' => $link,
  82.             'context'=>$context,
  83.             'home_url' => $_ENV['HOME_URL'] ? $_ENV['HOME_URL'] : '#'
  84.         ]);
  85.         /*Send email*/
  86.         $this->emailHandler->send([
  87.             'to' => $user->getEmail(),
  88.             'subject' => $context === "reset" 'Réinitialisation de votre mot de passe' 'Création de votre mot de passe',
  89.             'html' => $html
  90.         ]);
  91.     }
  92.     public function onNewAccount(GenericEvent $event)
  93.     {
  94.         /* @var $user User*/
  95.         $user $event->getSubject();
  96.         //create token
  97.         $jwt $this->jwt->createToken(['uid' => $user->getId()]);
  98.         $link $this->urlGenerator->generate('create-account', ['user' => $user->getId(), 'token' => $jwt], UrlGeneratorInterface::ABSOLUTE_URL);
  99.         if($user->hasRole('ROLE_RH')){
  100.             $html $this->twig->render('email/new/new.account.user.rh.html.twig', [
  101.                 'link' => $link,
  102.                 'user'=>$user,
  103.                 'home_url' => $_ENV['HOME_URL'] ?? '#'
  104.             ]);
  105.         }
  106.         else{
  107.             $html $this->twig->render('email/new/new.account.user.html.twig', [
  108.                 'link' => $link,
  109.                 'user'=>$user,
  110.                 'home_url' => $_ENV['HOME_URL'] ?? '#'
  111.             ]);
  112.         }
  113.         /*Send email*/
  114.         $this->emailHandler->send([
  115.             'to' => $user->getEmail(),
  116.             'subject' => "Bienvenue sur Kinalgo Corporate !",
  117.             'html' => $html
  118.         ]);
  119.     }
  120.     public function onNewAccountRelance(GenericEvent $event)
  121.     {
  122.         /* @var $user User*/
  123.         $user $event->getSubject();
  124.         //create token
  125.         $jwt $this->jwt->createToken(['uid' => $user->getId()]);
  126.         $link $this->urlGenerator->generate('create-account', ['user' => $user->getId(), 'token' => $jwt], UrlGeneratorInterface::ABSOLUTE_URL);
  127.         if($user->hasRole('ROLE_RH')){
  128.             $html $this->twig->render('email/new/relance.new.account.rh.html.twig', [
  129.                 'link' => $link,
  130.                 'user'=>$user,
  131.                 'home_url' => $_ENV['HOME_URL'] ?? '#'
  132.             ]);
  133.         }
  134.         else{
  135.             $html $this->twig->render('email/new/relance.new.account.html.twig', [
  136.                 'link' => $link,
  137.                 'user'=>$user,
  138.                 'home_url' => $_ENV['HOME_URL'] ?? '#'
  139.             ]);
  140.         }
  141.         /*Send email*/
  142.         $this->emailHandler->send([
  143.             'to' => $user->getEmail(),
  144.             'subject' => "📢 Dernier rappel : activez votre compte maintenant !",
  145.             'html' => $html
  146.         ]);
  147.     }
  148.     public function onFormationRegistration(GenericEvent $event)
  149.     {
  150.         $data $event->getSubject();
  151.         $html $this->twig->render('email/new/formation.registration.html.twig', [
  152.             'link' => $data['link'],
  153.             'user' => $data['user'],
  154.             'home_url' => $_ENV['HOME_URL'] ? $_ENV['HOME_URL'] : '#'
  155.         ]);
  156.         /*Send email*/
  157.         $this->emailHandler->send([
  158.             'to' => $data['user'] instanceof User ?  $data['user']->getEmail() : $data['user']['email'],
  159.             'subject' => "Vous avez reçu 1 recommandation de votre employeur",
  160.             'html' => $html
  161.         ]);
  162.     }
  163.     public function onFormationRelance(GenericEvent $event)
  164.     {
  165.         $data $event->getSubject();
  166.         $html $this->twig->render('email/new/formation.relance.html.twig', [
  167.             'link' => $data['link'],
  168.             'user' => $data['user'],
  169.             'formation' => $data['formation'],
  170.         ]);
  171.         /*Send email*/
  172.         $this->emailHandler->send([
  173.             'to' => $data['user'] instanceof User ?  $data['user']->getEmail() : $data['user']['email'],
  174.             'subject' => "Rappel : ".$data['formation']->getTitle(),
  175.             'html' => $html
  176.         ]);
  177.     }
  178.     public function onFormationReminder(GenericEvent $event)
  179.     {
  180.         $data $event->getSubject();
  181.         $subject = isset($data['subject']) ? $data['subject'] : "N’oubliez pas de consulter votre recommandation";
  182.         $html $this->twig->render('email/new/formation.reminder.html.twig', [
  183.             'link' => $data['link'],
  184.             'firstname' => $data['firstname'],
  185.             'home_url' => $_ENV['HOME_URL'] ?? '#'
  186.         ]);
  187.         /*Send email*/
  188.         $this->emailHandler->send([
  189.             'to' => $data['email'],
  190.             'subject' => $subject,
  191.             'html' => $html
  192.         ]);
  193.     }
  194.     /**
  195.      * TODO PAS UTILISE
  196.      * @param GenericEvent $event
  197.      * @return void
  198.      * @throws LoaderError
  199.      * @throws RuntimeError
  200.      * @throws SyntaxError
  201.      */
  202.     public function onFormationAdminReminder(GenericEvent $event)
  203.     {
  204.         $data $event->getSubject();
  205.         $message "Nous vous informons que votre publication ${data['title']} n'a toujours pas été entièrement visionnée par ${data['user_firstname']} ${data['user_lastname']}. ";
  206.         $subject "Publication obligatoire non lue";
  207.         $html $this->twig->render('email/formation.registration.html.twig', [
  208.             'user' => ['firstname' => $data['author_firstname']],
  209.             'message' =>  $message,
  210.             'home_url' => $_ENV['HOME_URL'] ?? '#'
  211.         ]);
  212.         /*Send email*/
  213.         $this->emailHandler->send([
  214.             'to' => $data['email'],
  215.             'subject' => $subject,
  216.             'html' => $html
  217.         ]);
  218.     }
  219.     public function onContact(GenericEvent $event)
  220.     {
  221.         $data $event->getSubject();
  222.         $html $this->twig->render('email/contact.html.twig', [
  223.             'user' =>  $this->user,
  224.             'subject' => $data['subject'],
  225.             'message' => $data['message'],
  226.             'home_url' => $_ENV['HOME_URL'] ? $_ENV['HOME_URL'] : '#'
  227.         ]);
  228.         /*Send email*/
  229.         $this->emailHandler->send([
  230.             'to' => $_ENV['EMAIL_ADMIN'],
  231.             'subject' => $data['subject'],
  232.             'html' => $html
  233.         ]);
  234.     }
  235.     public function onCompanyStatusChange(GenericEvent $event)
  236.     {
  237.         $data $event->getSubject();
  238.         $user $data['user'];
  239.         $subject $data['status'] === AbonnementConst::STATUS_ACTIVATED 'Réactivation de votre compte' 'Suspension de votre compte';
  240.         if($data['status'] === AbonnementConst::STATUS_ACTIVATED){
  241.             $html $this->twig->render('email/new/subscription.activated.html.twig', [
  242.                 'user' => $user,
  243.                 'home_url' => $_ENV['HOME_URL'] ? $_ENV['HOME_URL'] : '#'
  244.             ]);
  245.         }
  246.         else if($data['status'] === AbonnementConst::STATUS_SUSPENDED){
  247.             $html $this->twig->render('email/new/subscription.suspended.html.twig', [
  248.                 'user' => $user,
  249.                 'home_url' => $_ENV['HOME_URL'] ? $_ENV['HOME_URL'] : '#'
  250.             ]);
  251.         }else{
  252.             return false;
  253.         }
  254.         /*Send email*/
  255.         $this->emailHandler->send([
  256.             'to' => $user->getEmail(),
  257.             'subject' => $subject,
  258.             'html' => $html
  259.         ]);
  260.     }
  261.     public function onSubscriptionEndAdmin(GenericEvent $event)
  262.     {
  263.         $data $event->getSubject();
  264.         $subject "Abonnement de ${data['name']} bientôt expiré";
  265.         $html $this->twig->render('email/new/rappel.subscription.admin.html.twig'$data);
  266.         /*Send email*/
  267.         $this->emailHandler->send([
  268.             'to' => isset( $data["email"]) ? $data['email'] : $_ENV['EMAIL_ADMIN'],
  269.             'subject' => $subject,
  270.             'html' => $html
  271.         ]);
  272.         $this->emailHandler->send([
  273.             'to' => "sebastien.bourrel@kinalgo.com",
  274.             'subject' => $subject,
  275.             'html' => $html
  276.         ]);
  277.     }
  278.     public function onSubscriptionEndRh(GenericEvent $event)
  279.     {
  280.         $data $event->getSubject();
  281.         $subject "Votre abonnement va bientôt expirer";
  282.         $html $this->twig->render('email/new/rappel.subscription.rh.html.twig'$data);
  283.         /*Send email*/
  284.         $this->emailHandler->send([
  285.             'to' =>  $data['email'],
  286.             'subject' => $subject,
  287.             'html' => $html
  288.         ]);
  289.     }
  290.     public function onSubscriptionExpired(GenericEvent $event)
  291.     {
  292.         $data $event->getSubject();
  293.         $subject "Votre abonnement a expiré";
  294.         $html $this->twig->render('email/new/subscription.expired.rh.html.twig'$data);
  295.         /*Send email*/
  296.         $this->emailHandler->send([
  297.             'to' =>  $data['email'],
  298.             'subject' => $subject,
  299.             'html' => $html
  300.         ]);
  301.     }
  302.     public function onNotificationNew(GenericEvent $event)
  303.     {
  304.         $data $event->getSubject();
  305.         $user $data['userTo'];
  306.         $userFrom $data['userFrom'];
  307.         $type $data['type'] === 'formation' 'publication' $data['type'];
  308.         if (in_array('ROLE_ADMIN',$userFrom->getRoles())){
  309.             $fullName "Kinalgo® Corporate";
  310.         }
  311.         elseif (in_array('ROLE_RH',$userFrom->getRoles())){
  312.             $fullName "Votre établissement";
  313.         }
  314.         else{
  315.             $fullName $userFrom->getFirstname() . " " $userFrom->getLastname();
  316.         }
  317.         $message=sprintf(
  318.             '<p style="line-height:24px "> %s vous a partagé la %s : <br><a href="%s" style="color:#2B93D1">%s</a></p>',
  319.             $fullName,$type,$data['link'],$data['title']);
  320.         $html $this->twig->render('email/new/notification.new.html.twig', [
  321.             "user"=>$user,
  322.             "message"=>$message,
  323.             "link"=>$data['link']
  324.         ]);
  325.         /*Send email*/
  326.         $this->emailHandler->send([
  327.             'to' =>  $user->getEmail(),
  328.             'subject' => "Vous avez 1 nouvelle notification",
  329.             'html' => $html
  330.         ]);
  331.     }
  332.     public function onCompletionLessThan(GenericEvent $event)
  333.     {
  334.         /* @var $user User*/
  335.         $user $event->getSubject();
  336.         $html $this->twig->render('email/new/completion.html.twig', [
  337.             'user' => $user
  338.         ]);
  339.         /*Send email*/
  340.         $this->emailHandler->send([
  341.             'to' => $user->getEmail(),
  342.             'subject' => "Personnalisez votre expérience dès maintenant !",
  343.             'html' => $html
  344.         ]);
  345.     }
  346. }