vendor/shopware/core/Checkout/Customer/SalesChannel/LogoutRoute.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\SalesChannel;
  3. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  8. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  9. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  10. use Shopware\Core\Framework\Routing\Annotation\Since;
  11. use Shopware\Core\Framework\Util\Random;
  12. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  13. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  14. use Shopware\Core\System\SalesChannel\ContextTokenResponse;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20.  * @Route(defaults={"_routeScope"={"store-api"}})
  21.  */
  22. #[Package('customer-order')]
  23. class LogoutRoute extends AbstractLogoutRoute
  24. {
  25.     /**
  26.      * @var SalesChannelContextPersister
  27.      */
  28.     private $contextPersister;
  29.     /**
  30.      * @var EventDispatcherInterface
  31.      */
  32.     private $eventDispatcher;
  33.     /**
  34.      * @var SystemConfigService
  35.      */
  36.     private $systemConfig;
  37.     /**
  38.      * @var CartService
  39.      */
  40.     private $cartService;
  41.     /**
  42.      * @internal
  43.      */
  44.     public function __construct(
  45.         SalesChannelContextPersister $contextPersister,
  46.         EventDispatcherInterface $eventDispatcher,
  47.         SystemConfigService $systemConfig,
  48.         CartService $cartService
  49.     ) {
  50.         $this->contextPersister $contextPersister;
  51.         $this->eventDispatcher $eventDispatcher;
  52.         $this->systemConfig $systemConfig;
  53.         $this->cartService $cartService;
  54.     }
  55.     public function getDecorated(): AbstractLogoutRoute
  56.     {
  57.         throw new DecorationPatternException(self::class);
  58.     }
  59.     /**
  60.      * @Since("6.2.0.0")
  61.      * @Route(path="/store-api/account/logout", name="store-api.account.logout", methods={"POST"}, defaults={"_loginRequired"=true, "_loginRequiredAllowGuest"=true})
  62.      */
  63.     public function logout(SalesChannelContext $contextRequestDataBag $data): ContextTokenResponse
  64.     {
  65.         /** @var CustomerEntity $customer */
  66.         $customer $context->getCustomer();
  67.         if ($this->shouldDelete($context)) {
  68.             $this->cartService->deleteCart($context);
  69.             $this->contextPersister->delete($context->getToken(), $context->getSalesChannelId());
  70.             $event = new CustomerLogoutEvent($context$customer);
  71.             $this->eventDispatcher->dispatch($event);
  72.             return new ContextTokenResponse($context->getToken());
  73.         }
  74.         $newToken Random::getAlphanumericString(32);
  75.         if ((bool) $data->get('replace-token')) {
  76.             $newToken $this->contextPersister->replace($context->getToken(), $context);
  77.         }
  78.         $context->assign([
  79.             'token' => $newToken,
  80.         ]);
  81.         $event = new CustomerLogoutEvent($context$customer);
  82.         $this->eventDispatcher->dispatch($event);
  83.         return new ContextTokenResponse($context->getToken());
  84.     }
  85.     private function shouldDelete(SalesChannelContext $context): bool
  86.     {
  87.         $config $this->systemConfig->get('core.loginRegistration.invalidateSessionOnLogOut'$context->getSalesChannelId());
  88.         if ($config) {
  89.             return true;
  90.         }
  91.         if ($context->getCustomer() === null) {
  92.             return true;
  93.         }
  94.         return $context->getCustomer()->getGuest();
  95.     }
  96. }