custom/plugins/SwagB2bPlatform/components/StoreFrontAuthentication/Framework/AuthenticationService.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\StoreFrontAuthentication\Framework;
  3. use RuntimeException;
  4. use Shopware\B2B\Common\IdValue;
  5. class AuthenticationService
  6. {
  7.     /**
  8.      * @var AuthStorageAdapterInterface
  9.      */
  10.     private $authStorageAdapter;
  11.     /**
  12.      * @var StoreFrontAuthenticationRepository
  13.      */
  14.     private $authenticationRepository;
  15.     /**
  16.      * @var IdentityChainIdentityLoader
  17.      */
  18.     private $identityChainRepository;
  19.     /**
  20.      * @var LoginContextService
  21.      */
  22.     private $loginContextService;
  23.     public function __construct(
  24.         AuthStorageAdapterInterface $authStorageAdapter,
  25.         StoreFrontAuthenticationRepository $authenticationRepository,
  26.         IdentityChainIdentityLoader $identityChainRepository,
  27.         LoginContextService $loginContextService
  28.     ) {
  29.         $this->authStorageAdapter $authStorageAdapter;
  30.         $this->authenticationRepository $authenticationRepository;
  31.         $this->identityChainRepository $identityChainRepository;
  32.         $this->loginContextService $loginContextService;
  33.     }
  34.     public function getIdentity(): Identity
  35.     {
  36.         if (!$this->isAuthenticated()) {
  37.             throw new NotAuthenticatedException('Not authenticated, can not provide a valid identity.');
  38.         }
  39.         return $this->authStorageAdapter->getIdentity();
  40.     }
  41.     public function isAuthenticated(): bool
  42.     {
  43.         return $this->authStorageAdapter->isAuthenticated();
  44.     }
  45.     public function isB2b(): bool
  46.     {
  47.         try {
  48.             $this->getIdentity();
  49.         } catch (NotAuthenticatedException NoIdentitySetException RuntimeException $e) {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54.     public function is($className): bool
  55.     {
  56.         if (!$this->isB2b()) {
  57.             return false;
  58.         }
  59.         return $this->getIdentity() instanceof $className;
  60.     }
  61.     public function getIdentityByAuthId(IdValue $authId): Identity
  62.     {
  63.         $auth $this->authenticationRepository
  64.             ->fetchAuthenticationById($authId);
  65.         return $this->identityChainRepository
  66.             ->fetchIdentityByAuthentication($auth$this->loginContextService);
  67.     }
  68. }