Collection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\BundleGraphQl\Model\Resolver\Options;
  8. use Magento\Bundle\Model\OptionFactory;
  9. use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. /**
  12. * Collection to fetch bundle option data at resolution time.
  13. */
  14. class Collection
  15. {
  16. /**
  17. * @var OptionFactory
  18. */
  19. private $bundleOptionFactory;
  20. /**
  21. * @var JoinProcessorInterface
  22. */
  23. private $extensionAttributesJoinProcessor;
  24. /**
  25. * @var StoreManagerInterface
  26. */
  27. private $storeManager;
  28. /**
  29. * @var string[]
  30. */
  31. private $skuMap = [];
  32. /**
  33. * @var array
  34. */
  35. private $optionMap = [];
  36. /**
  37. * @param OptionFactory $bundleOptionFactory
  38. * @param JoinProcessorInterface $extensionAttributesJoinProcessor
  39. * @param StoreManagerInterface $storeManager
  40. */
  41. public function __construct(
  42. OptionFactory $bundleOptionFactory,
  43. JoinProcessorInterface $extensionAttributesJoinProcessor,
  44. StoreManagerInterface $storeManager
  45. ) {
  46. $this->bundleOptionFactory = $bundleOptionFactory;
  47. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  48. $this->storeManager = $storeManager;
  49. }
  50. /**
  51. * Add parent id/sku pair to use for option filter at fetch time.
  52. *
  53. * @param int $parentId
  54. * @param int $parentEntityId
  55. * @param string $sku
  56. */
  57. public function addParentFilterData(int $parentId, int $parentEntityId, string $sku) : void
  58. {
  59. $this->skuMap[$parentId] = ['sku' => $sku, 'entity_id' => $parentEntityId];
  60. }
  61. /**
  62. * Fetch data for bundle options and return the options for the given parent id.
  63. *
  64. * @param int $parentId
  65. * @return array
  66. */
  67. public function getOptionsByParentId(int $parentId) : array
  68. {
  69. $options = $this->fetch();
  70. if (!isset($options[$parentId])) {
  71. return [];
  72. }
  73. return $options[$parentId];
  74. }
  75. /**
  76. * Fetch bundle option data and return in array format. Keys for bundle options will be their parent product ids.
  77. *
  78. * @return array
  79. */
  80. private function fetch() : array
  81. {
  82. if (empty($this->skuMap) || !empty($this->optionMap)) {
  83. return $this->optionMap;
  84. }
  85. /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */
  86. $optionsCollection = $this->bundleOptionFactory->create()->getResourceCollection();
  87. // All products in collection will have same store id.
  88. $optionsCollection->joinValues($this->storeManager->getStore()->getId());
  89. $productTable = $optionsCollection->getTable('catalog_product_entity');
  90. $linkField = $optionsCollection->getConnection()->getAutoIncrementField($productTable);
  91. $optionsCollection->getSelect()->join(
  92. ['cpe' => $productTable],
  93. 'cpe.'.$linkField.' = main_table.parent_id',
  94. []
  95. )->where(
  96. "cpe.entity_id IN (?)",
  97. $this->skuMap
  98. );
  99. $optionsCollection->setPositionOrder();
  100. $this->extensionAttributesJoinProcessor->process($optionsCollection);
  101. if (empty($optionsCollection->getData())) {
  102. return null;
  103. }
  104. /** @var \Magento\Bundle\Model\Option $option */
  105. foreach ($optionsCollection as $option) {
  106. if (!isset($this->optionMap[$option->getParentId()])) {
  107. $this->optionMap[$option->getParentId()] = [];
  108. }
  109. $this->optionMap[$option->getParentId()][$option->getId()] = $option->getData();
  110. $this->optionMap[$option->getParentId()][$option->getId()]['title']
  111. = $option->getTitle() === null ? $option->getDefaultTitle() : $option->getTitle();
  112. $this->optionMap[$option->getParentId()][$option->getId()]['sku']
  113. = $this->skuMap[$option->getParentId()]['sku'];
  114. }
  115. return $this->optionMap;
  116. }
  117. }