src/EventSubscriber/UserSubscriber.php line 84

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