Skip to content

Commit

Permalink
create specific helper for ecommerce table #1708 for magento 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Jun 19, 2023
1 parent 00e39f9 commit 19a9061
Show file tree
Hide file tree
Showing 28 changed files with 483 additions and 272 deletions.
15 changes: 11 additions & 4 deletions Controller/Adminhtml/Ecommerce/ResetLocalErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\ValidatorException;
use Symfony\Component\Config\Definition\Exception\Exception;
use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;

class ResetLocalErrors extends \Magento\Backend\App\Action
{
Expand All @@ -27,29 +28,35 @@ class ResetLocalErrors extends \Magento\Backend\App\Action
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $helper;
/**
* @var SyncHelper
*/
private $syncHelper;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* ResetLocalErrors constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param JsonFactory $resultJsonFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param SyncHelper $syncHelper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
JsonFactory $resultJsonFactory,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
\Ebizmarts\MailChimp\Helper\Data $helper
\Ebizmarts\MailChimp\Helper\Data $helper,
SyncHelper $syncHelper
) {

parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->helper = $helper;
$this->storeManager = $storeManagerInterface;
$this->syncHelper = $syncHelper;
}

public function execute()
Expand Down Expand Up @@ -78,7 +85,7 @@ public function execute()

$resultJson = $this->resultJsonFactory->create();
try {
$this->helper->resetErrors($mailchimpStore, true);
$this->syncHelper->resetErrors($mailchimpStore, true);
} catch (ValidatorException $e) {
$valid = 0;
$message = $e->getMessage();
Expand Down
15 changes: 11 additions & 4 deletions Controller/Adminhtml/Ecommerce/ResetLocalErrorsNoRetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\ValidatorException;
use Symfony\Component\Config\Definition\Exception\Exception;
use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;

class ResetLocalErrorsNoRetry extends \Magento\Backend\App\Action
{
Expand All @@ -27,29 +28,35 @@ class ResetLocalErrorsNoRetry extends \Magento\Backend\App\Action
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $helper;
/**
* @var SyncHelper
*/
private $syncHelper;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* ResetLocalErrors constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param JsonFactory $resultJsonFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param SyncHelper $syncHelper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
JsonFactory $resultJsonFactory,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
\Ebizmarts\MailChimp\Helper\Data $helper
\Ebizmarts\MailChimp\Helper\Data $helper,
SyncHelper $syncHelper
) {

parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->helper = $helper;
$this->storeManager = $storeManagerInterface;
$this->syncHelper = $syncHelper;
}

public function execute()
Expand Down Expand Up @@ -78,7 +85,7 @@ public function execute()

$resultJson = $this->resultJsonFactory->create();
try {
$this->helper->resetErrors($mailchimpStore, false);
$this->syncHelper->resetErrors($mailchimpStore, false);
} catch (ValidatorException $e) {
$valid = 0;
$message = $e->getMessage();
Expand Down
15 changes: 11 additions & 4 deletions Controller/Cart/Loadquote.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* @copyright Copyright © 2017 Ebizmarts Corp.. All rights reserved.
* @author [email protected]
*/

namespace Ebizmarts\MailChimp\Controller\Cart;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;

class Loadquote extends Action
{
Expand All @@ -31,6 +32,10 @@ class Loadquote extends Action
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;
/**
* @var SyncHelper
*/
private $syncHelper;
/**
* @var \Magento\Framework\Url
*/
Expand All @@ -49,13 +54,13 @@ class Loadquote extends Action
protected $_checkoutSession;

/**
* Loadquote constructor.
* @param Context $context
* @param PageFactory $pageFactory
* @param \Magento\Quote\Model\QuoteFactory $quote
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param SyncHelper $syncHelper
* @param \Magento\Framework\Url $urlHelper
* @param \Magento\Customer\Model\Url $customerUrl
*/
Expand All @@ -66,14 +71,16 @@ public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Ebizmarts\MailChimp\Helper\Data $helper,
SyncHelper $syncHelper,
\Magento\Framework\Url $urlHelper,
\Magento\Customer\Model\Url $customerUrl
) {

$this->pageFactory = $pageFactory;
$this->_quote = $quote;
$this->_customerSession = $customerSession;
$this->_helper = $helper;
$this->syncHelper = $syncHelper;
$this->_urlHelper = $urlHelper;
$this->_message = $context->getMessageManager();
$this->_customerUrl = $customerUrl;
Expand All @@ -99,7 +106,7 @@ public function execute()
\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,
$magentoStoreId
);
$syncCommerce = $this->_helper->getChimpSyncEcommerce(
$syncCommerce = $this->syncHelper->getChimpSyncEcommerce(
$mailchimpStoreId,
$params['id'],
\Ebizmarts\MailChimp\Helper\Data::IS_QUOTE
Expand Down
16 changes: 12 additions & 4 deletions Controller/Checkout/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ebizmarts\MailChimp\Controller\Checkout;

use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;

class Success extends \Magento\Framework\App\Action\Action
{
/**
Expand All @@ -35,30 +37,36 @@ class Success extends \Magento\Framework\App\Action\Action
* @var \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory
*/
protected $_interestGroupFactory;
/**
* @var SyncHelper
*/
private $syncHelper;

/**
* Success constructor.
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $pageFactory
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory
* @param SyncHelper $syncHelper
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory,
SyncHelper $syncHelper
) {

$this->_pageFactory =$pageFactory;
$this->_helper = $helper;
$this->_checkoutSession = $checkoutSession;
$this->_subscriberFactory = $subscriberFactory;
$this->_interestGroupFactory= $interestGroupFactory;
$this->syncHelper = $syncHelper;
parent::__construct($context);
}

Expand Down Expand Up @@ -112,7 +120,7 @@ protected function _updateSubscriber(
$sync_error = null,
$sync_modified = null
) {
$this->_helper->saveEcommerceData(
$this->syncHelper->saveEcommerceData(
$listId,
$entityId,
\Ebizmarts\MailChimp\Helper\Data::IS_SUBSCRIBER,
Expand Down
106 changes: 1 addition & 105 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const SYNCERROR = 4;
const NOTSYNCED = 5;

const NEVERSYNC = 6;
const NEVERSYNC = 0;

const BATCH_CANCELED = 'canceled';
const BATCH_COMPLETED = 'completed';
Expand Down Expand Up @@ -135,18 +135,6 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @var \Magento\Customer\Model\ResourceModel\Customer\CustomerRepository
*/
private $_customer;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpErrors
*/
private $_mailChimpErrors;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory
*/
private $_mailChimpSyncEcommerce;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce
*/
private $_mailChimpSyncE;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncBatches
*/
Expand Down Expand Up @@ -268,9 +256,6 @@ public function __construct(
\Mailchimp $api,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Customer\Model\ResourceModel\CustomerRepository $customer,
\Ebizmarts\MailChimp\Model\MailChimpErrors $mailChimpErrors,
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory $mailChimpSyncEcommerce,
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $mailChimpSyncE,
\Ebizmarts\MailChimp\Model\MailChimpSyncBatches $syncBatches,
\Ebizmarts\MailChimp\Model\MailChimpStoresFactory $mailChimpStoresFactory,
\Ebizmarts\MailChimp\Model\MailChimpStores $mailChimpStores,
Expand Down Expand Up @@ -300,9 +285,6 @@ public function __construct(
$this->_config = $config;
$this->_api = $api;
$this->_customer = $customer;
$this->_mailChimpErrors = $mailChimpErrors;
$this->_mailChimpSyncEcommerce = $mailChimpSyncEcommerce;
$this->_mailChimpSyncE = $mailChimpSyncE;
$this->_syncBatches = $syncBatches;
$this->_mailChimpStores = $mailChimpStores;
$this->_mailChimpStoresFactory = $mailChimpStoresFactory;
Expand Down Expand Up @@ -840,93 +822,7 @@ public function getDateMicrotime()
$date = date('Y-m-d-H-i-s') . '-' . $msecArray[1];
return $date;
}
public function resetErrors($mailchimpStore, $retry)
{
try {
// clean the errors table
$connection = $this->_mailChimpErrors->getResource()->getConnection();
$tableName = $this->_mailChimpErrors->getResource()->getMainTable();
$connection->delete($tableName, "mailchimp_store_id = '".$mailchimpStore."'");
// clean the syncecommerce table with errors
if ($retry) {
$connection = $this->_mailChimpSyncE->getResource()->getConnection();
$tableName = $this->_mailChimpSyncE->getResource()->getMainTable();
$connection->delete(
$tableName,
"mailchimp_store_id = '" . $mailchimpStore . "' and mailchimp_sync_error is not null"
);
}
} catch (\Zend_Db_Exception $e) {
throw new ValidatorException(__($e->getMessage()));
}
}
public function resetEcommerce()
{
$this->resetErrors();
}
public function saveEcommerceData(
$storeId,
$entityId,
$type,
$date = null,
$error = null,
$modified = null,
$deleted = null,
$token = null,
$sent = null
) {
if (!empty($entityId)) {
$chimpSyncEcommerce = $this->getChimpSyncEcommerce($storeId, $entityId, $type);
if ($chimpSyncEcommerce->getRelatedId() == $entityId ||
!$chimpSyncEcommerce->getRelatedId() && $modified != 1) {
$chimpSyncEcommerce->setMailchimpStoreId($storeId);
$chimpSyncEcommerce->setType($type);
$chimpSyncEcommerce->setRelatedId($entityId);
if ($modified!==null) {
$chimpSyncEcommerce->setMailchimpSyncModified($modified);
} else {
$chimpSyncEcommerce->setMailchimpSyncModified(0);
}
if ($date) {
$chimpSyncEcommerce->setMailchimpSyncDelta($date);
} elseif ($modified != 1) {
$chimpSyncEcommerce->setBatchId(null);
}
if ($error) {
$chimpSyncEcommerce->setMailchimpSyncError($error);
}
if ($deleted) {
$chimpSyncEcommerce->setMailchimpSyncDeleted($deleted);
$chimpSyncEcommerce->setMailchimpSyncModified(0);
}
if ($token) {
$chimpSyncEcommerce->setMailchimpToken($token);
}
if ($sent) {
$chimpSyncEcommerce->setMailchimpSent($sent);
}
$chimpSyncEcommerce->getResource()->save($chimpSyncEcommerce);
}
}
}

public function markEcommerceAsDeleted($relatedId, $type, $relatedDeletedId = null)
{
$this->_mailChimpSyncE->markAllAsDeleted($relatedId, $type, $relatedDeletedId);
}
public function ecommerceDeleteAllByIdType($id, $type, $mailchimpStoreId)
{
$this->_mailChimpSyncE->deleteAllByIdType($id, $type, $mailchimpStoreId);
}
public function deleteAllByBatchId($batchId)
{
$this->_mailChimpSyncE->deleteAllByBatchid($batchId);
}
public function getChimpSyncEcommerce($storeId, $id, $type)
{
$chimp = $this->_mailChimpSyncEcommerce->create();
return $chimp->getByStoreIdType($storeId, $id, $type);
}
public function loadStores()
{

Expand Down
Loading

0 comments on commit 19a9061

Please sign in to comment.