Skip to content

Commit

Permalink
IT-12113 code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
SyedAbdullahShah committed Jan 11, 2019
1 parent d6855ce commit f52dc52
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions Model/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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));
}
}

}

0 comments on commit f52dc52

Please sign in to comment.