src/EventSubscriber/ClientSubscriber.php line 45

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\ClientUpdatedEvent;
  7. use App\Event\ClientAgencyUpdatedEvent;
  8. use App\Repository\SystemEmailRepository;
  9. use App\Repository\MissionParticipantRepository;
  10. use App\Service\FrontAPIService;
  11. use App\Service\NotificationService;
  12. use App\Service\UserService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use App\Enum\Role;
  15. use App\Event\Client\NoticeOfInsufficientBudgetEvent;
  16. use App\Service\MissionParticipantService;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use App\Service\AssociatedMailUserService
  19. class ClientSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         private FrontAPIService $frontAPIService,
  23.         private SystemEmailRepository $systemEmailRepository,
  24.         private MissionParticipantRepository $missionParticipantRepository,
  25.         private NotificationService $notificationService,
  26.         private MissionParticipantService $missionParticipantService,
  27.         private EntityManagerInterface $entityManager,
  28.         private UserService $userService,
  29.         private AssociatedMailUserService $associatedMailUserService,
  30.     ){}
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             ClientUpdatedEvent::NAME => 'onClientUpdated',
  35.             ClientAgencyUpdatedEvent::NAME => 'onClientAgencyUpdated',
  36.             NoticeOfInsufficientBudgetEvent::NAME => 'onNoticeInsufficientBudget',
  37.         ];
  38.     }
  39.     public function onNoticeInsufficientBudget(NoticeOfInsufficientBudgetEvent $event){
  40.         $campaign $event->getCampaign();
  41.         $mission $event->getMission();
  42.         $company $event->getCompany(); 
  43.         $toUsers $event->getToUsers();
  44.         $isCampaign false
  45.         $email  $this->systemEmailRepository->findOneBy(['code'=>SystemEmail::NOTICE_INSUFFICIENT_BUDGET]);
  46.         $users = [];
  47.         $userAlreadySendNotification = [];
  48.         $isCampaign $campaign instanceof Campaign ?  true false
  49.         
  50.             if($isCampaign && !in_array($campaign->getCreditHistory()->getTypePack(),[3,4]) && $campaign->getEmailSentInsuffBudget()!=true){
  51.                     if($isCampaign) {
  52.                         $missionParticipants $this->missionParticipantService->getParticipants($campaign'subcontractor'); 
  53.                         $validators array_filter($missionParticipants, function($item){
  54.                             return $item->getRole() == Role::ROLE_VALIDATOR;
  55.                         });
  56.                 
  57.                         foreach ($validators as $key => $validator) {
  58.                             $users= [... $users$validator->getUser()];
  59.                         }
  60.                 
  61.                         $users = [...$users,$campaign->getOrderedBy()];
  62.                     }
  63.                     $users = [...$users,... $toUsers];
  64.                     
  65.                     foreach ($users as  $user) { 
  66.                         if(!in_array($user->getId(), $userAlreadySendNotification))
  67.                             $this->notificationService->create($email$user$user,$companynull$campaign);
  68.                             $userAlreadySendNotification = [...$userAlreadySendNotification$user->getId()];
  69.                     }
  70.                     $campaign->setEmailSentInsuffBudget(true);
  71.                     $this->entityManager->flush();
  72.                     
  73.         }
  74.     }
  75.     public function onClientUpdated(ClientUpdatedEvent $event)
  76.     {
  77.         $client $event->getClient();
  78.         if (!$client instanceof User) {
  79.             return;
  80.         }
  81.         // $client->setIsNewClient(true);
  82.         // $this->entityManager->flush();
  83.         if (!in_array('ROLE_OBSERVER',$client->getRoles()) and !in_array('ROLE_VALIDATOR',$client->getRoles()) and  $event->getSendToApi()) {
  84.             $this->frontAPIService->pushClientToFront($client$event->getPlainPassword());
  85.         }
  86.         $email null;
  87.         if ($event->getSendNotification() &&  !$client->getIsNewClient()) {
  88.             $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CREATION_NOUVEAU_CLIENT]);
  89.         }
  90.     
  91.         if ($event->getThankYouNotification()) {
  92.             $clientCompany =  $client->getCompany(); 
  93.             if ($client->getMyFlowEmail() === null and ($clientCompany ==null || $clientCompany->getParent() === null && $clientCompany->getTypeCompany() === null)) {
  94.                 $emailMyFlow $this->associatedMailUserService->createEmailMyFlow($client->getEmail());
  95.                 $client->setMyFlowEmail($emailMyFlow);
  96.                 $this->entityManager->persist($client);
  97.                 $this->entityManager->flush();
  98.             }
  99.             if(!$client->getIsNewClient() ){
  100.                 $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CONFIRMATION_INSCRIPTION]);
  101.                 $this->sendMailAddMission($client);
  102.             }
  103.         }
  104.       
  105.         if (null !== $email) {
  106.             $this->notificationService->create($email$client$client);
  107.         }
  108.         $this->userService->generateIdentifierForUser$client);
  109.         $this->userService->createMyflowEmail($client); 
  110.     }
  111.     public function onClientAgencyUpdated(ClientAgencyUpdatedEvent $event){
  112.         $client $event->getClient();
  113.         if (!$client instanceof User) {
  114.             return;
  115.         }
  116.         if (!in_array('ROLE_OBSERVER',$client->getRoles()) and !in_array('ROLE_VALIDATOR',$client->getRoles()) and  $event->getSendToApi()) {
  117.             $this->frontAPIService->pushClientToFront($client$event->getPlainPassword());
  118.         }
  119.         $email null;
  120.         if ($event->getSendNotification() &&  !$client->getIsNewClient()) {
  121.             $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CREATION_NOUVEAU_CLIENT_AGENCE]);
  122.         }
  123.         // if ($event->getThankYouNotification() && !$client->getIsNewClient() ) {
  124.         //     $email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CONFIRMATION_INSCRIPTION]);
  125.         //     $this->sendMailAddMission($client);
  126.         // }
  127.         if (null !== $email) {
  128.             $this->notificationService->create($email$client$client);
  129.         }
  130.         $this->userService->generateIdentifierForUser$client);
  131.     }
  132.     /**
  133.      * send email to validator or observator to invite to participate to the mission after confirmation inscription
  134.      * @param  $user User
  135.      * @return void
  136.      */
  137.     public function sendMailAddMission($user){
  138.         $attendees $this->missionParticipantRepository->getMissionForUser($user);
  139.         foreach ($attendees as $key => $attendee) {
  140.             $user $attendee->getUser();
  141.             $mission $attendee->getMission();
  142.             $company $mission->getCampaign()->getCompany();
  143.             if ($attendee->getRole() == Role::ROLE_VALIDATOR){
  144.                 $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_VALIDATEUR]);
  145.             }else{
  146.                 $email $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_OBSERVATEUR]);
  147.             }
  148.             if (null !== $email) {
  149.                 $this->notificationService->create($email$user$user$company$mission->getWorkflow()?->getActiveStep(), $mission->getCampaign());
  150.             }
  151.         }
  152.     }
  153. }