vendor/store.shopware.com/swagb2bplatform/components/StoreFrontAuthentication/BridgePlatform/SalesChannelContextAuthStorageAdapter.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\StoreFrontAuthentication\BridgePlatform;
  3. use Shopware\B2B\Shop\BridgePlatform\ContextProvider;
  4. use Shopware\B2B\Shop\Framework\StorageInterface;
  5. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationIdentityLoaderInterface;
  6. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthStorageAdapterInterface;
  7. use Shopware\B2B\StoreFrontAuthentication\Framework\Identity;
  8. use Shopware\B2B\StoreFrontAuthentication\Framework\LoginContextService;
  9. use Shopware\B2B\StoreFrontAuthentication\Framework\NoIdentitySetException;
  10. use Shopware\B2B\StoreFrontAuthentication\Framework\StoreFrontAuthenticationRepository;
  11. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class SalesChannelContextAuthStorageAdapter implements AuthStorageAdapterInterfaceEventSubscriberInterface
  14. {
  15.     public const IDENTITY_ID_KEY 'b2b_front_auth_identity_id';
  16.     private ContextProvider $contextProvider;
  17.     private StoreFrontAuthenticationRepository $storefrontAuthRepository;
  18.     private LoginContextService $loginContextService;
  19.     private AuthenticationIdentityLoaderInterface $chainIdentityLoader;
  20.     private ?Identity $identity null;
  21.     private bool $isLoggedIn false;
  22.     private StorageInterface $storage;
  23.     public function __construct(
  24.         ContextProvider $contextProvider,
  25.         StoreFrontAuthenticationRepository $storefrontAuthRepository,
  26.         LoginContextService $loginContextService,
  27.         AuthenticationIdentityLoaderInterface $chainIdentityLoader,
  28.         StorageInterface $storage
  29.     ) {
  30.         $this->contextProvider $contextProvider;
  31.         $this->storefrontAuthRepository $storefrontAuthRepository;
  32.         $this->loginContextService $loginContextService;
  33.         $this->chainIdentityLoader $chainIdentityLoader;
  34.         $this->storage $storage;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             CustomerLogoutEvent::class => 'unsetIdentity',
  40.         ];
  41.     }
  42.     public function unsetIdentity(): void
  43.     {
  44.         $this->identity null;
  45.     }
  46.     public function setIdentity(Identity $identity): void
  47.     {
  48.         $this->identity $identity;
  49.         $this->storage->set(self::IDENTITY_ID_KEY, new B2bIdentityIdStruct($identity->getAuthId()));
  50.     }
  51.     public function getIdentity(): Identity
  52.     {
  53.         if ($this->identity !== null) {
  54.             return $this->identity;
  55.         }
  56.         $identityIdStruct $this->storage->get(self::IDENTITY_ID_KEY);
  57.         if (!$identityIdStruct) {
  58.             throw new NoIdentitySetException('Sales channel context does not have a stored identity');
  59.         }
  60.         $authEntity $this->storefrontAuthRepository
  61.             ->fetchAuthenticationById($identityIdStruct->getIdentityId());
  62.         return $this->identity $this->chainIdentityLoader
  63.             ->fetchIdentityByAuthentication($authEntity$this->loginContextService);
  64.     }
  65.     public function isAuthenticated(): bool
  66.     {
  67.         if ($this->isLoggedIn === true) {
  68.             return $this->isLoggedIn;
  69.         }
  70.         $salesChannelContext $this->contextProvider->getSalesChannelContext();
  71.         return $this->isLoggedIn $salesChannelContext->getCustomer() !== null;
  72.     }
  73. }