From f52dc52ef02ff54541d37866759392646d29075c Mon Sep 17 00:00:00 2001 From: Syed Abdullah Date: Fri, 11 Jan 2019 13:03:05 +0500 Subject: [PATCH] IT-12113 code optimization --- Model/Validate.php | 66 ++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/Model/Validate.php b/Model/Validate.php index d12c362..8e27530 100644 --- a/Model/Validate.php +++ b/Model/Validate.php @@ -33,6 +33,8 @@ class Validate extends \Magento\Framework\App\Config\Value const ACCESS_DENIED_RESPONSE = 'Access denied'; const SEGMENT_STATUS_ACTIVE = "active"; const SEGMENT_STATUS_INACTIVE = "inactive"; + const HTTP_METHOD_GET = 'GET'; + const HTTP_METHOD_PUT = 'PUT'; const HTTP_STATUS_OK = 200; /** @@ -86,22 +88,22 @@ public function beforeSave() $postData = $this->request->getPostValue(); $postValues = $postData['groups']['general']['fields']; $shopId = $postValues['shop_id']['value']; - $shopPw = $postValues['shop_password']['value']; + $shopPassword = $postValues['shop_password']['value']; $smartCheck = $postValues['smart_check']['value']; - $server_output = $this->verifyAccount($shopId, $shopPw); + $server_output = $this->verifyAccount($shopId, $shopPassword); if ($server_output == null || $server_output == self::ACCESS_DENIED_RESPONSE) { $this->setValue(0); $errorMsg = 'Access denied, Invalid Shop ID or Password'; $phrase = new Phrase($errorMsg); throw new LocalizedException($phrase); } else { - $customerSegment = $this->getSrrCustomerSegment($shopId, $shopPw); + $customerSegment = $this->getSrrCustomerSegment($shopId, $shopPassword); if ($customerSegment !== false && is_array($customerSegment)) { - $this->activateSrrCustomerSegment($shopId, $shopPw, $customerSegment["id"]); + $this->activateSrrCustomerSegment($shopId, $shopPassword, $customerSegment["id"]); } - $this->updateSmartCheck($shopId, $shopPw, $smartCheck); + $this->updateSmartCheck($shopId, $shopPassword, $smartCheck); return parent::beforeSave(); } @@ -111,12 +113,12 @@ public function beforeSave() * Validates eKomi account credentials * * @param string $shopId - * @param string $shopPw + * @param string $shopPassword * @return string */ - private function verifyAccount($shopId, $shopPw) + private function verifyAccount($shopId, $shopPassword) { - $apiUrl = self::GET_SETTINGS_API_URL . "?auth=" . $shopId . "|" . $shopPw . + $apiUrl = self::GET_SETTINGS_API_URL . "?auth=" . $shopId . "|" . $shopPassword . "&version=cust-1.0.0&type=request&charset=iso"; $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); @@ -131,13 +133,11 @@ private function verifyAccount($shopId, $shopPw) * @param string $shopPassword * @return bool|array */ - public function getSrrCustomerSegment($shopId, $shopPw) + public function getSrrCustomerSegment($shopId, $shopPassword) { $apiUrl = self::CUSTOMER_SEGMENT_URL . '?status=' . self::SEGMENT_STATUS_INACTIVE; - $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); - $this->curl->addHeader('shop-id', $shopId); - $this->curl->addHeader('interface-password', $shopPw); + $this->configureCurl($shopId, $shopPassword, self::HTTP_METHOD_GET); $this->curl->get($apiUrl); $responseJson = $this->curl->getBody(); @@ -179,14 +179,10 @@ public function getDefaultSegmentKey($customerSegments) public function activateSrrCustomerSegment($shopId, $shopPassword, $segmentId) { $apiUrl = self::CUSTOMER_SEGMENT_URL . '/' . $segmentId . '?status=' . self::SEGMENT_STATUS_ACTIVE; - - $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); - $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'PUT'); - $this->curl->addHeader('shop-id', $shopId); - $this->curl->addHeader('interface-password', $shopPassword); + $this->configureCurl($shopId, $shopPassword, self::HTTP_METHOD_PUT); $this->curl->post( $apiUrl, - ['status' => self::SEGMENT_STATUS_ACTIVE] + [] ); $responseJson = $this->curl->getBody(); @@ -203,18 +199,38 @@ public function activateSrrCustomerSegment($shopId, $shopPassword, $segmentId) */ private function updateSmartCheck($shopId, $shopPassword, $smartCheckOn) { - $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); - $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'PUT'); - $this->curl->setOption(CURLOPT_POSTFIELDS, json_encode(['smartcheck_on' => (bool)$smartCheckOn])); - $this->curl->addHeader('shop-id', $shopId); - $this->curl->addHeader('interface-password', $shopPassword); + $this->configureCurl( + $shopId, + $shopPassword, + self::HTTP_METHOD_PUT, + ['smartcheck_on' => (bool)$smartCheckOn] + ); $this->curl->post( self::SMART_CHECK_API_URL, json_encode(['smartcheck_on' => $smartCheckOn]) ); - $server_output = $this->curl->getBody(); + $response = $this->curl->getBody(); - return $server_output; + return $response; + } + + /** + * @param string $shopId + * @param string $shopPassword + * @param string $httpMethod + * @param array $postFields + */ + private function configureCurl($shopId, $shopPassword, $httpMethod = self::HTTP_METHOD_GET, $postFields = null) + { + $this->curl->addHeader('shop-id', $shopId); + $this->curl->addHeader('interface-password', $shopPassword); + $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); + if ($httpMethod === self::HTTP_METHOD_PUT) { + $this->curl->setOption(CURLOPT_CUSTOMREQUEST, self::HTTP_METHOD_PUT); + } + if ($postFields !== null && is_array($postFields)) { + $this->curl->setOption(CURLOPT_POSTFIELDS, json_encode($postFields)); + } } } \ No newline at end of file