searchCriteriaBuilder = $searchCriteriaBuilder; $this->sourceRepository = $sourceRepository; $this->getStockSourceLinks = $getStockSourceLinks; $this->logger = $logger; $this->sortOrderBuilder = $sortOrderBuilder; } /** * @inheritdoc */ public function execute(int $stockId): array { try { $stockSourceLinks = $this->getStockSourceLinks($stockId); $sources = []; foreach ($stockSourceLinks as $link) { $sources[] = $this->sourceRepository->get($link->getSourceCode()); } return $sources; } catch (\Exception $e) { $this->logger->error($e->getMessage()); throw new LocalizedException(__('Could not load Sources for Stock'), $e); } } /** * Get all stock-source links by given stockId * * @param int $stockId * @return array */ private function getStockSourceLinks(int $stockId): array { $sortOrder = $this->sortOrderBuilder ->setField(StockSourceLinkInterface::PRIORITY) ->setAscendingDirection() ->create(); $searchCriteria = $this->searchCriteriaBuilder ->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId) ->addSortOrder($sortOrder) ->create(); $searchResult = $this->getStockSourceLinks->execute($searchCriteria); return $searchResult->getItems(); } }