getFieldsetDataValue('api_url'); $apiKey = $this->getFieldsetDataValue('api_key'); $apiVersion = $this->getValue(); $api = new ApiClient($apiUrl, $apiKey, $apiVersion); $this->validateApiVersion($api, $apiVersion); parent::beforeSave(); } /** * Call after save api version * * @return void */ public function afterSave() { return parent::afterSave(); } /** * Validate selected api version * * @param ApiClient $api * @param string $apiVersion * * @throws \Magento\Framework\Exception\ValidatorException * * @return void */ protected function validateApiVersion(ApiClient $api, $apiVersion) { $apiVersions = [ 'v4' => '4.0', 'v5' => '5.0' ]; $response = $api->availableVersions(); if ($response->isSuccessful()) { $availableVersions = $response['versions']; } else { throw new \Magento\Framework\Exception\ValidatorException(__('Invalid CRM url or api key')); } if (isset($availableVersions)) { if (in_array($apiVersions[$apiVersion], $availableVersions)) { $this->setValue($this->getValue()); } else { throw new \Magento\Framework\Exception\ValidatorException(__('Selected api version forbidden')); } } } }