consentLogFactory = $consentLogFactory; $this->storeManager = $storeManager; $this->remoteAddress = $remoteAddress; $this->dateTime = $dateTime; $this->currentCustomer = $currentCustomer; $this->consentLogResource = $consentLogResource; $this->httpHeader = $httpHeader; $this->redirect = $redirect; } /** * @param array $settings * @return \Plumrocket\CookieConsent\Api\Data\ConsentLogInterface * @throws \Magento\Framework\Exception\AlreadyExistsException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function log(array $settings): ConsentLogInterface { /** @var ConsentLogInterface|Log $consentLog */ $consentLog = $this->consentLogFactory->create(); $consentLog->setCustomerId((int) $this->currentCustomer->getCustomerId()); $consentLog->setSettings($settings); $consentLog->setWebsiteId((int) $this->storeManager->getStore()->getWebsiteId()); $consentLog->setIpAddress($this->getAnonymizedIp()); $consentLog->setUserAgent($this->httpHeader->getHttpUserAgent()); $consentLog->setUrl($this->redirect->getRefererUrl()); $consentLog->setCreatedAt($this->getFormattedGmtDateTime()); $this->consentLogResource->save($consentLog); return $consentLog; } /** * Get the date in the proper format * * @return string */ private function getFormattedGmtDateTime(): string { return (string) date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()); } /** * Anonymize customer Ip * * @return string */ private function getAnonymizedIp(): string { $realIp = $this->remoteAddress->getRemoteAddress(); $ipPieces = explode('.', $realIp); end($ipPieces); $ipPieces[key($ipPieces)] = '000'; return implode('.', $ipPieces); } }