Skip to content

Commit

Permalink
closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Mar 22, 2017
1 parent 2a511d2 commit dbcb2a1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 126 deletions.
97 changes: 56 additions & 41 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,46 +181,57 @@ public function getApi($store = null)
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getConfigValue($path, $storeId = null)
public function getConfigValue($path, $storeId = null, $scope = null)
{
$areaCode = $this->_state->getAreaCode();
if ($storeId !== null) {
$configValue = $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
} elseif ($areaCode == 'frontend') {
$frontStoreId = $this->_storeManager->getStore()->getId();
$configValue = $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$frontStoreId
);
} else {
$storeId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$websiteId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
if (!empty($storeId)) {
$configValue = $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
} elseif (!empty($websiteId)) {
$configValue = $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
$websiteId
);
} else {
$configValue = $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
0
);
}
switch ($scope) {
case 'website':
$value = $this->_scopeConfig->getValue($path ,\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, $storeId);
break;
default:
$value = $this->_scopeConfig->getValue($path ,\Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
break;
}
return $configValue;
return $value;


// if ($storeId !== null) {
// $configValue = $this->scopeConfig->getValue(
// $path,
// \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
// $storeId
// );
// } elseif ($areaCode == 'frontend') {
// $frontStoreId = $this->_storeManager->getStore()->getId();
// $configValue = $this->scopeConfig->getValue(
// $path,
// \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
// $frontStoreId
// );
// } else {
// $storeId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
// $websiteId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
// if (!empty($storeId)) {
// $configValue = $this->scopeConfig->getValue(
// $path,
// \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
// $storeId
// );
// } elseif (!empty($websiteId)) {
// $configValue = $this->scopeConfig->getValue(
// $path,
// \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
// $websiteId
// );
// } else {
// $configValue = $this->scopeConfig->getValue(
// $path,
// \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
// 0
// );
// }
// }
// return $configValue;
}

/**
Expand Down Expand Up @@ -466,17 +477,19 @@ public function getDateMicrotime()
$date = date('Y-m-d-H-i-s') . '-' . $msecArray[1];
return $date;
}
public function resetErrors()
public function resetErrors($mailchimpStore)
{
try {
// clean the errors table
$connection = $this->_mailChimpErrors->getResource()->getConnection();
$tableName = $this->_mailChimpErrors->getResource()->getMainTable();
$connection->truncateTable($tableName);
$connection->delete($tableName, "mailchimp_store_id = '".$mailchimpStore."'");
// $connection->truncateTable($tableName);
// clean the syncecommerce table with errors
$connection = $this->_mailChimpSyncE->getResource()->getConnection();
$tableName = $this->_mailChimpSyncE->getResource()->getMainTable();
$connection->truncateTable($tableName);
$connection->delete($tableName, "mailchimp_store_id = '".$mailchimpStore."' and mailchimp_sync_error is not null");
// $connection->truncateTable($tableName);
} catch(\Zend_Db_Exception $e) {
throw new ValidatorException(__($e->getMessage()));
}
Expand All @@ -503,6 +516,8 @@ public function getChimpSyncEcommerce($storeId,$id,$type)
$chimp = $this->_mailChimpSyncEcommerce->create();
return $chimp->getByStoreIdType($storeId,$id,$type);
}

public function getScope()
{
}

}
102 changes: 17 additions & 85 deletions Model/Api/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,30 @@ class Result
*/
private $_archive;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpErrors
* @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory
*/
private $_chimpErrors;
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
private $_productRepository;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
private $_customerRepository;
/**
* @var \Magento\Sales\Model\OrderRepository
*/
private $_orderRepository;

private $_chimpSyncEcommerce;


