vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Context/CustomerContext.php line 28

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\Context;
  12. use Sylius\Component\Core\Model\ShopUserInterface;
  13. use Sylius\Component\Customer\Context\CustomerContextInterface;
  14. use Sylius\Component\Customer\Model\CustomerInterface as BaseCustomerInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  17. final class CustomerContext implements CustomerContextInterface
  18. {
  19.     public function __construct(private TokenStorageInterface $tokenStorage, private AuthorizationCheckerInterface $authorizationChecker)
  20.     {
  21.     }
  22.     public function getCustomer(): ?BaseCustomerInterface
  23.     {
  24.         if (null === $token $this->tokenStorage->getToken()) {
  25.             return null;
  26.         }
  27.         $user $token->getUser();
  28.         if (
  29.             $user instanceof ShopUserInterface &&
  30.             $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')
  31.         ) {
  32.             return $user->getCustomer();
  33.         }
  34.         return null;
  35.     }
  36. }