apiCredentialsValidatorFactory = $apiCredentialsValidatorFactory; $this->messageManager = $messageManager; $this->amazonCoreHelper = $amazonCoreHelper; $this->jsonCredentials = $jsonCredentials; $this->appConfig = $config; $this->request = $request; } /** * {@inheritdoc} */ public function execute(Observer $observer) { if (!$this->request->getParam('amazon_test_creds')) { return; } $scopeData = $this->getScopeData($observer); $jsonCredentials = $this->amazonCoreHelper->getCredentialsJson($scopeData['scope'], $scopeData['scope_id']); if (!empty($jsonCredentials)) { $this->appConfig->reinit(); $this->jsonCredentials->processCredentialsJson($jsonCredentials, $scopeData); } /** @see \Magento\Config\Model\Config::save() */ $validator = $this->apiCredentialsValidatorFactory->create(); $messageManagerMethod = 'addErrorMessage'; if ($validator->isValid($scopeData['scope_id'], $scopeData['scope'])) { $messageManagerMethod = 'addSuccessMessage'; } foreach ($validator->getMessages() as $message) { $this->messageManager->$messageManagerMethod($message); } } protected function getScopeData($observer) { $scopeData = []; $scopeData['scope'] = 'default'; $scopeData['scope_id'] = null; $website = $observer->getWebsite(); $store = $observer->getStore(); if ($website) { $scopeData['scope'] = ScopeInterface::SCOPE_WEBSITES; $scopeData['scope_id'] = $website; } if ($store) { $scopeData['scope'] = ScopeInterface::SCOPE_STORES; $scopeData['scope_id'] = $store; } return $scopeData; } }