Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreCookie.php
Go to the documentation of this file.
1 <?php
8 
14 use \InvalidArgumentException;
15 
20 {
24  protected $storeManager;
25 
30 
34  protected $storeRepository;
35 
41  public function __construct(
45  ) {
46  $this->storeManager = $storeManager;
47  $this->storeCookieManager = $storeCookieManager;
48  $this->storeRepository = $storeRepository;
49  }
50 
59  public function beforeDispatch(
60  \Magento\Framework\App\FrontController $subject,
61  \Magento\Framework\App\RequestInterface $request
62  ) {
63  $storeCodeFromCookie = $this->storeCookieManager->getStoreCodeFromCookie();
64  if ($storeCodeFromCookie) {
65  try {
66  $this->storeRepository->getActiveStoreByCode($storeCodeFromCookie);
67  } catch (StoreIsInactiveException $e) {
68  $this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
69  } catch (NoSuchEntityException $e) {
70  $this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
71  } catch (InvalidArgumentException $e) {
72  $this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
73  }
74  }
75  }
76 }