src/EventSubscriber/UserSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Campaign;
  4. use App\Entity\SystemEmail;
  5. use App\Entity\User;
  6. use App\Repository\SystemEmailRepository;
  7. use App\Repository\UserRepository;
  8. use App\Service\FirebaseCloudeMessagingService;
  9. use App\Service\FrontAPIService;
  10. use App\Service\MessageService;
  11. use App\Service\NotificationService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use App\Enum\AdminMail;
  14. use App\Enum\Notification;
  15. use App\Event\User\PasswordResetEmailEvent;
  16. use App\Event\User\UserMentionnedEvent;
  17. use App\Event\User\SendEmailBeforeDeletedEvent;
  18. use App\Event\User\SendEmailOneMonthBeforeDeletedEvent;
  19. use App\Event\User\UserLoginWithoutPasswordEvent;
  20. use App\Event\User\UserReactionEvent;
  21. use App\Repository\ChatNotificationRepository;
  22. use App\Service\ShortcodeService;
  23. use Symfony\Component\Mime\Address;
  24. use App\Repository\FileMessageRepository;
  25. use App\Repository\MessageRepository;
  26. use App\Service\GoogleStorageService;
  27. use Twig\Environment;
  28. class UserSubscriber implements EventSubscriberInterface
  29. {
  30.     public function __construct(
  31.         private FrontAPIService $frontAPIService,
  32.         private SystemEmailRepository $systemEmailRepository,
  33.         private UserRepository $userRepository,
  34.         private ShortcodeService $shortcodeService,
  35.         private NotificationService $notificationService,
  36.         private ChatNotificationRepository $chatNotificationRepository,
  37.         private FileMessageRepository $fileMessageRepository,
  38.         private MessageRepository $messageRepository,
  39.         private GoogleStorageService $googleStorageService
  40.         private MessageService $messageService
  41.         private Environment $twig,
  42.         private FirebaseCloudeMessagingService $firebaseCloudeMessagingService,
  43.     ){}
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             UserMentionnedEvent::NAME => "onUserMentionnedInTchat",
  48.             UserReactionEvent::NAME=>"onUserReactionInMessage",
  49.             PasswordResetEmailEvent::NAME => 'onUserResetPassword',
  50.             SendEmailBeforeDeletedEvent::NAME => 'onSendEmailBeforeDeletedEvent',
  51.             SendEmailOneMonthBeforeDeletedEvent::NAME => 'onSendEmailOneMonthBeforeDeletedEvent',
  52.             UserLoginWithoutPasswordEvent::NAME => 'onUserLoginWithoutPasswordEvent'
  53.         ];
  54.     }
  55.     public function onUserLoginWithoutPasswordEvent(UserLoginWithoutPasswordEvent $event){
  56.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::LOGIN_WITHOUT_PASSWORD]);
  57.         $user $event->getUser(); 
  58.         $this->notificationService->create($email$user$user);
  59.         
  60.     }
  61.     public function onUserResetPassword(PasswordResetEmailEvent $event){
  62.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::RESET_PASSWORD]);
  63.         $user $event->getUser(); 
  64.         $this->notificationService->create($email$user$user);
  65.     }
  66.     public function onUserReactionInMessage(UserReactionEvent $event){
  67.         $messageCampaingId $event->getCampaign()->getId();
  68.         $to $event->getTo();
  69.         $from $event->getFrom();
  70.         $emoji $event->getEmoji();
  71.         $message $event->getMessage();
  72.         $reaction $event->getReaction(); 
  73.         $attachments null;
  74.         $linkAttachments null;
  75.         $filelinksUrl null;
  76.         $displayCodeToAddInMissionInMailHeader true;
  77.         $mission $message->getMission(); 
  78.         foreach ($message->getFileMessages() as $fileMessage) {
  79.             if ($fileMessage->isIsNew()) {
  80.                 $nameOfFileinBucket "Campaigns/{$mission->getCampaign()->getId()}/{$fileMessage->getName()}";
  81.                 $nameOfBucket "company-{$mission->getCampaign()->getCompany()->getId()}";
  82.                 $link $this->googleStorageService->getUrlBucket(strtolower($nameOfBucket),$nameOfFileinBucket);
  83.             }
  84.             else {
  85.                 $link "{$this->getParameter('back_website_url')}/{$this->messageService->getFileMissionUrl($mission$fileMessage->getName())}";
  86.             } 
  87.         
  88.             $linksFileMessages[]=[
  89.                 'link'=>$link
  90.                 'name'=>$fileMessage->getName()
  91.             ];
  92.         }
  93.         // $content = "<p>Bonjour {$to->getFullName()}</p>
  94.         //             <div>{$from->getFullName()} a réagi à votre message : {$message->getContent() } 
  95.         //                     <p>(Réaction : $emoji)</p>
  96.         //             </div>";        
  97.         $render =  $this->twig->render('emails/metion_in_message.html.twig',[
  98.             'message'=>  $message,
  99.             'reaction'=>$reaction,
  100.             'mission'=> $mission,
  101.             'sendTo'=>  $to
  102.             'user_second_color'=>'#08085c',
  103.             'user_primary_color'=> '#08085c'
  104.             ]
  105.         ); 
  106.         $this->notificationService->sendEmail(
  107.             new Address('caroline.b@my-flow.fr''myFlow'),
  108.             new Address($to->getEmail()),
  109.             "{$from->getFullName()} a réagi à votre  message",
  110.             $render,
  111.             $attachments,
  112.             $linkAttachments,
  113.             $filelinksUrl,
  114.             $displayCodeToAddInMissionInMailHeader,
  115.             $messageCampaingId
  116.         );
  117.     }
  118.     public function onUserMentionnedInTchat(UserMentionnedEvent $event) {
  119.         $mission $event->getMission();
  120.         $campaign =  $mission->getCampaign();
  121.         $user $event->getUser();
  122.         $message $event->getMessage();
  123.         $messageId $event->getMessageId();
  124.         $destinationMail $event->getUserEmail();
  125.         $subject ="Vous avez été mentionné(e) !"
  126.         $content "Votre nom a été cité dans un message. Cliquez pour voir où !"
  127.         $messageObj $this->messageRepository->findOneBy(['id'=>$messageId]);
  128.         $groupId $messageObj->getDiscussionGroup()!=null $messageObj->getDiscussionGroup() : FirebaseCloudeMessagingService::GROUP
  129.         $linksFileMessages =[];
  130.         $render =  $this->twig->render('emails/new_message.html.twig',[
  131.             'message'=>  $messageObj,
  132.             'sendTo'=> $user
  133.             'user_second_color'=>'#08085c',
  134.             'user_primary_color'=> '#08085c'
  135.             ]
  136.         ); 
  137.         foreach ($messageObj->getFileMessages() as $fileMessage) {
  138.             if ($fileMessage->isIsNew()) {
  139.                 $nameOfFileinBucket "Campaigns/{$mission->getCampaign()->getId()}/{$fileMessage->getName()}";
  140.                 $nameOfBucket "company-{$mission->getCampaign()->getCompany()->getId()}";
  141.                 $link $this->googleStorageService->getUrlBucket(strtolower($nameOfBucket),$nameOfFileinBucket);
  142.             }
  143.             else {
  144.                 $link "{$this->getParameter('back_website_url')}/{$this->messageService->getFileMissionUrl($mission$fileMessage->getName())}";
  145.             } 
  146.         
  147.             $linksFileMessages[]=[
  148.                 'link'=>$link
  149.                 'name'=>$fileMessage->getName()
  150.             ];
  151.         }
  152.         
  153.         if ($user !=null &&  $this->canSendEmailToUser($user)) {
  154.             $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::USER_MENTIONNED]);
  155.             $user $this->setEmailForContactProject(user:$user);
  156.             // $emailParsed = $this->shortcodeService->parse($email->getContent(), $user, $campaign->getCompany(),null, $campaign, null, null,null, null, null, null, $message);
  157.            
  158.             $this->notificationService->sendEmail(
  159.                 from: new Address('operation@my-flow.fr'),
  160.                 to: new Address($destinationMail), 
  161.                 subject$email->getSubject(), 
  162.                 content$render
  163.                 attachmentsnull
  164.                 linkAttachmentsnull,
  165.                 filelinksUrl$linksFileMessages,
  166.                 displayCodeToAddInMissionInMailHeader true,
  167.                 campaignId$campaign->getId(),
  168.                 usernull,
  169.                 messageId$messageId,
  170.             );
  171.             
  172.             $this->removePreviewNotificationToSendTo($user$campaign);
  173.             $this->firebaseCloudeMessagingService->createPushNotificationByMission($user$mission,$subject$contentFirebaseCloudeMessagingService::MESSAGES$groupId); 
  174.         }
  175.         else {
  176.             
  177.             $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::ALL_USER_MENTIONNED]);
  178.             foreach ($this->getUserParticipants($campaign,$messageObj?->getDiscussionGroup()) as $user) {
  179.                 
  180.                 if ($this->canSendEmailToUser($user)) {
  181.                     $this->removePreviewNotificationToSendTo($user$campaign);
  182.                     $user $this->setEmailForContactProject(user:$user);
  183.                     // $emailParsed = $this->shortcodeService->parse($email->getContent(), $user, $campaign->getCompany(),null, $campaign, null, null,null, null, null, null, $message);
  184.                     
  185.                     $this->notificationService->sendEmail(
  186.                         from: new Address('operation@my-flow.fr'),
  187.                         to: new Address($user->getEmail()), 
  188.                         subject$email->getSubject(), 
  189.                         content:  $render
  190.                         attachmentsnull
  191.                         linkAttachmentsnull,
  192.                         filelinksUrl$linksFileMessages,
  193.                         displayCodeToAddInMissionInMailHeader true,
  194.                         campaignId$campaign->getId(),
  195.                     );
  196.                     $this->firebaseCloudeMessagingService->createPushNotificationByMission($user$mission,$subject$contentFirebaseCloudeMessagingService::MESSAGES$groupId); 
  197.                 }
  198.             } 
  199.         }
  200.     }
  201.     public function onSendEmailBeforeDeletedEvent(SendEmailBeforeDeletedEvent $event){
  202.         $user $event->getUser();
  203.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::EMAIL_BEFORE_DELETED_USER]);
  204.         $this->notificationService->create($email$user$user);
  205.     }
  206.     public function onSendEmailOneMonthBeforeDeletedEvent(SendEmailOneMonthBeforeDeletedEvent $event){
  207.         $user $event->getUser();
  208.         $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::EMAIL_ONE_MONTH_BEFORE_DELETED_USER]);
  209.         $this->notificationService->create($email$user$user);
  210.        
  211.     }
  212.     private function getUserParticipants(Campaign $campaign,$groupedMessage null){
  213.        $ids = [];
  214.        $allParticipantWithAdmin = [];
  215.        if (!is_null($groupedMessage)) {
  216.            foreach ($groupedMessage->getUsers() as $userInGroup) {
  217.                if (!in_array($userInGroup->getId(), $ids) and !$userInGroup->getDeleted() and $userInGroup->isEnabled()) {
  218.                     $ids[] = $userInGroup->getId();            
  219.                     $allParticipantWithAdmin[] = $userInGroup;            
  220.                 }
  221.            }
  222.        }else{
  223.             foreach ($campaign->getMissions() as $mission) {
  224.                 foreach ($mission->getParticipants() as $participant) {
  225.                     if (!in_array($participant->getUser()->getId(), $ids) and !$participant->getUser()->getDeleted() and $participant->getUser()->isEnabled()) {
  226.                         $ids[] = $participant->getUser()->getId();            
  227.                         $allParticipantWithAdmin[] = $participant->getUser();            
  228.                     }
  229.                 }
  230.             }
  231.        }
  232.        return $allParticipantWithAdmin ;
  233.     }
  234.     private function setEmailForContactProject(User $user){
  235.         if ($user->getId()=="Kb8bh3rBpsRukYgsMoYM9v") {
  236.              $adminEmail $this->userRepository->findOneBy(['email'=>"michael.lh@my-flow.fr"]) ; 
  237.              return  $adminEmail != null $adminEmail $user;
  238.         }
  239.         if ($user->getId()=="L15WDFhbcUcwtNFtBB2r5Q") {
  240.              $adminEmail $this->userRepository->findOneBy(['email'=>"thomas.s@my-flow.fr"]);
  241.              return  $adminEmail != null $adminEmail $user;
  242.              
  243.         }
  244.         return $user;
  245.     }
  246.     private function canSendEmailToUser(User $user){
  247.         // return (($user->getLastActivityInMinute() > 10 || !$user->isLogged()) && !is_null($user->getFreqNotificationEverytime()) && in_array(Notification::MENTIONNED_TO_ME->value, $user->getFreqNotificationEverytime())) ? true : false;
  248.         return (!is_null($user->getFreqNotificationEverytime()) && in_array(Notification::MENTIONNED_TO_ME->value$user->getFreqNotificationEverytime())) ? true false;
  249.     }
  250.     private function removePreviewNotificationToSendTo(User $user,Campaign $campaign){
  251.     
  252.            $chatNotifications =  $this->chatNotificationRepository->findNextNotificationToSend($user,$campaign);
  253.     }
  254. }