GetStockBySalesChannel.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySales\Model;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\InventoryApi\Api\Data\StockInterface;
  10. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  11. use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface;
  12. use Magento\InventoryApi\Api\StockRepositoryInterface;
  13. use Magento\InventorySales\Model\ResourceModel\StockIdResolver;
  14. /**
  15. * @inheritdoc
  16. */
  17. class GetStockBySalesChannel implements GetStockBySalesChannelInterface
  18. {
  19. /**
  20. * @var StockRepositoryInterface
  21. */
  22. private $stockRepository;
  23. /**
  24. * @var StockIdResolver
  25. */
  26. private $stockIdResolver;
  27. /**
  28. * @param StockRepositoryInterface $stockRepositoryInterface
  29. * @param StockIdResolver $stockIdResolver
  30. */
  31. public function __construct(
  32. StockRepositoryInterface $stockRepositoryInterface,
  33. StockIdResolver $stockIdResolver
  34. ) {
  35. $this->stockRepository = $stockRepositoryInterface;
  36. $this->stockIdResolver = $stockIdResolver;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function execute(SalesChannelInterface $salesChannel): StockInterface
  42. {
  43. $stockId = $this->stockIdResolver->resolve(
  44. $salesChannel->getType(),
  45. $salesChannel->getCode()
  46. );
  47. if (null === $stockId) {
  48. throw new NoSuchEntityException(__('No linked stock found'));
  49. }
  50. return $this->stockRepository->get($stockId);
  51. }
  52. }