PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/src/Magento/Setup/Fixtures/BundleProductsFixture.php

https://bitbucket.org/KholoshaMaxim/kms_someapi2
PHP | 261 lines | 141 code | 25 blank | 95 comment | 2 complexity | f43c48202332f9377719cc306d5382d0 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Fixtures;
  7. use Magento\Bundle\Api\Data\LinkInterface;
  8. use Magento\Bundle\Model\Product\Type;
  9. use Magento\Catalog\Model\Product\Visibility;
  10. /**
  11. * Generate bundle products based on profile configuration
  12. * Generated bundle selections are not displayed individually in catalog
  13. * Support the following format:
  14. * <bundle_products>{products amount}</bundle_products>
  15. * <bundle_products_options>{bundle product options amount}</bundle_products_options>
  16. * <bundle_products_variation>{amount of simple products per each option}</bundle_products_variation>
  17. *
  18. * Products will be uniformly distributed per categories and websites
  19. * If node "assign_entities_to_all_websites" from profile is set to "1" then products will be assigned to all websites
  20. *
  21. * @see setup/performance-toolkit/profiles/ce/small.xml
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class BundleProductsFixture extends Fixture
  25. {
  26. /**
  27. * Bundle sku pattern with entity number and suffix. Suffix equals "{options}-{variations_per_option}"
  28. */
  29. const SKU_PATTERN = 'Bundle Product %s - %s';
  30. /**
  31. * @var int
  32. */
  33. protected $priority = 42;
  34. /**
  35. * @var \Magento\Setup\Model\FixtureGenerator\ProductGenerator
  36. */
  37. private $productGenerator;
  38. /**
  39. * @var \Magento\Setup\Model\FixtureGenerator\BundleProductGenerator
  40. */
  41. private $bundleProductGenerator;
  42. /**
  43. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  44. */
  45. private $productCollectionFactory;
  46. /**
  47. * @var int
  48. */
  49. private $productStartIndex;
  50. /**
  51. * @var ProductsAmountProvider
  52. */
  53. private $productsAmountProvider;
  54. /**
  55. * @var WebsiteCategoryProvider
  56. */
  57. private $websiteCategoryProvider;
  58. /**
  59. * @var PriceProvider
  60. */
  61. private $priceProvider;
  62. /**
  63. * @param FixtureModel $fixtureModel
  64. * @param \Magento\Setup\Model\FixtureGenerator\ProductGenerator $productGenerator
  65. * @param \Magento\Setup\Model\FixtureGenerator\BundleProductGenerator $bundleProductGenerator
  66. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  67. * @param ProductsAmountProvider $productsAmountProvider
  68. * @param WebsiteCategoryProvider $websiteCategoryProvider
  69. * @param PriceProvider $priceProvider
  70. */
  71. public function __construct(
  72. FixtureModel $fixtureModel,
  73. \Magento\Setup\Model\FixtureGenerator\ProductGenerator $productGenerator,
  74. \Magento\Setup\Model\FixtureGenerator\BundleProductGenerator $bundleProductGenerator,
  75. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
  76. ProductsAmountProvider $productsAmountProvider,
  77. WebsiteCategoryProvider $websiteCategoryProvider,
  78. PriceProvider $priceProvider
  79. ) {
  80. parent::__construct($fixtureModel);
  81. $this->productGenerator = $productGenerator;
  82. $this->bundleProductGenerator = $bundleProductGenerator;
  83. $this->productCollectionFactory = $productCollectionFactory;
  84. $this->productsAmountProvider = $productsAmountProvider;
  85. $this->websiteCategoryProvider = $websiteCategoryProvider;
  86. $this->priceProvider = $priceProvider;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  91. */
  92. public function execute()
  93. {
  94. $bundlesAmount = $this->fixtureModel->getValue('bundle_products', 0);
  95. $bundleOptions = $this->fixtureModel->getValue('bundle_products_options', 1);
  96. $bundleProductsPerOption = $this->fixtureModel->getValue('bundle_products_variation', 10);
  97. $bundleOptionSuffix = $bundleOptions . '-' . $bundleProductsPerOption;
  98. $variationCount = $bundleOptions * $bundleProductsPerOption;
  99. $bundlesAmount = $this->productsAmountProvider->getAmount(
  100. $bundlesAmount,
  101. $this->getBundleSkuPattern($bundleOptionSuffix)
  102. );
  103. if (!$bundlesAmount) {
  104. return;
  105. }
  106. $variationSkuClosure = function ($productId, $entityNumber) use ($bundleOptionSuffix, $variationCount) {
  107. $productIndex = $this->getBundleProductIndex($entityNumber, $variationCount);
  108. $variationIndex = $this->getBundleVariationIndex($entityNumber, $variationCount);
  109. return sprintf($this->getBundleOptionItemSkuPattern($bundleOptionSuffix), $productIndex, $variationIndex);
  110. };
  111. $fixtureMap = [
  112. 'name' => $variationSkuClosure,
  113. 'sku' => $variationSkuClosure,
  114. 'price' => function ($index, $entityNumber) {
  115. return $this->priceProvider->getPrice($entityNumber);
  116. },
  117. 'website_ids' => function ($index, $entityNumber) use ($variationCount) {
  118. $configurableIndex = $this->getBundleProductIndex($entityNumber, $variationCount);
  119. return $this->websiteCategoryProvider->getWebsiteIds($configurableIndex);
  120. },
  121. 'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
  122. ];
  123. $this->productGenerator->generate($bundlesAmount * $bundleOptions * $bundleProductsPerOption, $fixtureMap);
  124. $optionPriceType = [
  125. LinkInterface::PRICE_TYPE_FIXED,
  126. LinkInterface::PRICE_TYPE_PERCENT,
  127. ];
  128. $priceTypeClosure = function ($index) use ($optionPriceType) {
  129. return $optionPriceType[$index % count($optionPriceType)];
  130. };
  131. $skuClosure = function ($index, $entityNumber) use ($bundleOptionSuffix) {
  132. return sprintf(
  133. $this->getBundleSkuPattern($bundleOptionSuffix),
  134. $entityNumber + $this->getNewProductStartIndex()
  135. );
  136. };
  137. $fixtureMap = [
  138. '_bundle_options' => $bundleOptions,
  139. '_bundle_products_per_option' => $bundleProductsPerOption,
  140. '_bundle_variation_sku_pattern' => sprintf(
  141. $this->getBundleOptionItemSkuPattern($bundleOptionSuffix),
  142. $this->getNewProductStartIndex(),
  143. '%s'
  144. ),
  145. 'type_id' => Type::TYPE_CODE,
  146. 'name' => $skuClosure,
  147. 'sku' => $skuClosure,
  148. 'meta_title' => $skuClosure,
  149. 'price' => function ($index) use ($priceTypeClosure) {
  150. return $priceTypeClosure($index) === LinkInterface::PRICE_TYPE_PERCENT
  151. ? mt_rand(10, 90)
  152. : $this->priceProvider->getPrice($index);
  153. },
  154. 'priceType' => $priceTypeClosure,
  155. 'website_ids' => function ($index, $entityNumber) {
  156. return $this->websiteCategoryProvider->getWebsiteIds($entityNumber + $this->getNewProductStartIndex());
  157. },
  158. 'category_ids' => function ($index, $entityNumber) {
  159. return $this->websiteCategoryProvider->getCategoryId($entityNumber + $this->getNewProductStartIndex());
  160. },
  161. ];
  162. $this->bundleProductGenerator->generate($bundlesAmount, $fixtureMap);
  163. }
  164. /**
  165. * Get sku pattern for bundle product option item
  166. *
  167. * @param string $bundleOptionSuffix
  168. * @return string
  169. */
  170. private function getBundleOptionItemSkuPattern($bundleOptionSuffix)
  171. {
  172. return $this->getBundleSkuPattern($bundleOptionSuffix) . ' - option %s';
  173. }
  174. /**
  175. * Get sku pattern for bundle product. Replace suffix pattern with passed value
  176. *
  177. * @param string $bundleOptionSuffix
  178. * @return string
  179. */
  180. private function getBundleSkuPattern($bundleOptionSuffix)
  181. {
  182. return sprintf(self::SKU_PATTERN, '%s', $bundleOptionSuffix);
  183. }
  184. /**
  185. * Get start index for product number generation
  186. *
  187. * @return int
  188. */
  189. private function getNewProductStartIndex()
  190. {
  191. if (null === $this->productStartIndex) {
  192. $this->productStartIndex = $this->productCollectionFactory->create()
  193. ->addFieldToFilter('type_id', Type::TYPE_CODE)
  194. ->getSize() + 1;
  195. }
  196. return $this->productStartIndex;
  197. }
  198. /**
  199. * Get bundle product index number
  200. *
  201. * @param int $entityNumber
  202. * @param int $variationCount
  203. * @return float
  204. */
  205. private function getBundleProductIndex($entityNumber, $variationCount)
  206. {
  207. return floor($entityNumber / $variationCount) + $this->getNewProductStartIndex();
  208. }
  209. /**
  210. * Get bundle variation index number
  211. *
  212. * @param int $entityNumber
  213. * @param int $variationCount
  214. * @return float
  215. */
  216. private function getBundleVariationIndex($entityNumber, $variationCount)
  217. {
  218. return $entityNumber % $variationCount + 1;
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function getActionTitle()
  224. {
  225. return 'Generating bundle products';
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function introduceParamLabels()
  231. {
  232. return [
  233. 'bundle_products' => 'Bundle products',
  234. ];
  235. }
  236. }