<?php
namespace App\Api\EventListener;
use App\Api\DataHub\CustomerPortalApiClient;
use App\Entity\PrivateUser;
use App\Entity\ProUser;
use App\Event\UserEvents;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DataHubFOSListener implements EventSubscriberInterface
{
public function __construct(
private readonly CustomerPortalApiClient $cpaClient,
private readonly EntityManagerInterface $entityManager
) {
}
public static function getSubscribedEvents(): array
{
return [
FOSUserEvents::REGISTRATION_COMPLETED => 'onUserRegistrationComplete',
FOSUserEvents::REGISTRATION_CONFIRMED => 'onUserRegistrationConfirmed',
UserEvents::EDIT_COMPLETED => 'onUserProfileEditComplete',
UserEvents::CONVERT_SOCIETY => 'onUserConversionSociety',
UserEvents::UPDATE_USER_BY_SALESFORCE => 'onUserUpdateBySalesforce',
UserEvents::ID_SEND_USER_BY_SALESFORCE => 'onUserSendIdBySalesforce',
UserEvents::ID_SEND_USER_BY_SALESFORCE_AFTER_BUS => 'onUserChangeIdSalesforceByOther',
UserEvents::UPDATE_ID_SALESFORCE => 'onUserChangeIdSalesforceByOther',
];
}
/**
* FOSUser Event REGISTRATION_COMPLETED handling method.
*
* @param FilterUserResponseEvent $evt - The event
*/
public function onUserRegistrationComplete(FilterUserResponseEvent $evt): bool
{
$user = $evt->getUser();
if ($user instanceof PrivateUser) {
return false;
}
$result = $this->cpaClient->findCompanyOrCreate($user);
if (isset($result['id'])) {
$this->cpaClient->updateContact($user, $result['id'], true);
$this->cpaClient->uploadCompanyKbis($result['id']);
if (null !== $user->getAdditionalFile()) {
$this->cpaClient->uploadCompanyDocument($result['id']);
}
$this->cpaClient->uploadContactDocument($user);
$user->setCompanyId($result['id']);
$this->entityManager->flush();
}
return true;
}
/**
* FOSUser Event REGISTRATION_CONFIRMED handling method.
*
* @param FilterUserResponseEvent $evt - The event
*/
public function onUserRegistrationConfirmed(FilterUserResponseEvent $evt): bool
{
$user = $evt->getUser();
if ($user instanceof PrivateUser) {
$result = $this->cpaClient->createCustomer($user);
$user->setCustomerId($result['id']);
$user->setAdId($result['principalContact']);
$this->entityManager->flush();
$this->cpaClient->updateCustomerStatus($user->getCustomerId());
return true;
}
return false;
}
/**
* Custom event EDIT_COMPLETED handling method.
*
* @param UserEvent $evt - The event
*/
public function onUserProfileEditComplete(UserEvent $evt): bool
{
$user = $evt->getUser();
if (null === $user->getAdId()) {
return false;
}
if ($user instanceof PrivateUser) {
$this->cpaClient->updateCustomer($user);
return true;
}
$companyId = $this->getCompanyId($user);
if (null === $companyId) {
return false;
}
$this->cpaClient->updateContact($user, $companyId);
return true;
}
/**
* Custom event CONVERT_SOCIETY handling method.
*
* @param UserEvent $evt - The event
*/
public function onUserConversionSociety(UserEvent $evt): bool
{
/* @var PrivateUser $user */
$user = $evt->getUser();
if (!$user instanceof PrivateUser && $user->isSociety()) {
return false;
}
$this->cpaClient->deleteContact($user->getAdId());
$connection = $this->entityManager->getConnection();
$companyId = $connection->executeQuery('SELECT companyId FROM user WHERE id = :id', ['id' => $user->getId()])->fetchOne();
if (!empty($companyId)) {
$datas = $this->cpaClient->getContactsByFilter(['$filter' => "assignedEntityId eq '".$companyId."'"]);
if (0 == $datas['count']) {
$this->cpaClient->deleteCompany($companyId);
}
}
$connection->executeStatement('UPDATE user SET companyId = NULL, adId = NULL WHERE id = :id', ['id' => $user->getId()]);
return true;
}
/**
* Custom event UPDATE_USER_BY_SALESFORCE handling method.
*
* @param UserEvent $evt - The event
*/
public function onUserUpdateBySalesforce(UserEvent $evt): bool
{
$user = $evt->getUser();
if (!$user instanceof ProUser) {
return false;
}
$companyId = $this->getCompanyId($user);
if (null === $companyId) {
return false;
}
$this->cpaClient->updateCompany($user, $companyId);
$this->cpaClient->updateContact($user, $companyId);
return true;
}
/**
* Custom event ID_SEND_USER_BY_SALESFORCE handling method.
*
* @param UserEvent $evt - The event
*/
public function onUserSendIdBySalesforce(UserEvent $evt): bool
{
$user = $evt->getUser();
if (!$user instanceof ProUser) {
return false;
}
$companyId = $this->getCompanyId($user);
if (null === $companyId) {
return false;
}
$this->cpaClient->updateCompany($user, $companyId);
$this->cpaClient->updateCompanyStatus($companyId);
$this->cpaClient->updateContactStatus($user->getAdId());
return true;
}
/**
* Custom event ID_SEND_USER_BY_SALESFORCE_AFTER_BUS handling method.
* Custom event UPDATE_ID_SALESFORCE handling method.
*
* @param UserEvent $evt - The event
*/
public function onUserChangeIdSalesforceByOther(UserEvent $evt): bool
{
$user = $evt->getUser();
if (!$user instanceof ProUser) {
return false;
}
$companyId = $this->getCompanyId($user);
if (null === $companyId) {
return false;
}
$this->cpaClient->updateCompany($user, $companyId);
return true;
}
private function getCompanyId(ProUser $user)
{
return $this->cpaClient->searchCompany($user);
}
}