src/EventSubscriber/EditProfileSubscriber.php line 84

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\Core\Contract\ICompanySelector;
  6. use Harmonizely\Service\Panel\Api\Logout\Contract\ILogoutAfterChangePasswordService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use FOS\UserBundle\FOSUserEvents;
  9. use FOS\UserBundle\Event\FilterUserResponseEvent;
  10. use Harmonizely\Core\BackgroundWork\Contract\IClient;
  11. use Harmonizely\Background\Data\DirectorySaveUserData;
  12. use Harmonizely\Service\RegistrationAbuse\SpamValidationScheduler;
  13. class EditProfileSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var string
  17.      */
  18.     private string $userEmail;
  19.     /**
  20.      * @var IClient
  21.      */
  22.     private IClient $backgroundClient;
  23.     /**
  24.      * @var ILogoutAfterChangePasswordService
  25.      */
  26.     private ILogoutAfterChangePasswordService $logoutAfterChangePasswordService;
  27.     /**
  28.      * @var ICompanySelector
  29.      */
  30.     private ICompanySelector $companySelector;
  31.     /**
  32.      * @var SpamValidationScheduler
  33.      */
  34.     private SpamValidationScheduler $spamValidationScheduler;
  35.     /**
  36.      * @param IClient $backgroundClient
  37.      * @param ILogoutAfterChangePasswordService $logoutAfterChangePasswordService
  38.      * @param ICompanySelector $companySelector
  39.      * @param SpamValidationScheduler $spamValidationScheduler
  40.      */
  41.     public function __construct(
  42.         IClient $backgroundClient,
  43.         ILogoutAfterChangePasswordService $logoutAfterChangePasswordService,
  44.         ICompanySelector $companySelector,
  45.         SpamValidationScheduler $spamValidationScheduler
  46.     )
  47.     {
  48.         $this->backgroundClient $backgroundClient;
  49.         $this->logoutAfterChangePasswordService $logoutAfterChangePasswordService;
  50.         $this->companySelector $companySelector;
  51.         $this->spamValidationScheduler $spamValidationScheduler;
  52.     }
  53.     /**
  54.      * @return string[]
  55.      */
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             FOSUserEvents::PROFILE_EDIT_COMPLETED => 'profileEditCompleted',
  60.             FOSUserEvents::PROFILE_EDIT_INITIALIZE => 'profileEditInitialize',
  61.         ];
  62.     }
  63.     public function profileEditInitialize(GetResponseUserEvent $event): void
  64.     {
  65.         $this->userEmail $event->getUser()->getEmail();
  66.     }
  67.     /**
  68.      * @param FilterUserResponseEvent $event
  69.      * @return void
  70.      */
  71.     public function profileEditCompleted(FilterUserResponseEvent $event): void
  72.     {
  73.         $user $event->getUser();
  74.         if ($user->getEmail() !== $this->userEmail) {
  75.             $this->logoutAfterChangePasswordService->perform();
  76.         }
  77.         $this->backgroundClient->add(new DirectorySaveUserData(
  78.             $user->getId(),
  79.             $this->companySelector->getCompany()
  80.         ));
  81.         $this->backgroundClient->runJobs();
  82.         // Schedule a delayed spam re-check of the just-saved profile (em's Company_SpamValidator::run()).
  83.         $this->spamValidationScheduler->schedule($user->getId());
  84.     }
  85. }