cookieFactory = $cookieFactory; $this->searchResultsFactory = $searchResultsFactory; $this->resourceModel = $resourceModel; $this->getCookieIdByName = $getCookieIdByName; $this->collectionFactory = $collectionFactory; $this->collectionProcessor = $collectionProcessor; } /** * @inheritDoc */ public function save(CookieInterface $cookie): CookieInterface { try { $this->clearLocalCache($cookie); $this->resourceModel->save($cookie); } catch (AttributeException $exception) { throw InputException::invalidFieldValue( $exception->getAttributeCode(), $cookie->getData($exception->getAttributeCode()), $exception ); } catch (\Exception $e) { throw new CouldNotSaveException( __('The cookie was unable to be saved. Please try again.'), $e ); } $this->clearLocalCache($cookie); return $this->getById($cookie->getId(), $cookie->getStoreId()); } /** * @inheritDoc */ public function get(string $name, ?int $storeId = null, bool $forceReload = false): CookieInterface { if (! isset($this->instancesByName[$name][$storeId]) || $forceReload) { $cookieId = $this->getCookieIdByName->execute($name, $forceReload); if (! $cookieId) { throw new NoSuchEntityException( __("The cookie that was requested doesn't exist. Verify the cookie and try again.") ); } return $this->getById($cookieId); } return $this->instancesByName[$name][$storeId]; } /** * @inheritDoc */ public function getById(int $cookieId, ?int $storeId = null, bool $forceReload = false): CookieInterface { if (! isset($this->instancesById[$cookieId][$storeId]) || $forceReload) { /** @var \Plumrocket\CookieConsent\Model\Cookie $cookie */ $cookie = $this->cookieFactory->create(); if ($storeId !== null) { $cookie->setStoreId($storeId); } $cookie->load($cookieId); if (! $cookie->getId()) { throw new NoSuchEntityException( __("The cookie that was requested doesn't exist. Verify the cookie and try again.") ); } $this->saveToLocalCache($cookie, $storeId); } return $this->instancesById[$cookieId][$storeId]; } /** * @inheritDoc */ public function delete(CookieInterface $cookie): bool { $cookieId = $cookie->getId(); $cookieKey = $cookie->getName(); try { $this->clearLocalCache($cookie); $this->resourceModel->delete($cookie); } catch (ValidatorException $e) { throw new CouldNotSaveException(__($e->getMessage())); } catch (\Exception $e) { throw new StateException( __('The "%1" cookie couldn\'t be removed.', $cookieId) ); } unset($this->instancesById[$cookieId], $this->instancesByName[$cookieKey]); return true; } /** * @inheritDoc */ public function deleteById(int $id): bool { $cookie = $this->getById($id); return $this->delete($cookie); } /** * @inheritDoc */ public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface { /** @var \Plumrocket\CookieConsent\Model\ResourceModel\Cookie\Collection $collection */ $collection = $this->collectionFactory->create(); $collection->addAttributeToSelect('*'); $this->collectionProcessor->process($searchCriteria, $collection); /** @var \Plumrocket\CookieConsent\Api\Data\CookieSearchResultsInterface $searchResult */ $searchResult = $this->searchResultsFactory->create(); $searchResult->setSearchCriteria($searchCriteria); $searchResult->setItems($collection->getItems()); $searchResult->setTotalCount($collection->getSize()); return $searchResult; } /** * Save loaded cookie model. * * @param \Plumrocket\CookieConsent\Api\Data\CookieInterface $cookie * @param int|null $storeId */ private function saveToLocalCache(CookieInterface $cookie, ?int $storeId = null): void { $this->instancesById[$cookie->getId()][$storeId] = $cookie; $this->instancesByName[$cookie->getName()][$storeId] = $cookie; } /** * Clear loaded models. * * @param \Plumrocket\CookieConsent\Api\Data\CookieInterface $cookie */ private function clearLocalCache(CookieInterface $cookie): void { unset($this->instancesById[$cookie->getId()], $this->instancesByName[$cookie->getKey()]); } }