Get.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Inventory\Model\Stock\Command;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Inventory\Model\ResourceModel\Stock as StockResourceModel;
  10. use Magento\InventoryApi\Api\Data\StockInterface;
  11. use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
  12. /**
  13. * @inheritdoc
  14. */
  15. class Get implements GetInterface
  16. {
  17. /**
  18. * @var StockResourceModel
  19. */
  20. private $stockResource;
  21. /**
  22. * @var StockInterfaceFactory
  23. */
  24. private $stockFactory;
  25. /**
  26. * @param StockResourceModel $stockResource
  27. * @param StockInterfaceFactory $stockFactory
  28. */
  29. public function __construct(
  30. StockResourceModel $stockResource,
  31. StockInterfaceFactory $stockFactory
  32. ) {
  33. $this->stockResource = $stockResource;
  34. $this->stockFactory = $stockFactory;
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function execute(int $stockId): StockInterface
  40. {
  41. /** @var StockInterface $stock */
  42. $stock = $this->stockFactory->create();
  43. $this->stockResource->load($stock, $stockId, StockInterface::STOCK_ID);
  44. if (null === $stock->getStockId()) {
  45. throw new NoSuchEntityException(__('Stock with id "%value" does not exist.', ['value' => $stockId]));
  46. }
  47. return $stock;
  48. }
  49. }