src/EventSubscriber/UserSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Email\Transactional\AccountPasswordResetEmail;
  4. use App\Email\Transactional\FakeLoginAccountActivationEmail;
  5. use App\Email\Transactional\RegisteredAccountActivationEmail;
  6. use App\Email\Transactional\RegisteredAccountConfirmationUserEmail;
  7. use App\Event\Account\AccountEnrollmentEvent;
  8. use App\Event\NotFullRegisteredUserEvent;
  9. use App\Event\PasswordChangedEvent;
  10. use App\Event\ReceivePasswordEvent;
  11. use App\Event\RegisteredSuccessUserEvent;
  12. use App\Security\LoginFormAuthenticator;
  13. use App\Service\Mailer;
  14. use App\Event\RegisteredUserEvent;
  15. use App\Service\UserService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  19. class UserSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         private Mailer $mailer,
  23.         private UserService $userService,
  24.         private GuardAuthenticatorHandler $guardHandler,
  25.         private LoginFormAuthenticator $authenticator,
  26.         private RequestStack $requestStack
  27.     ) {}
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             RegisteredUserEvent::NAME => 'onUserRegister',
  32.             RegisteredSuccessUserEvent::NAME => 'onUserRegisterSuccess',
  33.             NotFullRegisteredUserEvent::NAME => 'onUserNotFullRegister',
  34.             ReceivePasswordEvent::NAME => 'onUserReceivePassword',
  35.             PasswordChangedEvent::NAME => 'onUserPasswordChanged',
  36.             AccountEnrollmentEvent::NAME => 'onAccountEnrollment',
  37.         ];
  38.     }
  39.     public function onUserReceivePassword(ReceivePasswordEvent $receivePasswordEvent): void
  40.     {
  41.         $this->mailer->send(new AccountPasswordResetEmail($receivePasswordEvent->getRegisteredUser()));
  42.     }
  43.     public function onUserPasswordChanged(PasswordChangedEvent $passwordChangedEvent): void
  44.     {
  45.         $this->guardHandler->authenticateUserAndHandleSuccess($passwordChangedEvent->getRegisteredUser(), $this->requestStack->getCurrentRequest(), $this->authenticator'main');
  46.     }
  47.     public function onUserRegister(RegisteredUserEvent $registeredUserEvent): void
  48.     {
  49.         $this->mailer->send(new RegisteredAccountActivationEmail($registeredUserEvent->getRegisteredUser()));
  50.     }
  51.     public function onUserRegisterSuccess(RegisteredSuccessUserEvent $registeredUserEvent): void
  52.     {
  53.         $this->mailer->send(new RegisteredAccountConfirmationUserEmail($registeredUserEvent->getRegisteredUser()));
  54.     }
  55.     public function onUserNotFullRegister(NotFullRegisteredUserEvent $registeredUserEvent): void
  56.     {
  57.         $this->mailer->send(new FakeLoginAccountActivationEmail($registeredUserEvent->getRegisteredUser()));
  58.     }
  59.     public function onAccountEnrollment(AccountEnrollmentEvent $accountEnrollmentEvent): void
  60.     {
  61.         $this->userService->checkAccountBalanceNormal($accountEnrollmentEvent->getEnrollment()->getAccount());
  62.     }
  63. }