src/EventSubscriber/EditProfileSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use FOS\UserBundle\Event\GetResponseUserEvent;
  5. use Harmonizely\Service\Panel\Api\Logout\Contract\ILogoutAfterChangePasswordService;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use FOS\UserBundle\FOSUserEvents;
  8. use FOS\UserBundle\Event\FilterUserResponseEvent;
  9. use Harmonizely\Core\BackgroundWork\Contract\IClient;
  10. use Harmonizely\Background\Data\DirectorySaveUserData;
  11. class EditProfileSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var string
  15.      */
  16.     private string $userEmail;
  17.     /**
  18.      * @var IClient
  19.      */
  20.     private IClient $backgroundClient;
  21.     /**
  22.      * @var ILogoutAfterChangePasswordService
  23.      */
  24.     private ILogoutAfterChangePasswordService $logoutAfterChangePasswordService;
  25.     /**
  26.      * @param IClient $backgroundClient
  27.      * @param ILogoutAfterChangePasswordService $logoutAfterChangePasswordService
  28.      */
  29.     public function __construct(
  30.         IClient $backgroundClient,
  31.         ILogoutAfterChangePasswordService $logoutAfterChangePasswordService
  32.     )
  33.     {
  34.         $this->backgroundClient $backgroundClient;
  35.         $this->logoutAfterChangePasswordService $logoutAfterChangePasswordService;
  36.     }
  37.     /**
  38.      * @return string[]
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             FOSUserEvents::PROFILE_EDIT_COMPLETED => 'profileEditCompleted',
  44.             FOSUserEvents::PROFILE_EDIT_INITIALIZE => 'profileEditInitialize',
  45.         ];
  46.     }
  47.     public function profileEditInitialize(GetResponseUserEvent $event): void
  48.     {
  49.         $this->userEmail $event->getUser()->getEmail();
  50.     }
  51.     /**
  52.      * @param FilterUserResponseEvent $event
  53.      * @return void
  54.      */
  55.     public function profileEditCompleted(FilterUserResponseEvent $event): void
  56.     {
  57.         $user $event->getUser();
  58.         if ($user->getEmail() !== $this->userEmail) {
  59.             $this->logoutAfterChangePasswordService->perform();
  60.         }
  61.         $this->backgroundClient->add(new DirectorySaveUserData($user->getId()));
  62.         $this->backgroundClient->runJobs();
  63.     }
  64. }