Skip to content

Commit

Permalink
Update the inventory updater to make sure the InventoryUpdate entity …
Browse files Browse the repository at this point in the history
…is synced
  • Loading branch information
loevgaard committed Sep 16, 2024
1 parent df0662a commit 7b3fa06
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Updater/InventoryUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Setono\PeakWMS\Client\ClientInterface;
use Setono\PeakWMS\DataTransferObject\Product\Product;
use Setono\PeakWMS\Request\Query\Product\PageQuery;
use Setono\SyliusPeakPlugin\Model\InventoryUpdateInterface;
use Setono\SyliusPeakPlugin\Provider\InventoryUpdateProviderInterface;
use Setono\SyliusPeakPlugin\Workflow\InventoryUpdateWorkflow;
use Sylius\Component\Core\Model\ProductVariant;
Expand Down Expand Up @@ -62,21 +61,17 @@ public function update(ProductVariantInterface $productVariant): void
$this->getManager($productVariant)->flush();
}

// todo what happens if the transitions are not possible?
public function updateAll(bool $onlyUpdated = true): void
{
$inventoryUpdate = $this->inventoryUpdateProvider->getInventoryUpdate();
$this->getManager($inventoryUpdate)->persist($inventoryUpdate);

if (!$this->inventoryUpdateWorkflow->can($inventoryUpdate, InventoryUpdateWorkflow::TRANSITION_RESET)) {
throw new \RuntimeException('The inventory update cannot be reset');
}
$manager = $this->getManager($inventoryUpdate);
$manager->persist($inventoryUpdate);
$manager->flush();

try {
$this->transition($inventoryUpdate, InventoryUpdateWorkflow::TRANSITION_RESET);
$this->transition($inventoryUpdate, InventoryUpdateWorkflow::TRANSITION_PROCESS);
$this->inventoryUpdateTransition(InventoryUpdateWorkflow::TRANSITION_RESET);
$this->inventoryUpdateTransition(InventoryUpdateWorkflow::TRANSITION_PROCESS);

$manager = $this->getManager(ProductVariant::class);
$productVariantRepository = $this->getRepository(ProductVariant::class);

$i = 0;
Expand All @@ -85,10 +80,13 @@ public function updateAll(bool $onlyUpdated = true): void
++$i;

if ($i % 100 === 0) {
$inventoryUpdate->setProductsProcessed($i);

$manager->flush();
$manager->clear();

$inventoryUpdate->setProductsProcessed($i);
// We need to get the inventory update again because the previous one is detached
$inventoryUpdate = $this->inventoryUpdateProvider->getInventoryUpdate();
}

try {
Expand Down Expand Up @@ -121,17 +119,18 @@ public function updateAll(bool $onlyUpdated = true): void
$inventoryUpdate->setProductsProcessed($i);
$manager->flush();

$this->transition($inventoryUpdate, InventoryUpdateWorkflow::TRANSITION_COMPLETE);
$this->inventoryUpdateTransition(InventoryUpdateWorkflow::TRANSITION_COMPLETE);
} catch (\Throwable $e) {
$inventoryUpdate->addError($e->getMessage());
$this->transition($inventoryUpdate, InventoryUpdateWorkflow::TRANSITION_FAIL);
$this->inventoryUpdateTransition(InventoryUpdateWorkflow::TRANSITION_FAIL);
} finally {
$this->getManager($inventoryUpdate)->flush();
$manager->flush();
}
}

private function transition(InventoryUpdateInterface $inventoryUpdate, string $transition): void
private function inventoryUpdateTransition(string $transition): void
{
$inventoryUpdate = $this->inventoryUpdateProvider->getInventoryUpdate();
$this->inventoryUpdateWorkflow->apply($inventoryUpdate, $transition);

$this->getManager($inventoryUpdate)->flush();
Expand Down

0 comments on commit 7b3fa06

Please sign in to comment.