} */ 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; } }