97 lines
4.0 KiB
PHP
97 lines
4.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Model\Sku;
|
|
|
|
use Esun\LingxingErp\Model\Config;
|
|
use Esun\LingxingErp\Model\Sync\LogWriter;
|
|
use Magento\Framework\Lock\LockManagerInterface;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
|
|
class SkuSynchronizer
|
|
{
|
|
private const LOCK_NAME = 'esun_lingxing_sku_sync';
|
|
|
|
public function __construct(
|
|
private readonly Config $config,
|
|
private readonly StoreManagerInterface $storeManager,
|
|
private readonly PairFetcher $pairFetcher,
|
|
private readonly ProductErpSkuRepository $repository,
|
|
private readonly LogWriter $logWriter,
|
|
private readonly LockManagerInterface $lockManager
|
|
) {
|
|
}
|
|
|
|
/** @return array{websites:int,pairs:int,mapped:int,unmapped:int,errors:array<int, string>} */
|
|
public function synchronizeAll(): array
|
|
{
|
|
if (!$this->lockManager->lock(self::LOCK_NAME, 0)) {
|
|
throw new \RuntimeException('ERP SKU synchronization is already running.');
|
|
}
|
|
$summary = ['websites' => 0, 'pairs' => 0, 'mapped' => 0, 'unmapped' => 0, 'errors' => []];
|
|
try {
|
|
foreach ($this->storeManager->getWebsites(false) as $website) {
|
|
$websiteId = (int)$website->getId();
|
|
if ($websiteId <= 0 || !$this->config->hasStoreMapping($websiteId)) {
|
|
continue;
|
|
}
|
|
$started = microtime(true);
|
|
$platformCode = $this->config->getPlatformCode($websiteId);
|
|
$storeId = $this->config->getLingxingStoreId($websiteId);
|
|
$request = [
|
|
'website_id' => $websiteId,
|
|
'platform_codes' => [(string)$platformCode],
|
|
'store_ids' => [$storeId],
|
|
];
|
|
try {
|
|
$mskus = $this->repository->getWebsiteProductSkus($websiteId);
|
|
$pairs = $this->pairFetcher->fetchAll(
|
|
$platformCode,
|
|
$storeId,
|
|
$mskus,
|
|
$this->config->getSkuSyncPageSize()
|
|
);
|
|
$result = $this->repository->updateWebsiteErpSkus(
|
|
$websiteId,
|
|
$platformCode,
|
|
$storeId,
|
|
$pairs
|
|
);
|
|
$summary['websites']++;
|
|
$summary['pairs'] += $result['pairs'];
|
|
$summary['mapped'] += $result['mapped'];
|
|
$summary['unmapped'] += $result['unmapped'];
|
|
$this->logWriter->write([
|
|
'direction' => 'pull',
|
|
'operation' => 'sku_sync',
|
|
'entity_type' => 'product',
|
|
'external_id' => $storeId,
|
|
'status' => 'success',
|
|
'attempt_number' => 1,
|
|
'request_payload' => $request,
|
|
'response_payload' => $result,
|
|
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
$summary['errors'][$websiteId] = $exception->getMessage();
|
|
$this->logWriter->write([
|
|
'direction' => 'pull',
|
|
'operation' => 'sku_sync',
|
|
'entity_type' => 'product',
|
|
'external_id' => $storeId,
|
|
'status' => 'failed',
|
|
'attempt_number' => 1,
|
|
'error_code' => (string)$exception->getCode(),
|
|
'error_message' => $exception->getMessage(),
|
|
'request_payload' => $request,
|
|
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
|
|
]);
|
|
}
|
|
}
|
|
} finally {
|
|
$this->lockManager->unlock(self::LOCK_NAME);
|
|
}
|
|
return $summary;
|
|
}
|
|
}
|