Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Controller/AbandonedCart/LoadCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,11 @@ public function execute()

$customerSessionCustomer = $this->customerSession->getCustomer();
$isLoggedInCustomerDifferent = false;
if ($customerSessionCustomer) {
$customerEmail = $customerSessionCustomer->getEmail();
$abandonedQuoteCustomerEmail = $abandonedQuote->getCustomer()->getEmail();
if ($customerEmail !== $abandonedQuoteCustomerEmail) {
$isLoggedInCustomerDifferent = true;
$this->customerSession->logoutCustomer();
}
$customerEmail = $customerSessionCustomer->getEmail();
$abandonedQuoteCustomerEmail = $abandonedQuote->getCustomer()->getEmail();
if ($customerEmail !== $abandonedQuoteCustomerEmail) {
$isLoggedInCustomerDifferent = true;
$this->customerSession->logoutCustomer();
}

/** @phpstan-ignore-next-line */
Expand Down
5 changes: 5 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@ class Config extends CoreConfig
/**
* Custom attribute code for SMS marketing
*/
// phpcs:ignore
const YOTPO_CUSTOM_ATTRIBUTE_SMS_MARKETING = 'yotpo_accepts_sms_marketing';

/**
* Custom Customer attribute name for synced to yotpo customer
*/
// phpcs:ignore
const SYNCED_TO_YOTPO_CUSTOMER_ATTRIBUTE_NAME = 'synced_to_yotpo_customer';

/**
* Customer entity int table name
*/
// phpcs:ignore
const CUSTOMER_ENTITY_INT_TABLE_NAME = 'customer_entity_int';

/**
* Yotpo customers sync table name
*/
// phpcs:ignore
const YOTPO_CUSTOMERS_SYNC_TABLE_NAME = 'yotpo_customers_sync';

/**
* HTTP Request PATCH method string
*/
// phpcs:ignore
const PATCH_METHOD_STRING = 'PATCH';

/**
Expand Down
4 changes: 3 additions & 1 deletion Model/Config/Backend/Sync/CustomersScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public function __construct(
*/
public function afterSave()
{
$customersCronExpressionString = $this->getData('groups/sync_settings/groups/customers_sync/fields/frequency/value');
$customersCronExpressionString = $this->getData(
'groups/sync_settings/groups/customers_sync/fields/frequency/value'
);
try {
$this->configureCronCustomersSync($customersCronExpressionString);

Expand Down
1 change: 1 addition & 0 deletions Model/Sync/Customers/Cron/CustomersSyncRetry.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Yotpo\SmsBump\Model\Sync\Customers\Cron;

use Yotpo\SmsBump\Model\Sync\Customers\Services\CustomersService;

/**
Expand Down
29 changes: 14 additions & 15 deletions Model/Sync/Customers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Yotpo\SmsBump\Model\Sync\Customers;

use Magento\Framework\DataObject;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\App\Emulation as AppEmulation;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerFactory;
Expand All @@ -19,6 +18,7 @@ class Main extends CoreCustomersProcessor
/**
* Customers sync limit config key
*/
// phpcs:ignore
const CUSTOMERS_SYNC_LIMIT_CONFIG_KEY = 'customers_sync_limit';

/**
Expand Down Expand Up @@ -48,6 +48,7 @@ class Main extends CoreCustomersProcessor

/**
* Customer sync batch size retrieved from configuration
* @var mixed|string
*/
protected $customersSyncBatchSize;

Expand Down Expand Up @@ -78,7 +79,7 @@ public function __construct(
}

/**
* @param array $retryCustomersIds
* @param array<mixed>|null $retryCustomersIds
* @param int $storeId
* @return mixed
*/
Expand All @@ -96,7 +97,7 @@ public function createCustomersCollectionQuery($retryCustomersIds, $storeId)
$syncedToYotpoCustomerAttributeName = $this->config::SYNCED_TO_YOTPO_CUSTOMER_ATTRIBUTE_NAME;
$customersCollectionQuery
->addFieldToFilter('store_id', $storeId)
->addAttributeToFilter([
->addFieldToFilter([
[ 'attribute' => $syncedToYotpoCustomerAttributeName, 'null' => true ],
[ 'attribute' => $syncedToYotpoCustomerAttributeName, 'eq' => 0 ]
])
Expand Down Expand Up @@ -132,18 +133,17 @@ public function getCustomersIdsForCustomersThatShouldBeRetriedForSync()

/**
* Prepares Customer sync table data
* @param DataObject $customerSyncToYotpoResponse
* @param mixed $customerSyncToYotpoResponse
* @param int $magentoCustomerId
* @param string $storeId
* @return array
* @param int $storeId
* @return array<mixed>
*/
public function createCustomerSyncData($customerSyncToYotpoResponse, $magentoCustomerId, $storeId)
{
$currentTime = date('Y-m-d H:i:s');
$statusCode = $customerSyncToYotpoResponse->getData('status');
$shouldRetry = $this->config->isNetworkRetriableResponse($statusCode);
$customerSyncData = [
/** @phpstan-ignore-next-line */
'customer_id' => $magentoCustomerId,
'response_code' => $statusCode,
'should_retry' => $shouldRetry,
Expand All @@ -157,16 +157,15 @@ public function createCustomerSyncData($customerSyncToYotpoResponse, $magentoCus
/**
* Creates failed customer sync table data
* @param int $magentoCustomerId
* @param string $storeId
* @return array
* @param string|int $storeId
* @return array<mixed>
*/
public function createServerErrorCustomerSyncData($magentoCustomerId, $storeId)
{
$currentTime = date('Y-m-d H:i:s');
$statusCode = '500';
$shouldRetry = false;
$customerSyncData = [
/** @phpstan-ignore-next-line */
'customer_id' => $magentoCustomerId,
'response_code' => $statusCode,
'should_retry' => $shouldRetry,
Expand All @@ -180,7 +179,7 @@ public function createServerErrorCustomerSyncData($magentoCustomerId, $storeId)
/**
* Inserts or updates custom table data
*
* @param array $customerSyncData
* @param array<mixed> $customerSyncData
* @return void
*/
public function insertOrUpdateCustomerSyncData($customerSyncData)
Expand All @@ -189,18 +188,18 @@ public function insertOrUpdateCustomerSyncData($customerSyncData)
}

/**
* @param int $customerId
* @param mixed $customerId
* @param string $attributeCode
* @param boolean $isSynced
* @param int $isSynced
* @return void
* @throws NoSuchEntityException
*/
public function insertOrUpdateCustomerAttribute($customerId, $attributeCode, $isSynced = true)
public function insertOrUpdateCustomerAttribute($customerId, $attributeCode, $isSynced = 0)
{
$customerEntityIntData = [
'attribute_id' => $attributeCode,
'entity_id' => $customerId,
'value' => $isSynced
'value' => (int) $isSynced
];

$this->insertOnDuplicate($this->config::CUSTOMER_ENTITY_INT_TABLE_NAME, [$customerEntityIntData]);
Expand Down
74 changes: 49 additions & 25 deletions Model/Sync/Customers/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@ public function __construct(
$this->storeManager= $storeManager;
$this->yotpoCoreSyncData = $yotpoCoreSyncData;
$this->yotpoCustomersSyncRepositoryInterface = $yotpoCustomersSyncRepositoryInterface;
parent::__construct($appEmulation, $resourceConnection, $yotpoSmsConfig, $data, $customerFactory, $yotpoCustomersLogger);
parent::__construct(
$appEmulation,
$resourceConnection,
$yotpoSmsConfig,
$data,
$customerFactory,
$yotpoCustomersLogger
);
}

/**
* Process customers
* @param array $retryCustomersIds
* @param array $storeIds
* @param array<mixed> $retryCustomersIds
* @param array<int> $storeIds
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
Expand Down Expand Up @@ -168,8 +175,8 @@ public function process($retryCustomersIds = [], $storeIds = [])

/**
* Process customer entities
* @param array|null $retryCustomersIdsToSync
* @param string|null $storeId
* @param array<mixed>|null $retryCustomersIdsToSync
* @param int|null $storeId
* @param boolean|null $isAttributeSync
* @return void
* @throws LocalizedException
Expand Down Expand Up @@ -199,7 +206,8 @@ public function processEntities($retryCustomersIdsToSync = [], $storeId = null,
);
$isCustomerAccountShared = $this->config->isCustomerAccountShared();
$storesIdsEligibleForSync = $this->getEligibleStoresForSync();
$syncedToYotpoCustomerAttributeCode = $this->yotpoCoreSyncData->getAttributeId($this->config::SYNCED_TO_YOTPO_CUSTOMER_ATTRIBUTE_NAME);
$syncedToYotpoCustomerAttributeCode =
$this->yotpoCoreSyncData->getAttributeId($this->config::SYNCED_TO_YOTPO_CUSTOMER_ATTRIBUTE_NAME);
foreach ($customersCollection as $customer) {
$customerId = $customer->getId();
try {
Expand All @@ -219,7 +227,8 @@ public function processEntities($retryCustomersIdsToSync = [], $storeId = null,
$this->insertOrUpdateCustomerSyncData($customerSyncData);
$this->yotpoCustomersLogger->info(
__(
'Failed to sync customer to Yotpo - Magento Store ID: %1, Magento Store Name: %2, Customer ID: %3 Exception Message: %4',
'Failed to sync customer to Yotpo - Magento Store ID: %1,
Magento Store Name: %2, Customer ID: %3 Exception Message: %4',
$storeId,
$this->config->getStoreName($storeId),
$customerId,
Expand Down Expand Up @@ -253,17 +262,15 @@ public function processEntities($retryCustomersIdsToSync = [], $storeId = null,
*/
public function processCustomer($customer, $customerAddress = null)
{
$storeId = $customer->getStoreId();
$storeId = $this->config->getStoreId();
if ($this->config->syncResetInProgress($storeId, 'customer')) {
$this->yotpoSmsBumpLogger->info(
if ($this->config->isSyncResetInProgress($storeId, 'customer')) {
$this->yotpoCustomersLogger->info(
__(
'Customer sync is skipped because sync reset is in progress - Magento Store ID: %1, Name: %2',
$storeId,
$this->config->getStoreName($storeId)
)
);
return [];
}
$customerId = $customer->getId();

Expand Down Expand Up @@ -302,7 +309,8 @@ public function processCustomer($customer, $customerAddress = null)
$this->insertOrUpdateCustomerSyncData($customerSyncData);
$this->yotpoCustomersLogger->info(
__(
'Failed to sync Customer to Yotpo - Magento Store ID: %1, Magento Store Name: %2, Customer ID: %3, Exception Message: %4',
'Failed to sync Customer to Yotpo - Magento Store ID: %1,
Magento Store Name: %2, Customer ID: %3, Exception Message: %4',
$storeId,
$this->config->getStoreName($storeId),
$customerId,
Expand Down Expand Up @@ -339,17 +347,22 @@ public function retryCustomersSync()

/**
* @param Customer $customer
* @param string $storeId
* @param int $storeId
* @param boolean $isRealTimeSync
* @param null $customerAddress
* @param array<mixed>|null $customerAddress
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function syncCustomer(Customer $customer, $storeId, $isRealTimeSync = false, $customerAddress = null)
{
$customerId = $customer->getId();
$customerDataForSync = $this->prepareCustomerDataForSync($customer, $customerId, $isRealTimeSync, $customerAddress);
$customerDataForSync = $this->prepareCustomerDataForSync(
$customer,
$customerId,
$isRealTimeSync,
$customerAddress
);
$response = $this->executeCustomerSyncRequest($customer, $customerDataForSync);
if ($response) {
$customerSyncData = $this->createCustomerSyncData($response, $customerId, $storeId);
Expand Down Expand Up @@ -404,15 +417,19 @@ private function executeCustomerSyncRequest($customer, $customerDataForSync = nu

/**
* @param Customer $customer
* @param array $storesIds
* @param array<mixed> $storesIds
* @param boolean $isRealTimeSync
* @param array $customerAddress
* @param array<mixed> $customerAddress
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function syncSharedStoresCustomer(Customer $customer, $storesIds, $isRealTimeSync = false, $customerAddress = null)
{
private function syncSharedStoresCustomer(
Customer $customer,
$storesIds,
$isRealTimeSync = false,
$customerAddress = null
) {
foreach ($storesIds as $storeId) {
$this->emulateFrontendArea($storeId);
$this->syncCustomer($customer, $storeId, $isRealTimeSync, $customerAddress);
Expand All @@ -427,8 +444,12 @@ private function syncSharedStoresCustomer(Customer $customer, $storesIds, $isRea
* @param mixed|null $customerAddress
* @return mixed
*/
private function prepareCustomerDataForSync(Customer $customer, $customerId, $isRealTimeSync, $customerAddress = null)
{
private function prepareCustomerDataForSync(
Customer $customer,
$customerId,
$isRealTimeSync,
$customerAddress = null
) {
if (isset($this->customerDataPrepared[$customerId])) {
$customerDataForSync = $this->customerDataPrepared[$customerId];
} else {
Expand All @@ -440,14 +461,14 @@ private function prepareCustomerDataForSync(Customer $customer, $customerId, $is
}

/**
* @return array
* @return array<int>
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function getEligibleStoresForSync(): array
{
$storesIdsEligibleForSync = [];
$storesIds = $this->config->getAllStoreIds(false);
$storesIds = (array) $this->config->getAllStoreIds(false);
foreach ($storesIds as $storeId) {
if ($this->config->isCustomerSyncActive($storeId)) {
$storesIdsEligibleForSync[] = $storeId;
Expand All @@ -470,11 +491,14 @@ private function updateLastSyncDate($currentTime)
}

/**
* @param string $customerId
* @param mixed $customerId
* @return void
*/
private function updateSyncedToYotpoCustomerAttributeAsSynced($customerId)
{
$syncedToYotpoCustomerAttributeCode = $this->yotpoCoreSyncData->getAttributeId($this->config::SYNCED_TO_YOTPO_CUSTOMER_ATTRIBUTE_NAME);
$syncedToYotpoCustomerAttributeCode = $this->yotpoCoreSyncData->getAttributeId(
$this->config::SYNCED_TO_YOTPO_CUSTOMER_ATTRIBUTE_NAME
);
$this->insertOrUpdateCustomerAttribute($customerId, $syncedToYotpoCustomerAttributeCode);
}
}
Loading