Skip to content

Commit

Permalink
save all API calls and errors #2041 for magento 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Dec 3, 2024
1 parent a77a2e2 commit a8a8ad2
Show file tree
Hide file tree
Showing 24 changed files with 67 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Block/Adminhtml/System/Config/Form/Field/MailchimpMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function _getMailchimpTags()
$ret[$item['tag']] = $item['tag'] . ' (' . $item['name'] . ' : ' . $item['type'] . ')';
}
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
}
return $ret;
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Ecommerce/GetInterest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function execute()
if (is_array($result['categories']) && count($result['categories'])) {
$rc = $result['categories'];
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
$error = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Ecommerce/Getaccountdetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function execute()
$options['nostore'] = ['label' => __('Ecommerce disabled, only subscribers will be synchronized (your orders, products,etc will be not synchronized)'), 'value' => ''];
}
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
$options['error'] = ['label' => 'Error', 'value' => __('--- Invalid API Key ---')];
}
Expand Down
4 changes: 2 additions & 2 deletions Controller/Adminhtml/Lists/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Framework\Encryption\Encryptor $encryptor
) {

parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_helper = $helper;
Expand All @@ -62,7 +62,7 @@ public function execute()
$result['lists'][] = ['id' => $list['id'], 'name' => $list['name']];
}
$result['valid'] = 1;
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$result['valid'] = 0;
$result['errormsg'] = $e->getTitle();
$this->_helper->log($e->getFriendlyMessage());
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Stores/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function execute()
$api->ecommerce->stores->delete($storeModel->getStoreid());
$this->messageManager->addSuccess(__('You deleted the store.'));
return $resultRedirect->setPath('mailchimp/stores');
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->messageManager->addError(__('Store could not be deleted.'.$e->getMessage()));
$this->_mhelper->log($e->getFriendlyMessage());
return $resultRedirect->setPath('mailchimp/stores/edit', ['id'=>$storeId]);
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Stores/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function execute()
$result['valid'] = 1;
}
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_mhelper->log($e->getFriendlyMessage());
$result['valid'] = 0;
$result['errormsg'] = $e->getTitle();
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Stores/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function execute()
} else {
return $resultRedirect->setPath('mailchimp/stores');
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->messageManager->addErrorMessage(__('Store could not be saved.'.$e->getMessage()));
$this->_mhelper->log($e->getFriendlyMessage());
return $resultRedirect->setPath('mailchimp/stores/edit', ['id'=>$storeId]);
Expand Down
6 changes: 3 additions & 3 deletions Cron/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected function _processStore($storeId, $mailchimpStoreId, $listId)
$this->markWithError($BadOperations, $mailchimpStoreId, $listId);
}

} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
} catch (\Exception $e) {
$this->_helper->log($e->getMessage());
Expand Down Expand Up @@ -340,7 +340,7 @@ protected function apiUpdateSyncFlag($storeId, $mailchimpStoreId)
null,
false
);
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log('MailChimp error when updating syncing flag for store ' . $storeId);
$this->_helper->log($e->getFriendlyMessage());
}
Expand All @@ -350,7 +350,7 @@ protected function _ping($storeId)
try {
$api = $this->_helper->getApi($storeId);
$api->root->info();
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
return false;
}
Expand Down
15 changes: 4 additions & 11 deletions Cron/GenerateStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory as ScheduleCollectionFactory;
use Ebizmarts\MailChimp\Model\MailchimpNotificationFactory as MailchimpNotificationFactory;
use Magento\Framework\App\ProductMetadataInterface;
use Ebizmarts\MailChimp\Helper\Data;
use Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\CollectionFactory as MailChimpSyncBatchesCollectionFactory;
use Ebizmarts\MailChimp\Model\Config\ModuleVersion;

