custom/plugins/SwagB2bPlatform/components/StoreFrontAuthentication/BridgePlatform/LoginSubscriber.php line 84

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.     /**
  16.      * @var LoginService
  17.      */
  18.     private $loginService;
  19.     /**
  20.      * @var UserRepository
  21.      */
  22.     private $userRepository;
  23.     /**
  24.      * @var CredentialsBuilderInterface
  25.      */
  26.     private $credentialsBuilder;
  27.     /**
  28.      * @var RequestStack
  29.      */
  30.     private $requestStack;
  31.     /**
  32.      * @var AuthStorageAdapterInterface
  33.      */
  34.     private $authStorageAdapter;
  35.     public function __construct(
  36.         LoginService $loginService,
  37.         UserRepository $userRepository,
  38.         CredentialsBuilderInterface $credentialsBuilder,
  39.         RequestStack $requestStack,
  40.         AuthStorageAdapterInterface $authStorageAdapter
  41.     ) {
  42.         $this->loginService $loginService;
  43.         $this->userRepository $userRepository;
  44.         $this->credentialsBuilder $credentialsBuilder;
  45.         $this->requestStack $requestStack;
  46.         $this->authStorageAdapter $authStorageAdapter;
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             CustomerBeforeLoginEvent::class => ['syncUserData'10],
  52.             CustomerLoginEvent::class => 'storeIdentity',
  53.         ];
  54.     }
  55.     public function syncUserData(CustomerBeforeLoginEvent $event): void
  56.     {
  57.         if ($this->isGuestCustomerRegistration()) {
  58.             return;
  59.         }
  60.         $credentials $this->credentialsBuilder
  61.             ->createCredentialsByEmail($event->getEmail());
  62.         try {
  63.             $userData $this->loginService
  64.                 ->getUserDataBeforeLogin($credentials);
  65.         } catch (NotFoundException $e) {
  66.             return;
  67.         }
  68.         $this->userRepository->syncUser($userData);
  69.     }
  70.     public function storeIdentity(CustomerLoginEvent $event): void
  71.     {
  72.         $customer $event->getCustomer();
  73.         if ($customer->getGuest()) {
  74.             return;
  75.         }
  76.         $credentials $this->credentialsBuilder->createCredentialsByEmail($customer->getEmail());
  77.         try {
  78.             $identity $this->loginService->getIdentityByCredentials($credentials);
  79.         } catch (NotFoundException $e) {
  80.             return;
  81.         }
  82.         $this->checkBillingAddress($identity);
  83.         $this->checkShippingAddress($identity);
  84.         $this->authStorageAdapter->setIdentity($identity);
  85.     }
  86.     /**
  87.      * @protected
  88.      */
  89.     protected function checkBillingAddress(Identity $identity): void
  90.     {
  91.         $billingAddressId $identity->getMainBillingAddress()->id;
  92.         $this->userRepository->checkAddress(
  93.             $billingAddressId,
  94.             AddressEntity::TYPE_BILLING,
  95.             $identity->getOwnershipContext()
  96.         );
  97.     }
  98.     /**
  99.      * @protected
  100.      */
  101.     protected function checkShippingAddress(Identity $identity): void
  102.     {
  103.         $shippingAddressId $identity->getMainShippingAddress()->id;
  104.         $this->userRepository->checkAddress(
  105.             $shippingAddressId,
  106.             AddressEntity::TYPE_SHIPPING,
  107.             $identity->getOwnershipContext()
  108.         );
  109.     }
  110.     /**
  111.      * @internal
  112.      */
  113.     protected function isGuestCustomerRegistration(): bool
  114.     {
  115.         $request $this->requestStack->getMasterRequest();
  116.         return $request && (bool) $request->get('guest'false);
  117.     }
  118. }