src/EventSubscriber/ClientSubscriber.php line 81

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