/**
* Result constructor.
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\Collection $batchCollection
* @param \Ebizmarts\MailChimp\Model\MailChimpErrors $chimpErrors
* @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $chimpErrors
* @param \Magento\Framework\Archive $archive
* @param \Magento\Catalog\Model\ProductRepository $productRepository
*/
public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\Collection $batchCollection,
\Ebizmarts\MailChimp\Model\MailChimpErrors $chimpErrors,
\Magento\Framework\Archive $archive,
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Sales\Model\OrderRepository $orderRepository,
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce
\Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $chimpErrors,
\Magento\Framework\Archive $archive
)
{
$this->_batchCollection = $batchCollection;
$this->_helper = $helper;
$this->_archive = $archive;
$this->_chimpErrors = $chimpErrors;
$this->_productRepository = $productRepository;
$this->_customerRepository = $customerRepository;
$this->_orderRepository = $orderRepository;
$this->_chimpSyncEcommerce = $chimpSyncEcommerce;
}
public function processResponses($storeId, $isMailChimpStoreId = false)
public function processResponses($storeId, $isMailChimpStoreId = false, $mailchimpStoreId)
{
$collection = $this->_batchCollection;
$collection
Expand All @@ -91,7 +67,7 @@ public function processResponses($storeId, $isMailChimpStoreId = false)
$storeId = ($isMailChimpStoreId) ? 0 : $storeId;
$files = $this->getBatchResponse($item->getBatchId(), $storeId);
if (count($files)) {
$this->processEachResponseFile($files, $item->getBatchId());
$this->processEachResponseFile($files, $item->getBatchId(), $mailchimpStoreId, $storeId);
$item->setStatus('completed');
$item->getResource()->save($item);
}
Expand Down Expand Up @@ -145,7 +121,7 @@ protected function getBatchResponse($batchId, $storeId = 0)
}
return $files;
}
protected function processEachResponseFile($files, $batchId)
protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , $storeId)
{
foreach ($files as $file) {
$items = json_decode(file_get_contents($file));
Expand All @@ -155,10 +131,12 @@ protected function processEachResponseFile($files, $batchId)
$type = $line[0];
$id = $line[2];

$mailchimpErrors = $this->_chimpErrors;
$mailchimpErrors = $this->_chimpErrors->create();

//parse error
$response = json_decode($item->response);
$this->_helper->log($response);
$this->_helper->log("type [$type] id [$id]");
$errorDetails = "";
if (!empty($response->errors)) {
foreach ($response->errors as $error) {
Expand All @@ -173,66 +151,20 @@ protected function processEachResponseFile($files, $batchId)
}

$error = $response->title . " : " . $response->detail;

switch ($type) {
case \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT:
$p = $this->_productRepository->getById($id);
if ($p->getId() == $id) {
$p->setData("mailchimp_sync_error", $error);
//$p->setMailchimpUpdateObserverRan(true);
$this->_productRepository->save($p);
} else {
$this->_helper->log("Error: product " . $id . " not found");
}
break;
case \Ebizmarts\MailChimp\Helper\Data::IS_CUSTOMER:
$c = $this->_customerRepository->getById($id);
if ($c->getId() == $id) {
// $c->setCustomAttribute("mailchimp_sync_error", $error);
$this->_customerRepository->save($c);
} else {
$this->_helper->log("Error: customer " . $id . " not found");
}
break;
case \Ebizmarts\MailChimp\Helper\Data::IS_ORDER:
$o = $this->_orderRepository->get($id);
if ($o->getId() == $id) {
$c = $this->_chimpSyncEcommerce->getByStoreIdType($o->getStoreId(),$id,$type);
$c->setData("mailchimp_sync_error", $error);
$c->getResource()->save($c);
} else {
$this->_helper->log("Error: order " . $id . " not found");
}
break;
// case \Ebizmarts\MailChimp\Helper\Data::IS_QUOTE:
// $q = Mage::getModel('sales/quote')->load($id);
// if ($q->getId() == $id) {
// $q->setData("mailchimp_sync_error", $error);
// $q->save();
// } else {
// $this->_helper->log("Error: quote " . $id . " not found");
// }
// break;
// case \Ebizmarts\MailChimp\Helper\Data::IS_SUBSCRIBER:
// $s = Mage::getModel('newsletter/subscriber')->load($id);
// if ($s->getId() == $id) {
// $s->setData("mailchimp_sync_error", $error);
// $s->save();
// } else {
// $this->_helper->log("Error: subscriber " . $id . " not found");
// }
// break;
default:
$this->_helper->log("Error: no identification " . $type . " found");
break;
}
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSync
*/
$chimpSync = $this->_helper->getChimpSyncEcommerce($storeId,$id,$type);
$chimpSync->setData("mailchimp_sync_error", $error);
$chimpSync->getResource()->save($chimpSync);
$mailchimpErrors->setType($response->type);
$mailchimpErrors->setTitle($response->title);
$mailchimpErrors->setStatus($item->status_code);
$mailchimpErrors->setErrors($errorDetails);
$mailchimpErrors->setRegtype($type);
$mailchimpErrors->setOriginalId($id);
$mailchimpErrors->setBatchId($batchId);
$mailchimpErrors->setMailchimpStoreId($mailchimpStoreId);
$mailchimpErrors->getResource()->save($mailchimpErrors);
}
}
Expand Down

0 comments on commit dbcb2a1

Please sign in to comment.