custom/plugins/SwagB2bPlatform/components/StoreFrontAuthentication/BridgePlatform/SalesChannelContextAuthStorageAdapter.php line 106

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.     /**
  17.      * @var ContextProvider
  18.      */
  19.     private $contextProvider;
  20.     /**
  21.      * @var StoreFrontAuthenticationRepository
  22.      */
  23.     private $storefrontAuthRepository;
  24.     /**
  25.      * @var LoginContextService
  26.      */
  27.     private $loginContextService;
  28.     /**
  29.      * @var AuthenticationIdentityLoaderInterface
  30.      */
  31.     private $chainIdentityLoader;
  32.     /**
  33.      * @var Identity
  34.      */
  35.     private $identity;
  36.     /**
  37.      * @var bool
  38.      */
  39.     private $isLoggedIn;
  40.     /**
  41.      * @var StorageInterface
  42.      */
  43.     private $storage;
  44.     public function __construct(
  45.         ContextProvider $contextProvider,
  46.         StoreFrontAuthenticationRepository $storefrontAuthRepository,
  47.         LoginContextService $loginContextService,
  48.         AuthenticationIdentityLoaderInterface $chainIdentityLoader,
  49.         StorageInterface $storage
  50.     ) {
  51.         $this->contextProvider $contextProvider;
  52.         $this->storefrontAuthRepository $storefrontAuthRepository;
  53.         $this->loginContextService $loginContextService;
  54.         $this->chainIdentityLoader $chainIdentityLoader;
  55.         $this->storage $storage;
  56.     }
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         return [
  60.             CustomerLogoutEvent::class => 'unsetIdentity',
  61.         ];
  62.     }
  63.     public function unsetIdentity(): void
  64.     {
  65.         $this->identity null;
  66.     }
  67.     public function setIdentity(Identity $identity): void
  68.     {
  69.         $this->identity $identity;
  70.         $this->storage->set(self::IDENTITY_ID_KEY, new B2bIdentityIdStruct($identity->getAuthId()));
  71.     }
  72.     public function getIdentity(): Identity
  73.     {
  74.         if ($this->identity) {
  75.             return $this->identity;
  76.         }
  77.         $identityIdStruct $this->storage->get(self::IDENTITY_ID_KEY);
  78.         if (!$identityIdStruct) {
  79.             throw new NoIdentitySetException('Sales channel context does not have a stored identity');
  80.         }
  81.         $authEntity $this->storefrontAuthRepository
  82.             ->fetchAuthenticationById($identityIdStruct->getIdentityId());
  83.         return $this->identity $this->chainIdentityLoader
  84.             ->fetchIdentityByAuthentication($authEntity$this->loginContextService);
  85.     }
  86.     public function isAuthenticated(): bool
  87.     {
  88.         if ($this->isLoggedIn) {
  89.             return $this->isLoggedIn;
  90.         }
  91.         $salesChannelContext $this->contextProvider->getSalesChannelContext();
  92.         return $this->isLoggedIn = (bool) $salesChannelContext->getCustomer();
  93.     }
  94. }