class GenerateStatistics
{
/**
* @var MailchimpNotificationFactory
*/
protected $mailchimpNotificationFactory;
/**
* @var Data
*/
Expand Down Expand Up @@ -56,13 +51,15 @@ class GenerateStatistics
* @var ModuleVersion
*/
protected $moduleVersion;
/**
* @var TimezoneInterface
*/
protected $locale;
protected $deleteAction = [
0 => 'Unsubscribe',
1 => 'Delete',
];
public function __construct(
MailchimpNotificationFactory $mailchimpNotificationFactory,
Data $helper,
StoreManager $storeManager,
CustomerCollectionFactory $customerCollectionFactory,
Expand All @@ -75,7 +72,6 @@ public function __construct(
TimezoneInterface $locale
)
{
$this->mailchimpNotificationFactory = $mailchimpNotificationFactory;
$this->helper = $helper;
$this->storeManager = $storeManager;
$this->customerCollectionFactory = $customerCollectionFactory;
Expand Down Expand Up @@ -104,10 +100,7 @@ public function execute()
}
$data['jobs'] = $this->getJobs();
if (!empty($data)) {
$mailchimpNotification = $this->mailchimpNotificationFactory->create();
$mailchimpNotification->setNotificationData(json_encode($data));
$mailchimpNotification->setProcessed(false);
$mailchimpNotification->getResource()->save($mailchimpNotification);
$this->helper->saveNotification($data);
}
}
private function getMagentoTotals($storeId)
Expand Down
1 change: 1 addition & 0 deletions Cron/SyncStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private function cleanData()
$connection = $this->mailchimpNotification->getConnection();
$tableName = $this->mailchimpNotification->getMainTable();
$connection->delete($tableName, ['date_add(generated_at , interval 1 week) <= NOW()']);
$connection->delete($tableName, ['processed' => 1]);
} catch (\Exception $e) {
$this->helper->log($e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion Cron/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ protected function _profile($data)
$this->_subscribeMember($subscriber, \Magento\Newsletter\Model\Subscriber::STATUS_UNSUBSCRIBED);
}
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
}
}
Expand Down
46 changes: 32 additions & 14 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\Exception\ValidatorException;
use Magento\Store\Model\Store;
use Symfony\Component\Config\Definition\Exception\Exception;
use Ebizmarts\MailChimp\Model\MailchimpNotificationFactory as MailchimpNotificationFactory;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand Down Expand Up @@ -194,6 +195,11 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
/**
* @var \Magento\Framework\Locale\Resolver
*/
/**
* @var MailchimpNotificationFactory
*/
protected $mailchimpNotificationFactory;

protected $resolver;
private $customerAtt = null;
private $addressAtt = null;
Expand Down Expand Up @@ -223,6 +229,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
* @param \Magento\Directory\Model\CountryFactory $countryFactory
* @param \Magento\Framework\Locale\Resolver $resolver
* @param MailchimpNotificationFactory $mailchimpNotificationFactory
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
Expand All @@ -247,7 +254,8 @@ public function __construct(
\Magento\Framework\App\DeploymentConfig $deploymentConfig,
\Magento\Framework\Stdlib\DateTime\DateTime $date,
\Magento\Directory\Model\CountryFactory $countryFactory,
\Magento\Framework\Locale\Resolver $resolver
\Magento\Framework\Locale\Resolver $resolver,
MailchimpNotificationFactory $mailchimpNotificationFactory
) {

$this->_storeManager = $storeManager;
Expand All @@ -274,6 +282,7 @@ public function __construct(
$this->_date = $date;
$this->countryFactory = $countryFactory;
$this->resolver = $resolver;
$this->mailchimpNotificationFactory = $mailchimpNotificationFactory;
parent::__construct($context);
}

Expand Down Expand Up @@ -321,6 +330,7 @@ public function getApi($store = null, $scope = null)
$apiKey = $this->getApiKey($store, $scope);
$timeOut = $this->getTimeOut($store,$scope);
$this->_api->setApiKey($apiKey);
$this->_api->setHelper($this);
$this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion());
if ($timeOut) {
$this->_api->setTimeOut($timeOut);
Expand Down Expand Up @@ -452,6 +462,8 @@ public function getApiByApiKey($apiKey, $encrypted = false)
$this->_api->setApiKey($apiKey);
}
$this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion());
$this->_api->setHelper($this);

return $this->_api;
}

