36 lines
1.0 KiB
PHP
36 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Model\Warehouse;
|
|
|
|
use Magento\Framework\Lock\LockManagerInterface;
|
|
|
|
class WarehouseSynchronizer
|
|
{
|
|
private const LOCK_NAME = 'esun_lingxing_warehouse_sync';
|
|
|
|
public function __construct(
|
|
private readonly WarehouseFetcher $fetcher,
|
|
private readonly WarehouseRepository $repository,
|
|
private readonly LockManagerInterface $lockManager
|
|
) {
|
|
}
|
|
|
|
/** @return array{warehouses:int} */
|
|
public function synchronize(): array
|
|
{
|
|
if (!$this->lockManager->lock(self::LOCK_NAME, 0)) {
|
|
throw new \RuntimeException('Warehouse synchronization is already running.');
|
|
}
|
|
try {
|
|
$warehouses = $this->fetcher->fetchAll();
|
|
foreach ($warehouses as $warehouse) {
|
|
$this->repository->saveMetadata($warehouse);
|
|
}
|
|
return ['warehouses' => count($warehouses)];
|
|
} finally {
|
|
$this->lockManager->unlock(self::LOCK_NAME);
|
|
}
|
|
}
|
|
}
|