Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ManageStoreCookie.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Framework\App\Http\Context as HttpContext;
16 
21 {
25  private $storeCookieManager;
26 
30  private $httpContext;
31 
35  private $storeManager;
36 
42  public function __construct(
43  StoreCookieManagerInterface $storeCookieManager,
44  HttpContext $httpContext,
45  StoreManagerInterface $storeManager
46  ) {
47  $this->storeCookieManager = $storeCookieManager;
48  $this->httpContext = $httpContext;
49  $this->storeManager = $storeManager;
50  }
51 
58  public function switch(StoreInterface $fromStore, StoreInterface $targetStore, string $redirectUrl): string
59  {
60  $defaultStoreView = $this->storeManager->getDefaultStoreView();
61  if ($defaultStoreView !== null) {
62  if ($defaultStoreView->getId() === $targetStore->getId()) {
63  $this->storeCookieManager->deleteStoreCookie($targetStore);
64  } else {
65  $this->httpContext->setValue(Store::ENTITY, $targetStore->getCode(), $defaultStoreView->getCode());
66  $this->storeCookieManager->setStoreCookie($targetStore);
67  }
68  }
69 
70  return $redirectUrl;
71  }
72 }
$storeManager