123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Inventory\Model\Source\Command;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Framework\Api\SortOrderBuilder;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
- use Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface;
- use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
- use Magento\InventoryApi\Api\SourceRepositoryInterface;
- use Psr\Log\LoggerInterface;
- /**
- * @inheritdoc
- */
- class GetSourcesAssignedToStockOrderedByPriority implements GetSourcesAssignedToStockOrderedByPriorityInterface
- {
- /**
- * @var SearchCriteriaBuilder
- */
- private $searchCriteriaBuilder;
- /**
- * @var SourceRepositoryInterface
- */
- private $sourceRepository;
- /**
- * @var GetStockSourceLinksInterface
- */
- private $getStockSourceLinks;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var SortOrderBuilder
- */
- private $sortOrderBuilder;
- /**
- * @param SearchCriteriaBuilder $searchCriteriaBuilder
- * @param SourceRepositoryInterface $sourceRepository
- * @param GetStockSourceLinksInterface $getStockSourceLinks
- * @param SortOrderBuilder $sortOrderBuilder
- * @param LoggerInterface $logger
- */
- public function __construct(
- SearchCriteriaBuilder $searchCriteriaBuilder,
- SourceRepositoryInterface $sourceRepository,
- GetStockSourceLinksInterface $getStockSourceLinks,
- SortOrderBuilder $sortOrderBuilder,
- LoggerInterface $logger
- ) {
- $this->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();
- }
- }
|