Collection.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Links;
  8. use Magento\Bundle\Model\Selection;
  9. use Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory;
  10. use Magento\Bundle\Model\ResourceModel\Selection\Collection as LinkCollection;
  11. use Magento\Framework\GraphQl\Query\EnumLookup;
  12. /**
  13. * Collection to fetch link data at resolution time.
  14. */
  15. class Collection
  16. {
  17. /**
  18. * @var CollectionFactory
  19. */
  20. private $linkCollectionFactory;
  21. /**
  22. * @var EnumLookup
  23. */
  24. private $enumLookup;
  25. /**
  26. * @var int[]
  27. */
  28. private $optionIds = [];
  29. /**
  30. * @var int[]
  31. */
  32. private $parentIds = [];
  33. /**
  34. * @var array
  35. */
  36. private $links = [];
  37. /**
  38. * @param CollectionFactory $linkCollectionFactory
  39. * @param EnumLookup $enumLookup
  40. */
  41. public function __construct(CollectionFactory $linkCollectionFactory, EnumLookup $enumLookup)
  42. {
  43. $this->linkCollectionFactory = $linkCollectionFactory;
  44. $this->enumLookup = $enumLookup;
  45. }
  46. /**
  47. * Add option and id filter pair to filter for fetch.
  48. *
  49. * @param int $optionId
  50. * @param int $parentId
  51. * @return void
  52. */
  53. public function addIdFilters(int $optionId, int $parentId) : void
  54. {
  55. if (!in_array($optionId, $this->optionIds)) {
  56. $this->optionIds[] = $optionId;
  57. }
  58. if (!in_array($parentId, $this->parentIds)) {
  59. $this->parentIds[] = $parentId;
  60. }
  61. }
  62. /**
  63. * Retrieve links for passed in option id.
  64. *
  65. * @param int $optionId
  66. * @return array
  67. */
  68. public function getLinksForOptionId(int $optionId) : array
  69. {
  70. $linksList = $this->fetch();
  71. if (!isset($linksList[$optionId])) {
  72. return [];
  73. }
  74. return $linksList[$optionId];
  75. }
  76. /**
  77. * Fetch link data and return in array format. Keys for links will be their option Ids.
  78. *
  79. * @return array
  80. */
  81. private function fetch() : array
  82. {
  83. if (empty($this->optionIds) || empty($this->parentIds) || !empty($this->links)) {
  84. return $this->links;
  85. }
  86. /** @var LinkCollection $linkCollection */
  87. $linkCollection = $this->linkCollectionFactory->create();
  88. $linkCollection->setOptionIdsFilter($this->optionIds);
  89. $field = 'parent_product_id';
  90. foreach ($linkCollection->getSelect()->getPart('from') as $tableAlias => $data) {
  91. if ($data['tableName'] == $linkCollection->getTable('catalog_product_bundle_selection')) {
  92. $field = $tableAlias . '.' . $field;
  93. }
  94. }
  95. $linkCollection->getSelect()
  96. ->where($field . ' IN (?)', $this->parentIds);
  97. /** @var Selection $link */
  98. foreach ($linkCollection as $link) {
  99. $data = $link->getData();
  100. $formattedLink = [
  101. 'price' => $link->getSelectionPriceValue(),
  102. 'position' => $link->getPosition(),
  103. 'id' => $link->getSelectionId(),
  104. 'qty' => (int)$link->getSelectionQty(),
  105. 'is_default' => (bool)$link->getIsDefault(),
  106. 'price_type' => $this->enumLookup->getEnumValueFromField(
  107. 'PriceTypeEnum',
  108. (string)$link->getSelectionPriceType()
  109. ) ?: 'DYNAMIC',
  110. 'can_change_quantity' => $link->getSelectionCanChangeQty(),
  111. ];
  112. $data = array_replace($data, $formattedLink);
  113. if (!isset($this->links[$link->getOptionId()])) {
  114. $this->links[$link->getOptionId()] = [];
  115. }
  116. $this->links[$link->getOptionId()][] = $data;
  117. }
  118. return $this->links;
  119. }
  120. }