init
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user