custom/plugins/UandiEfbLoginById/src/Subscriber/AccountRecoverySubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\UandiEfbLoginById\Subscriber;
  3. use Exception;
  4. use Monolog\Logger;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Uandi\UandiEfbLoginById\Service\EmailSuffixService;
  9. class AccountRecoverySubscriber implements EventSubscriberInterface
  10. {
  11.     private EmailSuffixService $emailSuffixService;
  12.     private Logger $logger;
  13.     /**
  14.      * @param EmailSuffixService $emailSuffixService
  15.      * @param Logger $logger
  16.      */
  17.     public function __construct(
  18.         EmailSuffixService $emailSuffixService,
  19.         Logger $logger
  20.     ) {
  21.         $this->emailSuffixService $emailSuffixService;
  22.         $this->logger $logger;
  23.     }
  24.     /**
  25.      * @return string[]
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             KernelEvents::CONTROLLER => 'onBeforeGenerateAccountRecovery'
  31.         ];
  32.     }
  33.     /**
  34.      * Convert customer id to email and perform standard recovery password process
  35.      *
  36.      * @param ControllerEvent $event
  37.      */
  38.     public function onBeforeGenerateAccountRecovery(ControllerEvent $event): void
  39.     {
  40.         $route $event->getRequest()->attributes->get('_route') ?? '';
  41.         if ($route === 'frontend.account.recover.request') {
  42.             try {
  43.                 $request $event->getRequest();
  44.                 //Get email suffix
  45.                 $emailSuffix $this->emailSuffixService->getBySalesChannelId(
  46.                     $request->attributes->get('sw-sales-channel-id''')
  47.                 );
  48.                 //Get email
  49.                 $customerExternalId $request->request->get('email') ?? null;
  50.                 if (is_array($customerExternalId)) {
  51.                     $customerExternalId current($customerExternalId);
  52.                 }
  53.                 //Check if is not an email and add email suffix
  54.                 if (strpos($customerExternalId'@') === false && !empty($customerExternalId)) {
  55.                     //Update username to use as an email
  56.                     $request->request->set('email', ['email' => $customerExternalId $emailSuffix]);
  57.                 }
  58.             } catch (Exception $exception) {
  59.                 $this->logger->error(
  60.                     '[onBeforeGenerateAccountRecovery] It was not possible to get username. '
  61.                     $exception->getMessage()
  62.                     );
  63.             }
  64.         }
  65.     }
  66. }