AggregationResolver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\Adapter\Aggregation;
  7. use Magento\Catalog\Api\AttributeSetFinderInterface;
  8. use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
  9. use Magento\Framework\Api\SearchCriteriaBuilder;
  10. use Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface;
  11. use Magento\Framework\Search\Request\BucketInterface;
  12. use Magento\Framework\Search\Request\Config;
  13. use Magento\Framework\Search\RequestInterface;
  14. use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection as AttributeCollection;
  15. /**
  16. * Aggregation resolver.
  17. */
  18. class AggregationResolver implements AggregationResolverInterface
  19. {
  20. /**
  21. * @var AttributeSetFinderInterface
  22. */
  23. private $attributeSetFinder;
  24. /**
  25. * @var ProductAttributeRepositoryInterface
  26. */
  27. private $productAttributeRepository;
  28. /**
  29. * @var SearchCriteriaBuilder
  30. */
  31. private $searchCriteriaBuilder;
  32. /**
  33. * @var Config
  34. */
  35. private $config;
  36. /**
  37. * @var RequestCheckerInterface
  38. */
  39. private $requestChecker;
  40. /**
  41. * @var AttributeCollection
  42. */
  43. private $attributeCollection;
  44. /**
  45. * AggregationResolver constructor
  46. *
  47. * @param AttributeSetFinderInterface $attributeSetFinder
  48. * @param ProductAttributeRepositoryInterface $productAttributeRepository
  49. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  50. * @param Config $config
  51. * @param AttributeCollection $attributeCollection [optional]
  52. * @param RequestCheckerInterface|null $aggregationChecker
  53. */
  54. public function __construct(
  55. AttributeSetFinderInterface $attributeSetFinder,
  56. ProductAttributeRepositoryInterface $productAttributeRepository,
  57. SearchCriteriaBuilder $searchCriteriaBuilder,
  58. Config $config,
  59. AttributeCollection $attributeCollection = null,
  60. RequestCheckerInterface $aggregationChecker = null
  61. ) {
  62. $this->attributeSetFinder = $attributeSetFinder;
  63. $this->productAttributeRepository = $productAttributeRepository;
  64. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  65. $this->config = $config;
  66. $this->attributeCollection = $attributeCollection
  67. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeCollection::class);
  68. $this->requestChecker = $aggregationChecker ?: \Magento\Framework\App\ObjectManager::getInstance()
  69. ->get(RequestCheckerInterface::class);
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public function resolve(RequestInterface $request, array $documentIds)
  75. {
  76. if (!$this->requestChecker->isApplicable($request)) {
  77. return [];
  78. }
  79. $data = $this->config->get($request->getName());
  80. $bucketKeys = isset($data['aggregations']) ? array_keys($data['aggregations']) : [];
  81. $attributeCodes = $this->getApplicableAttributeCodes($documentIds);
  82. $resolvedAggregation = array_filter(
  83. $request->getAggregation(),
  84. function ($bucket) use ($attributeCodes, $bucketKeys) {
  85. /** @var BucketInterface $bucket */
  86. return in_array($bucket->getField(), $attributeCodes, true) ||
  87. in_array($bucket->getName(), $bucketKeys, true);
  88. }
  89. );
  90. return array_values($resolvedAggregation);
  91. }
  92. /**
  93. * Get applicable attributes
  94. *
  95. * @param array $documentIds
  96. * @return array
  97. */
  98. private function getApplicableAttributeCodes(array $documentIds)
  99. {
  100. $attributeSetIds = $this->attributeSetFinder->findAttributeSetIdsByProductIds($documentIds);
  101. $this->attributeCollection->setAttributeSetFilter($attributeSetIds);
  102. $this->attributeCollection->setEntityTypeFilter(
  103. \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE
  104. );
  105. $this->attributeCollection->getSelect()
  106. ->reset(\Magento\Framework\DB\Select::COLUMNS)
  107. ->columns('attribute_code');
  108. return $this->attributeCollection->getConnection()->fetchCol($this->attributeCollection->getSelect());
  109. }
  110. }