GetStockItemConfiguration.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\InventoryConfiguration\Model;
  8. use Magento\InventoryApi\Model\IsProductAssignedToStockInterface;
  9. use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
  10. use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
  11. use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
  12. use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException;
  13. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForSkuInterface;
  14. /**
  15. * @inheritdoc
  16. */
  17. class GetStockItemConfiguration implements GetStockItemConfigurationInterface
  18. {
  19. /**
  20. * @var GetLegacyStockItem
  21. */
  22. private $getLegacyStockItem;
  23. /**
  24. * @var StockItemConfigurationFactory
  25. */
  26. private $stockItemConfigurationFactory;
  27. /**
  28. * @var IsProductAssignedToStockInterface
  29. */
  30. private $isProductAssignedToStock;
  31. /**
  32. * @var DefaultStockProviderInterface
  33. */
  34. private $defaultStockProvider;
  35. /**
  36. * @var IsSourceItemManagementAllowedForSkuInterface
  37. */
  38. private $isSourceItemManagementAllowedForSku;
  39. /**
  40. * @param GetLegacyStockItem $getLegacyStockItem
  41. * @param StockItemConfigurationFactory $stockItemConfigurationFactory
  42. * @param IsProductAssignedToStockInterface $isProductAssignedToStock
  43. * @param DefaultStockProviderInterface $defaultStockProvider
  44. * @param IsSourceItemManagementAllowedForSkuInterface $isSourceItemManagementAllowedForSku
  45. */
  46. public function __construct(
  47. GetLegacyStockItem $getLegacyStockItem,
  48. StockItemConfigurationFactory $stockItemConfigurationFactory,
  49. IsProductAssignedToStockInterface $isProductAssignedToStock,
  50. DefaultStockProviderInterface $defaultStockProvider,
  51. IsSourceItemManagementAllowedForSkuInterface $isSourceItemManagementAllowedForSku
  52. ) {
  53. $this->getLegacyStockItem = $getLegacyStockItem;
  54. $this->stockItemConfigurationFactory = $stockItemConfigurationFactory;
  55. $this->isProductAssignedToStock = $isProductAssignedToStock;
  56. $this->defaultStockProvider = $defaultStockProvider;
  57. $this->isSourceItemManagementAllowedForSku = $isSourceItemManagementAllowedForSku;
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function execute(string $sku, int $stockId): StockItemConfigurationInterface
  63. {
  64. if ($this->defaultStockProvider->getId() !== $stockId
  65. && true === $this->isSourceItemManagementAllowedForSku->execute($sku)
  66. && false === $this->isProductAssignedToStock->execute($sku, $stockId)) {
  67. throw new SkuIsNotAssignedToStockException(
  68. __('The requested sku is not assigned to given stock.')
  69. );
  70. }
  71. return $this->stockItemConfigurationFactory->create(
  72. [
  73. 'stockItem' => $this->getLegacyStockItem->execute($sku)
  74. ]
  75. );
  76. }
  77. }