Expand Down Expand Up @@ -553,7 +565,14 @@ public function log($message, $store = null, $file = null)
$this->_mlogger->mailchimpLog($message, $file);
}
}
public function saveNotification($data)
{
$mailchimpNotification = $this->mailchimpNotificationFactory->create();
$mailchimpNotification->setNotificationData(json_encode($data));
$mailchimpNotification->setProcessed(false);
$mailchimpNotification->getResource()->save($mailchimpNotification);

}
/**
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
Expand All @@ -573,7 +592,7 @@ public function deleteStore($mailchimpStore)
// $storeId = $this->getConfigValue(self::XML_MAILCHIMP_STORE);
$this->getApi()->ecommerce->stores->delete($mailchimpStore);
$this->cancelAllPendingBatches($mailchimpStore);
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
} catch (Exception $e) {
$this->log($e->getMessage());
Expand Down Expand Up @@ -774,7 +793,7 @@ public function getListForMailChimpStore($mailchimpStoreId, $apiKey)
if (isset($store['list_id'])) {
return $store['list_id'];
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
}
return null;
Expand Down Expand Up @@ -804,14 +823,13 @@ public function loadStores()
}
$this->_api->setApiKey(trim($apiKey));
$this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion());
$this->_api->setHelper($this);


try {
$apiStores = $this->_api->ecommerce->stores->get(null, null, null, self::MAXSTORES);
} catch (\Mailchimp_Error $mailchimpError) {
$this->log($mailchimpError->getFriendlyMessage());
continue;
} catch (\Mailchimp_HttpError $mailchimpError) {
$this->log($mailchimpError->getMessage());
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
continue;
}

Expand Down Expand Up @@ -857,7 +875,7 @@ public function loadStores()
$mstore->setMcAccountName($mcUserName[$apiKey]);
$mstore->getResource()->save($mstore);
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
}
}
Expand All @@ -881,7 +899,7 @@ public function saveJsUrl($storeId, $scope = null, $mailChimpStoreId = null)
$storeId
);
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
}
}
Expand All @@ -904,7 +922,7 @@ public function getJsUrl($storeId)
$storeId
);
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
}
}
Expand Down Expand Up @@ -944,7 +962,7 @@ public function createWebHook($apikey, $listId, $scope=null, $scopeId=null)
'_secure' => true]);
// the urlencode of the hookUrl not work
$ret = $api->lists->webhooks->add($listId, $hookUrl, $events, $sources);
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
$ret ['message']= $e->getMessage();
}
Expand All @@ -968,7 +986,7 @@ public function deleteWebHook($apikey, $listId)
}
}
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
}
}
Expand Down Expand Up @@ -1075,7 +1093,7 @@ public function getInterest($storeId)
$this->log(__('Error retrieving interest groups for store ').$storeId);
$rc = [];
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->log($e->getFriendlyMessage());
}
return $rc;
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/PromoRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function _getDeletedPromoRules($mailchimpStoreId, $magentoStoreId)
$batchArray[$count]['path'] = "/ecommerce/stores/$mailchimpStoreId/promo-rules/$ruleId";
$batchArray[$count]['operation_id'] = $this->_batchId . '_' . $ruleId;
$count++;
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
}
$this->syncHelper->ecommerceDeleteAllByIdType(
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function getBatchResponse($batchId, $storeId = null)
self::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId . '/' . $batchId . '.tar');
$this->_driver->deleteFile($fileName . '.tar.gz');
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
return false;
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function deleteSubscriber(\Magento\Newsletter\Model\Subscriber $subscribe
try {
$md5HashEmail = hash('md5', strtolower($subscriber->getSubscriberEmail()));
$api->lists->members->update($listId, $md5HashEmail, null, 'cleaned');
} catch (\MailChimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage(), $storeId);
$this->_message->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Backend/MonkeyStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private function getStore($apiKey, $store)
$api = $this->_helper->getApiByApiKey($apiKey);
$store = $api->ecommerce->stores->get($store);
return $store['list_id'];
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function __construct(
} else {
$this->_options['store_exists'] = false;
}
} catch (\Mailchimp_Error $e) {
} catch (\Mailchimp_Error | \Mailchimp_HttpError $e) {
$this->_helper->log($e->getFriendlyMessage());
$this->_error = $e->getMessage();
$this->_options['store_exists'] = false;
Expand Down
Loading

0 comments on commit a8a8ad2

Please sign in to comment.