ProductBuilder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Integration\Builder;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Catalog\Api\Data\ProductInterfaceFactory;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Catalog\Api\ProductRepositoryInterfaceFactory;
  11. use Magento\Catalog\Model\Product\Attribute\Source\Status;
  12. use Magento\Catalog\Model\Product\Type;
  13. use Magento\Catalog\Model\Product\Visibility;
  14. use Magento\CatalogInventory\Api\StockRegistryInterface;
  15. use Magento\CatalogInventory\Model\StockRegistryStorage;
  16. /**
  17. * Build a product with stock
  18. */
  19. class ProductBuilder
  20. {
  21. const EXAMPLE_PRODUCT_ATTRIBUTE_SET = 4;
  22. const EXAMPLE_PRODUCT_NAME = 'Example Product';
  23. const EXAMPLE_PRODUCT_PRICE = 5.00;
  24. const EXAMPLE_PRODUCT_SKU = 'TEST';
  25. const EXAMPLE_PRODUCT_STATUS = Status::STATUS_ENABLED;
  26. const EXAMPLE_PRODUCT_TYPE = Type::TYPE_SIMPLE;
  27. const EXAMPLE_PRODUCT_VISIBILITY = Visibility::VISIBILITY_BOTH;
  28. /** @var ProductInterfaceFactory */
  29. private $productFactory;
  30. /** @var ProductRepositoryInterface */
  31. private $productRepositoryFactory;
  32. /** @var StockRegistryInterface */
  33. private $stockRegistry;
  34. /** @var StockRegistryStorage */
  35. private $stockRegistryStorage;
  36. /**
  37. * @param ProductInterfaceFactory $productFactory
  38. * @param ProductRepositoryInterfaceFactory $productRepositoryFactory
  39. * @param StockRegistryInterface $stockRegistry
  40. * @param StockRegistryStorage $stockRegistryStorage
  41. */
  42. public function __construct(
  43. ProductInterfaceFactory $productFactory,
  44. ProductRepositoryInterfaceFactory $productRepositoryFactory,
  45. StockRegistryInterface $stockRegistry,
  46. StockRegistryStorage $stockRegistryStorage
  47. ) {
  48. $this->productFactory = $productFactory;
  49. $this->productRepositoryFactory = $productRepositoryFactory;
  50. $this->stockRegistry = $stockRegistry;
  51. $this->stockRegistryStorage = $stockRegistryStorage;
  52. }
  53. /**
  54. * Create an example product including stock
  55. *
  56. * Performs 3 database queries
  57. *
  58. * @param callable $productConfiguration Receives 1 parameter of ProductInterface. Should return a ProductInterface
  59. * @param bool $isInStock
  60. * @param int $stockQty
  61. * @return ProductInterface
  62. * @throws \Magento\Framework\Exception\CouldNotSaveException
  63. * @throws \Magento\Framework\Exception\InputException
  64. * @throws \Magento\Framework\Exception\NoSuchEntityException
  65. * @throws \Magento\Framework\Exception\StateException
  66. */
  67. public function createExampleProduct(callable $productConfiguration = null, $isInStock = true, $stockQty = 500)
  68. {
  69. return $this->createProduct(
  70. function (ProductInterface $product) use ($productConfiguration) {
  71. $product->setName(static::EXAMPLE_PRODUCT_NAME);
  72. $product->setSku(static::EXAMPLE_PRODUCT_SKU);
  73. $product->setPrice(static::EXAMPLE_PRODUCT_PRICE);
  74. $product->setVisibility(static::EXAMPLE_PRODUCT_VISIBILITY);
  75. $product->setStatus(static::EXAMPLE_PRODUCT_STATUS);
  76. $product->setTypeId(static::EXAMPLE_PRODUCT_TYPE);
  77. $product->setAttributeSetId(static::EXAMPLE_PRODUCT_ATTRIBUTE_SET);
  78. return $productConfiguration !== null ? $productConfiguration($product) : $product;
  79. },
  80. $isInStock,
  81. $stockQty
  82. );
  83. }
  84. /**
  85. * Create a product including stock
  86. *
  87. * Performs 3 database queries.
  88. *
  89. * @param callable $productConfiguration Receives 1 parameter of ProductInterface. Should return a ProductInterface
  90. * @param bool $isInStock
  91. * @param int $stockQty
  92. * @return ProductInterface
  93. * @throws \Magento\Framework\Exception\CouldNotSaveException
  94. * @throws \Magento\Framework\Exception\InputException
  95. * @throws \Magento\Framework\Exception\NoSuchEntityException
  96. * @throws \Magento\Framework\Exception\StateException
  97. */
  98. public function createProduct(callable $productConfiguration, $isInStock = true, $stockQty = 500)
  99. {
  100. /** @var ProductInterface $product */
  101. $product = $this->productFactory->create();
  102. $product = $productConfiguration($product);
  103. if (!($product instanceof ProductInterface)) {
  104. throw new \TypeError('Result of createProduct callback must return a ProductInterface');
  105. }
  106. /** @var ProductRepositoryInterface $productRepository */
  107. $productRepository = $this->productRepositoryFactory->create();
  108. $product = $productRepository->save($product);
  109. $stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());
  110. $stockItem->setIsInStock($isInStock);
  111. $stockItem->setQty($stockQty);
  112. $this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
  113. // Reload product in such a way as to re-generate saleability data
  114. $this->stockRegistryStorage->removeStockStatus($product->getId());
  115. $this->stockRegistryStorage->removeStockItem($product->getId());
  116. $product = $productRepository->get($product->getSku());
  117. return $product;
  118. }
  119. }