<?php
declare(strict_types=1);
namespace Harmonizely\EventSubscriber;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Harmonizely\Service\Panel\Api\Logout\Contract\ILogoutAfterChangePasswordService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Harmonizely\Core\BackgroundWork\Contract\IClient;
use Harmonizely\Background\Data\DirectorySaveUserData;
class EditProfileSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private string $userEmail;
/**
* @var IClient
*/
private IClient $backgroundClient;
/**
* @var ILogoutAfterChangePasswordService
*/
private ILogoutAfterChangePasswordService $logoutAfterChangePasswordService;
/**
* @param IClient $backgroundClient
* @param ILogoutAfterChangePasswordService $logoutAfterChangePasswordService
*/
public function __construct(
IClient $backgroundClient,
ILogoutAfterChangePasswordService $logoutAfterChangePasswordService
)
{
$this->backgroundClient = $backgroundClient;
$this->logoutAfterChangePasswordService = $logoutAfterChangePasswordService;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
FOSUserEvents::PROFILE_EDIT_COMPLETED => 'profileEditCompleted',
FOSUserEvents::PROFILE_EDIT_INITIALIZE => 'profileEditInitialize',
];
}
public function profileEditInitialize(GetResponseUserEvent $event): void
{
$this->userEmail = $event->getUser()->getEmail();
}
/**
* @param FilterUserResponseEvent $event
* @return void
*/
public function profileEditCompleted(FilterUserResponseEvent $event): void
{
$user = $event->getUser();
if ($user->getEmail() !== $this->userEmail) {
$this->logoutAfterChangePasswordService->perform();
}
$this->backgroundClient->add(new DirectorySaveUserData($user->getId()));
$this->backgroundClient->runJobs();
}
}