cookieFactory = $cookieFactory; $this->storeManager = $storeManager; $this->storeFactory = $storeFactory; $this->cookieLocator = $cookieLocator; $this->cookieRepository = $cookieRepository; } /** * Build cookie item based on user request * * @param RequestInterface $request * @return \Plumrocket\CookieConsent\Model\Cookie */ public function build(RequestInterface $request): Cookie { $storeId = (int) $request->getParam('store', 0); $cookieId = (int) $request->getParam('id'); $store = $this->storeManager->getStore($storeId); $this->storeManager->setCurrentStore($store->getCode()); if ($cookieId) { try { $cookie = $this->cookieRepository->getById($cookieId, $storeId); } catch (NoSuchEntityException $e) { /** @var \Plumrocket\CookieConsent\Model\Cookie $cookie */ $cookie = $this->cookieFactory->create(); } } else { /** @var \Plumrocket\CookieConsent\Model\Cookie $cookie */ $cookie = $this->cookieFactory->create(); } /** @var \Magento\Store\Model\Store $store */ $store = $this->storeFactory->create(); $store->load($storeId); $cookie->setStoreId(null !== $store->getId() ? (int) $store->getId() : null); $this->cookieLocator->setStore($store); $this->cookieLocator->setCookie($cookie); return $cookie; } }