Skip to content

Commit

Permalink
Merge pull request #70 from sailthru/IN-1414
Browse files Browse the repository at this point in the history
[IN-1414] Fix 500 on Re-order with multiple items
  • Loading branch information
lindsaycooper committed Jul 13, 2020
2 parents fd11e67 + 725b6cd commit 28bfa64
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 199 deletions.
8 changes: 3 additions & 5 deletions Console/Command/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Sailthru\MageSail\Console\Command;

use Magento\Catalog\Api\ProductRepositoryInterface\Proxy as ProductRepositoryInterface;
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Helper\Image;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\Collection\Proxy as Collection;
Expand Down Expand Up @@ -98,8 +96,8 @@ protected function execute(
$storeName = $this->storeManager->getStore($storeId)->getName();
$output->writeln("Checking {$this->productCollection->getSize()} products to import for Store #$storeId $storeName");

$sailClient = $this->clientManager->getClient(true, $storeId);
$sailClient->_eventType = $this::EVENT_NAME;
$client = $this->clientManager->getClient($storeId);
$client->_eventType = $this::EVENT_NAME;

$actionId = time();
$checkedProducts = 0;
Expand All @@ -122,7 +120,7 @@ protected function execute(
if ($status == Status::STATUS_ENABLED and $payload = $this->productIntercept->getProductData($product, $storeId)) {
$payload['integration_action'] = $this::EVENT_NAME;
$payload['integration_action_id'] = $actionId;
$sailClient->apiPost('content', $payload);
$client->apiPost('content', $payload);
$syncedProducts++;
} else {
$skippedProducts[] = $product;
Expand Down
54 changes: 32 additions & 22 deletions Helper/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@

class ClientManager extends AbstractHelper
{
const XML_API_KEY = 'magesail_config/service/api_key';
const XML_API_SECRET = 'magesail_config/service/secret_key';
const XML_JS_ENABLED = 'magesail_config/js/enabled';
const XML_JS_CUSTOMER_ID = 'magesail_config/js/customer_id';
const API_SUCCESS_MESSAGE = 'Successfully Validated!';

const XML_API_KEY = "magesail_config/service/api_key";
const XML_API_SECRET = "magesail_config/service/secret_key";
const XML_JS_ENABLED = "magesail_config/js/enabled";
const XML_JS_CUSTOMER_ID = "magesail_config/js/customer_id";
const API_SUCCESS_MESSAGE = "Successfully Validated!";

/** @var MageClient */
protected $client;
/** @var MageClient[] */
protected $clientsRegistry = [];

/** @var ModuleListInterface */
private $moduleList;

/** @var array */
private $settings;

/** @var string */
private $customerId;

public function __construct(
Context $context,
StoreManager $storeManager,
Expand All @@ -54,26 +50,38 @@ public function __construct(
$this->moduleList = $moduleList;
}

public function initClient($storeId = null)
public function getClient($storeId = null)
{
$apiKey = $this->getSettingsVal(self::XML_API_KEY, $storeId);
$apiSecret = $this->getSettingsVal(self::XML_API_SECRET, $storeId);
$this->client = new MageClient($apiKey, $apiSecret, $this->getSetupVersion(), $this->logger, $this->scopeResolver);
$cacheKey = (int)$storeId;
if (!isset($this->clientsRegistry[$cacheKey])) {
$this->clientsRegistry[$cacheKey] = $this->initClient($storeId);
}

return $this->clientsRegistry[$cacheKey];
}

public function getClient($update=false, $storeId = null)
/**
* @param null|int|string $storeId
*
* @return MageClient
*/
public function initClient($storeId = null)
{
if ($update or !$this->client) {
$this->initClient($storeId);
}
return $this->client;
return new MageClient(
$this->getSettingsVal(self::XML_API_KEY, $storeId),
$this->getSettingsVal(self::XML_API_SECRET, $storeId),
$this->getSetupVersion(),
$this->logger,
$this->scopeResolver
);
}

public function getSettings($update = false)
{
if ($update || empty($this->settings)) {
$this->settings = $this->getClient()->getSettings();
}

return $this->settings;
}

Expand All @@ -96,7 +104,7 @@ public function apiValidate()
{
try {
$result = $this->getSettings(true);
if (!array_key_exists("error", $result)) {
if (!array_key_exists('error', $result)) {
return [1, self::API_SUCCESS_MESSAGE];
} else {
return 0;
Expand All @@ -109,15 +117,17 @@ public function apiValidate()
public function isValid()
{
$check = $this->apiValidate();

return $check[0];
}

private function getSetupVersion()
{
$moduleData = $this->moduleList->getOne('Sailthru_MageSail');

return isset($moduleData['setup_version'])
? $moduleData['setup_version']
: "";
: '';
}

}
73 changes: 39 additions & 34 deletions Helper/ScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Website;

class ScopeResolver extends AbstractHelper {
class ScopeResolver extends AbstractHelper
{

/** @var State */
/** @var State */
protected $appState;

/** @var WebapiRequest */
/** @var WebapiRequest */
protected $webapiRequest;

/** @var StoreManagerInterface */
/** @var StoreManagerInterface */
protected $storeManager;

/** @var OrderRepositoryInterface */
/** @var OrderRepositoryInterface */
protected $orderRepo;

/** @var ShipmentRepositoryInterface */
/** @var ShipmentRepositoryInterface */
protected $shipmentRepo;

private static $ADMIN_AREAS = [
Expand Down Expand Up @@ -61,7 +62,6 @@ public function getScope()
{
if ($store = $this->getStore()) {
return [$store->getCode(), ScopeInterface::SCOPE_STORE];

} elseif ($website = $this->getWebsite()) {
return [$website->getCode(), ScopeInterface::SCOPE_WEBSITE];
}
Expand All @@ -71,22 +71,20 @@ public function getScope()

/**
* @return StoreInterface|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getStore()
{
if ($this->isFrontendArea()){
$this->_logger->info("frontend");
return $this->_getStore();
if ($this->isFrontendArea()) {

return $this->_getStore();
} elseif ($storeId = $this->getRequestStoreScope()) {
$this->_logger->info("store request");
return $this->_getStore($storeId);

} elseif ($this->isSalesRequest()) {
$this->_logger->info("sales request");
$storeId = $this->getStoreIdFromSalesRequest();
return $this->_getStore($storeId);
} elseif ($this->isSalesRequest()) {
$storeId = $this->getStoreIdFromSalesRequest();

return $this->_getStore($storeId);
}

return null;
Expand All @@ -99,7 +97,6 @@ public function getWebsite()
{
if ($this->isFrontendArea() and $website = $this->_getWebsite()) {
return $website;

} elseif ($wid = $this->getRequestWebsiteScope()) {
return $this->_getWebsite($wid);
}
Expand All @@ -116,13 +113,14 @@ protected function getStoreIdFromSalesRequest()
{
$orderId = $this->getRequestOrderId();
$shipmentId = $this->getRequestShipmentId();
$entityId = $orderId ?: $shipmentId;
$entityId = $orderId ? : $shipmentId;
$repository = $orderId ? $this->orderRepo : $this->shipmentRepo;

try {
$entity = $repository->get($entityId);
} catch (\Exception $e) {
$this->_logger->error("Error resolving sales scope.", $e);
$this->_logger->error('Error resolving sales scope.', $e);

return null;
}

Expand All @@ -131,47 +129,49 @@ protected function getStoreIdFromSalesRequest()

protected function isAdminArea()
{
return in_array($this->getAreaCode(), self::$ADMIN_AREAS);
return in_array($this->getAreaCode(), self::$ADMIN_AREAS);
}

protected function isFrontendArea()
{
return $this->getAreaCode() === "frontend";
return $this->getAreaCode() === Area::AREA_FRONTEND || $this->getAreaCode() === Area::AREA_WEBAPI_REST;
}

protected function getRequestStoreScope()
{
return $this->_request->getParam("store");
return $this->_request->getParam('store');
}

protected function getRequestWebsiteScope()
{
return $this->_request->getParam("website");
return $this->_request->getParam('website');
}

protected function getRequestOrderId()
{
return $this->_request->getParam('order_id') ?: $this->webapiRequest->getParam('orderId');
return $this->_request->getParam('order_id') ? : $this->webapiRequest->getParam('orderId');
}

protected function getRequestShipmentId()
{
return $this->_request->getParam("shipment_id");
return $this->_request->getParam('shipment_id');
}

/**
* @param WebsiteInterface $website
*
* @return null|int
*/
protected function getWebsiteSingleStore(WebsiteInterface $website)
{
if ($website instanceof Website) {
$stores = $website->getStores();
if ($stores and count($stores) == 1) {
return array_values($stores)[0]; // getStores returns indexed by store ID
}
}
return null;
if ($website instanceof Website) {
$stores = $website->getStores();
if ($stores and count($stores) == 1) {
return array_values($stores)[0]; // getStores returns indexed by store ID
}
}

return null;
}

/**
Expand All @@ -183,13 +183,16 @@ private function getAreaCode()
return $this->appState->getAreaCode();
} catch (LocalizedException $e) {
$this->_logger->error("Error getting area code: {$e->getMessage()}");

return null;
}
}

/**
* @param null|int $storeId
* @param null|int|string $storeId
*
* @return StoreInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function _getStore($storeId = null)
{
Expand All @@ -198,6 +201,7 @@ private function _getStore($storeId = null)

/**
* @param null|int $websiteId
*
* @return null|WebsiteInterface
*/
private function _getWebsite($websiteId = null)
Expand All @@ -206,8 +210,9 @@ private function _getWebsite($websiteId = null)
return $this->storeManager->getWebsite($websiteId);
} catch (LocalizedException $e) {
$this->_logger->error("Error getting website: {$e->getMessage()}");

return null;
}
}

}
}
Loading

0 comments on commit 28bfa64

Please sign in to comment.