コード例 #1
0
ファイル: InventoryOperator.php プロジェクト: aleherse/Sylius
 /**
  * {@inheritdoc}
  */
 public function decrease($inventoryUnits)
 {
     if (!is_array($inventoryUnits) && !$inventoryUnits instanceof Collection) {
         throw new \InvalidArgumentException('Inventory units value must be array or instance of "Doctrine\\Common\\Collections\\Collection".');
     }
     $quantity = count($inventoryUnits);
     if ($quantity < 1) {
         throw new \InvalidArgumentException('Quantity of units must be greater than 0.');
     }
     if ($inventoryUnits instanceof Collection) {
         $stockable = $inventoryUnits->first()->getStockable();
     } else {
         $stockable = $inventoryUnits[0]->getStockable();
     }
     if (!$this->availabilityChecker->isStockSufficient($stockable, $quantity)) {
         throw new InsufficientStockException($stockable, $quantity);
     }
     $this->eventDispatcher->dispatch(SyliusStockableEvents::PRE_DECREASE, new GenericEvent($stockable));
     $this->backordersHandler->processBackorders($inventoryUnits);
     $onHand = $stockable->getOnHand();
     foreach ($inventoryUnits as $inventoryUnit) {
         if (InventoryUnitInterface::STATE_SOLD === $inventoryUnit->getInventoryState()) {
             --$onHand;
         }
     }
     $stockable->setOnHand($onHand);
     $this->eventDispatcher->dispatch(SyliusStockableEvents::POST_DECREASE, new GenericEvent($stockable));
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  *
  * Fills backorders using backorders handler.
  *
  * @param GenericEvent $event
  */
 public function onInventoryChange(GenericEvent $event)
 {
     $this->backordersHandler->fillBackorders($event->getSubject());
 }