jsonSerializer = $jsonSerializer; $this->config = $config; $this->logger = $logger; $this->createSettingsFromParam = $createSettingsFromParam; } /** * Execute controller. */ public function execute() { /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); $consentDetailJson = $this->getRequest()->getContent(); if (! $consentDetailJson || ! $this->config->isModuleEnabled()) { $resultJson->setHttpResponseCode(404); $response = ['message' => __('Not found.')]; return $resultJson->setData($response); } try { $consentDetail = $this->jsonSerializer->unserialize($consentDetailJson); $acceptedKeys = $consentDetail['acceptedKeys'] ?: []; $settings = $this->createSettingsFromParam->execute($acceptedKeys); $this->_eventManager->dispatch( 'pr_cookie_consent_update', ['settings' => $settings] ); $response = ['message' => __('Log successfully added.')]; } catch (LocalizedException $e) { $resultJson->setHttpResponseCode(400); $response = [ 'message' => $e->getMessage() ]; } catch (\Exception $e) { $this->logger->error($e->getMessage()); $resultJson->setHttpResponseCode(400); $response = ['message' => __('Something went wrong.')]; } return $resultJson->setData($response); } }