Files
esun3d/app/code/Esun/LingxingErp/Model/Store/StoreSynchronizer.php
T
2026-09-15 10:35:56 +08:00

50 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Store;
use Esun\LingxingErp\Model\Api\ApiException;
use Esun\LingxingErp\Model\Sync\LogWriter;
class StoreSynchronizer
{
public function __construct(
private readonly StoreFetcher $fetcher,
private readonly StoreRepository $repository,
private readonly LogWriter $logWriter
) {
}
public function synchronize(): int
{
$started = microtime(true);
try {
$stores = $this->fetcher->fetchAll();
$this->repository->synchronize($stores);
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'store_sync',
'entity_type' => 'store',
'status' => 'success',
'response_payload' => ['store_count' => count($stores)],
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
]);
return count($stores);
} catch (\Throwable $exception) {
$code = $exception instanceof ApiException ? $exception->getErrorCode() : (string)$exception->getCode();
$response = $exception instanceof ApiException ? $exception->getResponse() : [];
$this->logWriter->write([
'direction' => 'pull',
'operation' => 'store_sync',
'entity_type' => 'store',
'status' => 'failed',
'error_code' => $code,
'error_message' => $exception->getMessage(),
'response_payload' => $response,
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
]);
throw $exception;
}
}
}