123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\InventorySourceDeductionApi\Model;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\InventoryApi\Api\Data\SourceItemInterface;
- use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Retrieve source item from specific source by given SKU.
- */
- class GetSourceItemBySourceCodeAndSku
- {
- /**
- * @var SourceItemRepositoryInterface
- */
- private $sourceItemRepository;
- /**
- * @var SearchCriteriaBuilder
- */
- private $searchCriteriaBuilder;
- /**
- * @param SourceItemRepositoryInterface $sourceItemRepository
- * @param SearchCriteriaBuilder $searchCriteriaBuilder
- */
- public function __construct(
- SourceItemRepositoryInterface $sourceItemRepository,
- SearchCriteriaBuilder $searchCriteriaBuilder
- ) {
- $this->sourceItemRepository = $sourceItemRepository;
- $this->searchCriteriaBuilder = $searchCriteriaBuilder;
- }
- /**
- * Returns source item from specific source by given SKU. Return null if source item is not found
- *
- * @param string $sourceCode
- * @param string $sku
- * @return SourceItemInterface
- * @throws NoSuchEntityException
- */
- public function execute(string $sourceCode, string $sku): SourceItemInterface
- {
- $searchCriteria = $this->searchCriteriaBuilder
- ->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCode)
- ->addFilter(SourceItemInterface::SKU, $sku)
- ->create();
- $sourceItemsResult = $this->sourceItemRepository->getList($searchCriteria);
- if ($sourceItemsResult->getTotalCount() === 0) {
- throw new NoSuchEntityException(
- __('Source item not found by source code: %1 and sku: %2.', $sourceCode, $sku)
- );
- }
- return current($sourceItemsResult->getItems());
- }
- }
|