getStockItemConfiguration = $getStockItemConfig; $this->getStockItemData = $getStockItemData; $this->getReservationsQuantity = $getReservationsQuantity; $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType; $this->getProductTypesBySkus = $getProductTypesBySkus; } /** * @inheritdoc */ public function execute(string $sku, int $stockId): float { $this->validateProductType($sku); $stockItemData = $this->getStockItemData->execute($sku, $stockId); if (null === $stockItemData || (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] === false) { return 0; } $stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId); $minQty = $stockItemConfig->getMinQty(); $productQtyInStock = $stockItemData[GetStockItemDataInterface::QUANTITY] + $this->getReservationsQuantity->execute($sku, $stockId) - $minQty; return $productQtyInStock; } /** * Check if source can be managed for a product of specific type. * * @param string $sku * @throws InputException */ private function validateProductType(string $sku): void { $productTypesBySkus = $this->getProductTypesBySkus->execute([$sku]); if (!array_key_exists($sku, $productTypesBySkus)) { throw new NoSuchEntityException( __('The product that was requested doesn\'t exist. Verify the product and try again.') ); } $productType = $productTypesBySkus[$sku]; if ($productType && false === $this->isSourceItemManagementAllowedForProductType->execute($productType)) { throw new InputException( __('Can\'t check requested quantity for products without Source Items support.') ); } } }