vendor/store.shopware.com/swagb2bplatform/components/StoreFrontAuthentication/BridgePlatform/LoginSubscriber.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\StoreFrontAuthentication\BridgePlatform;
  3. use Shopware\B2B\Address\Framework\AddressEntity;
  4. use Shopware\B2B\Common\Repository\NotFoundException;
  5. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthStorageAdapterInterface;
  6. use Shopware\B2B\StoreFrontAuthentication\Framework\CredentialsBuilderInterface;
  7. use Shopware\B2B\StoreFrontAuthentication\Framework\Identity;
  8. use Shopware\B2B\StoreFrontAuthentication\Framework\LoginService;
  9. use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
  10. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class LoginSubscriber implements EventSubscriberInterface
  14. {
  15.     private LoginService $loginService;
  16.     private UserRepository $userRepository;
  17.     private CredentialsBuilderInterface $credentialsBuilder;
  18.     private RequestStack $requestStack;
  19.     private AuthStorageAdapterInterface $authStorageAdapter;
  20.     public function __construct(
  21.         LoginService $loginService,
  22.         UserRepository $userRepository,
  23.         CredentialsBuilderInterface $credentialsBuilder,
  24.         RequestStack $requestStack,
  25.         AuthStorageAdapterInterface $authStorageAdapter
  26.     ) {
  27.         $this->loginService $loginService;
  28.         $this->userRepository $userRepository;
  29.         $this->credentialsBuilder $credentialsBuilder;
  30.         $this->requestStack $requestStack;
  31.         $this->authStorageAdapter $authStorageAdapter;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CustomerBeforeLoginEvent::class => ['syncUserData'10],
  37.             CustomerLoginEvent::class => 'storeIdentity',
  38.         ];
  39.     }
  40.     public function syncUserData(CustomerBeforeLoginEvent $event): void
  41.     {
  42.         if ($this->isGuestCustomerRegistration()) {
  43.             return;
  44.         }
  45.         $credentials $this->credentialsBuilder
  46.             ->createCredentialsByEmail($event->getEmail());
  47.         try {
  48.             $userData $this->loginService
  49.                 ->getUserDataBeforeLogin($credentials);
  50.         } catch (NotFoundException $e) {
  51.             return;
  52.         }
  53.         $this->userRepository->syncUser($userData);
  54.     }
  55.     public function storeIdentity(CustomerLoginEvent $event): void
  56.     {
  57.         $customer $event->getCustomer();
  58.         if ($customer->getGuest()) {
  59.             return;
  60.         }
  61.         $credentials $this->credentialsBuilder->createCredentialsByEmail($customer->getEmail());
  62.         try {
  63.             $identity $this->loginService->getIdentityByCredentials($credentials);
  64.         } catch (NotFoundException $e) {
  65.             return;
  66.         }
  67.         $this->checkBillingAddress($identity);
  68.         $this->checkShippingAddress($identity);
  69.         $this->authStorageAdapter->setIdentity($identity);
  70.     }
  71.     /**
  72.      * @protected
  73.      */
  74.     protected function checkBillingAddress(Identity $identity): void
  75.     {
  76.         $billingAddressId $identity->getMainBillingAddress()->id;
  77.         $this->userRepository->checkAddress(
  78.             $billingAddressId,
  79.             AddressEntity::TYPE_BILLING,
  80.             $identity->getOwnershipContext()
  81.         );
  82.     }
  83.     /**
  84.      * @protected
  85.      */
  86.     protected function checkShippingAddress(Identity $identity): void
  87.     {
  88.         $shippingAddressId $identity->getMainShippingAddress()->id;
  89.         $this->userRepository->checkAddress(
  90.             $shippingAddressId,
  91.             AddressEntity::TYPE_SHIPPING,
  92.             $identity->getOwnershipContext()
  93.         );
  94.     }
  95.     /**
  96.      * @internal
  97.      */
  98.     protected function isGuestCustomerRegistration(): bool
  99.     {
  100.         $request $this->requestStack->getMasterRequest();
  101.         return $request && (bool) $request->get('guest'false);
  102.     }
  103. }