itemRequestFactory = $itemRequestFactory; $this->inventoryRequestFactory = $inventoryRequestFactory; $this->sourceSelectionService = $sourceSelectionService; $this->getDefaultSourceSelectionAlgorithmCode = $getDefaultSourceSelectionAlgorithmCode; $this->sourceRepository = $sourceRepository; } /** * @param int $stockId * @param string $sku * @param float $qty * @return array * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.LongVariable) */ public function execute(int $stockId, string $sku, float $qty): array { $algorithmCode = $this->getDefaultSourceSelectionAlgorithmCode->execute(); $requestItem = $this->itemRequestFactory->create([ 'sku' => $sku, 'qty' => $qty ]); $inventoryRequest = $this->inventoryRequestFactory->create([ 'stockId' => $stockId, 'items' => [$requestItem] ]); $sourceSelectionResult = $this->sourceSelectionService->execute( $inventoryRequest, $algorithmCode ); $result = []; foreach ($sourceSelectionResult->getSourceSelectionItems() as $item) { $sourceCode = $item->getSourceCode(); $result[] = [ 'sourceName' => $this->getSourceName($sourceCode), 'sourceCode' => $sourceCode, 'qtyAvailable' => $item->getQtyAvailable(), 'qtyToDeduct' => $item->getQtyToDeduct() ]; } return $result; } /** * Get source name by code * * @param string $sourceCode * @return mixed * @throws NoSuchEntityException */ private function getSourceName(string $sourceCode): string { if (!isset($this->sources[$sourceCode])) { $this->sources[$sourceCode] = $this->sourceRepository->get($sourceCode)->getName(); } return $this->sources[$sourceCode]; } }