<?php
namespace App\EventSubscriber;
use App\Entity\Campaign;
use App\Entity\SystemEmail;
use App\Entity\User;
use App\Event\ClientUpdatedEvent;
use App\Event\ClientAgencyUpdatedEvent;
use App\Repository\SystemEmailRepository;
use App\Repository\MissionParticipantRepository;
use App\Service\FrontAPIService;
use App\Service\NotificationService;
use App\Service\UserService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Enum\Role;
use App\Event\Client\NoticeOfInsufficientBudgetEvent;
use App\Service\MissionParticipantService;
use Doctrine\ORM\EntityManagerInterface;
class ClientSubscriber implements EventSubscriberInterface
{
public function __construct(
private FrontAPIService $frontAPIService,
private SystemEmailRepository $systemEmailRepository,
private MissionParticipantRepository $missionParticipantRepository,
private NotificationService $notificationService,
private MissionParticipantService $missionParticipantService,
private EntityManagerInterface $entityManager,
private UserService $userService,
){}
public static function getSubscribedEvents()
{
return [
ClientUpdatedEvent::NAME => 'onClientUpdated',
ClientAgencyUpdatedEvent::NAME => 'onClientAgencyUpdated',
NoticeOfInsufficientBudgetEvent::NAME => 'onNoticeInsufficientBudget',
];
}
public function onNoticeInsufficientBudget(NoticeOfInsufficientBudgetEvent $event){
$campaign = $event->getCampaign();
$mission = $event->getMission();
$company = $event->getCompany();
$toUsers = $event->getToUsers();
$isCampaign = false;
$email = $this->systemEmailRepository->findOneBy(['code'=>SystemEmail::NOTICE_INSUFFICIENT_BUDGET]);
$users = [];
$userAlreadySendNotification = [];
$isCampaign = $campaign instanceof Campaign ? true : false;
if($isCampaign && !in_array($campaign->getCreditHistory()->getTypePack(),[3,4]) && $campaign->getEmailSentInsuffBudget()!=true){
if($isCampaign) {
$missionParticipants = $this->missionParticipantService->getParticipants($campaign, 'subcontractor');
$validators = array_filter($missionParticipants, function($item){
return $item->getRole() == Role::ROLE_VALIDATOR;
});
foreach ($validators as $key => $validator) {
$users= [... $users, $validator->getUser()];
}
$users = [...$users,$campaign->getOrderedBy()];
}
$users = [...$users,... $toUsers];
foreach ($users as $user) {
if(!in_array($user->getId(), $userAlreadySendNotification))
$this->notificationService->create($email, $user, $user,$company, null, $campaign);
$userAlreadySendNotification = [...$userAlreadySendNotification, $user->getId()];
}
$campaign->setEmailSentInsuffBudget(true);
$this->entityManager->flush();
}
}
public function onClientUpdated(ClientUpdatedEvent $event)
{
$client = $event->getClient();
if (!$client instanceof User) {
return;
}
// $client->setIsNewClient(true);
// $this->entityManager->flush();
if (!in_array('ROLE_OBSERVER',$client->getRoles()) and !in_array('ROLE_VALIDATOR',$client->getRoles()) and $event->getSendToApi()) {
$this->frontAPIService->pushClientToFront($client, $event->getPlainPassword());
}
$email = null;
if ($event->getSendNotification() && !$client->getIsNewClient()) {
$email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CREATION_NOUVEAU_CLIENT]);
}
if ($event->getThankYouNotification() && !$client->getIsNewClient() ) {
$email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CONFIRMATION_INSCRIPTION]);
$this->sendMailAddMission($client);
}
if (null !== $email) {
$this->notificationService->create($email, $client, $client);
}
$this->userService->generateIdentifierForUser( $client);
}
public function onClientAgencyUpdated(ClientAgencyUpdatedEvent $event){
$client = $event->getClient();
if (!$client instanceof User) {
return;
}
if (!in_array('ROLE_OBSERVER',$client->getRoles()) and !in_array('ROLE_VALIDATOR',$client->getRoles()) and $event->getSendToApi()) {
$this->frontAPIService->pushClientToFront($client, $event->getPlainPassword());
}
$email = null;
if ($event->getSendNotification() && !$client->getIsNewClient()) {
$email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CREATION_NOUVEAU_CLIENT_AGENCE]);
}
// if ($event->getThankYouNotification() && !$client->getIsNewClient() ) {
// $email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::CONFIRMATION_INSCRIPTION]);
// $this->sendMailAddMission($client);
// }
if (null !== $email) {
$this->notificationService->create($email, $client, $client);
}
$this->userService->generateIdentifierForUser( $client);
}
/**
* send email to validator or observator to invite to participate to the mission after confirmation inscription
* @param $user User
* @return void
*/
public function sendMailAddMission($user){
$attendees = $this->missionParticipantRepository->getMissionForUser($user);
foreach ($attendees as $key => $attendee) {
$user = $attendee->getUser();
$mission = $attendee->getMission();
$company = $mission->getCampaign()->getCompany();
if ($attendee->getRole() == Role::ROLE_VALIDATOR){
$email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_VALIDATEUR]);
}else{
$email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::AJOUT_OBSERVATEUR]);
}
if (null !== $email) {
$this->notificationService->create($email, $user, $user, $company, $mission->getWorkflow()?->getActiveStep(), $mission->getCampaign());
}
}
}
}