|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\CategoryListInterface; |
| 11 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 12 | +use Magento\Framework\Search\Response\Aggregation; |
| 13 | +use Magento\Framework\Search\Response\AggregationFactory; |
| 14 | +use Magento\Framework\Search\Response\BucketFactory; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Framework\Api\Search\AggregationInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class to include only direct subcategories of category in aggregation |
| 20 | + */ |
| 21 | +class IncludeDirectChildrenOnly |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private const CATEGORY_BUCKET = 'category_bucket'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private const BUCKETS_NAME = 'buckets'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var AggregationFactory |
| 35 | + */ |
| 36 | + private $aggregationFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var BucketFactory |
| 40 | + */ |
| 41 | + private $bucketFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var StoreManagerInterface |
| 45 | + */ |
| 46 | + private $storeManager; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var CategoryListInterface |
| 50 | + */ |
| 51 | + private $categoryList; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var array |
| 55 | + */ |
| 56 | + private $filter = []; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var SearchCriteriaBuilder |
| 60 | + */ |
| 61 | + private $searchCriteriaBuilder; |
| 62 | + |
| 63 | + /** |
| 64 | + * @param AggregationFactory $aggregationFactory |
| 65 | + * @param BucketFactory $bucketFactory |
| 66 | + * @param StoreManagerInterface $storeManager |
| 67 | + * @param CategoryListInterface $categoryList |
| 68 | + * @param SearchCriteriaBuilder $searchCriteriaBuilder |
| 69 | + */ |
| 70 | + public function __construct( |
| 71 | + AggregationFactory $aggregationFactory, |
| 72 | + BucketFactory $bucketFactory, |
| 73 | + StoreManagerInterface $storeManager, |
| 74 | + CategoryListInterface $categoryList, |
| 75 | + SearchCriteriaBuilder $searchCriteriaBuilder |
| 76 | + ) { |
| 77 | + $this->aggregationFactory = $aggregationFactory; |
| 78 | + $this->bucketFactory = $bucketFactory; |
| 79 | + $this->storeManager = $storeManager; |
| 80 | + $this->categoryList = $categoryList; |
| 81 | + $this->searchCriteriaBuilder = $searchCriteriaBuilder; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Filter category aggregation to include only direct subcategories of requested category |
| 86 | + * |
| 87 | + * @param AggregationInterface $aggregation |
| 88 | + * @param int|null $storeId |
| 89 | + * @return Aggregation |
| 90 | + */ |
| 91 | + public function filter(AggregationInterface $aggregation, ?int $storeId): Aggregation |
| 92 | + { |
| 93 | + $categoryIdsRequested = $this->filter['category'] ?? null; |
| 94 | + if ($categoryIdsRequested === null) { |
| 95 | + return $aggregation; |
| 96 | + } |
| 97 | + $buckets = $aggregation->getBuckets(); |
| 98 | + $categoryBucket = $buckets[self::CATEGORY_BUCKET] ?? null; |
| 99 | + if ($categoryBucket === null || empty($categoryBucket->getValues())) { |
| 100 | + return $aggregation; |
| 101 | + } |
| 102 | + $categoryIdsRequested = is_array($categoryIdsRequested) ? $categoryIdsRequested : [$categoryIdsRequested]; |
| 103 | + $bucketValuesFiltered = $this->filterBucketValues( |
| 104 | + $categoryBucket->getValues(), |
| 105 | + $categoryIdsRequested, |
| 106 | + $storeId |
| 107 | + ); |
| 108 | + $categoryBucketResolved = $this->bucketFactory->create( |
| 109 | + [ |
| 110 | + 'name' => self::CATEGORY_BUCKET, |
| 111 | + 'values' => $bucketValuesFiltered |
| 112 | + ] |
| 113 | + ); |
| 114 | + $buckets[self::CATEGORY_BUCKET] = $categoryBucketResolved; |
| 115 | + return $this->aggregationFactory->create([self::BUCKETS_NAME => $buckets]); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Set filter for categories aggregation |
| 120 | + * |
| 121 | + * @param array $filter |
| 122 | + */ |
| 123 | + public function setFilter(array $filter): void |
| 124 | + { |
| 125 | + $this->filter = $filter; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Filter bucket values to include only direct subcategories of requested category |
| 130 | + * |
| 131 | + * @param array $categoryBucketValues |
| 132 | + * @param array $categoryIdsRequested |
| 133 | + * @param int|null $storeId |
| 134 | + * @return array |
| 135 | + */ |
| 136 | + private function filterBucketValues( |
| 137 | + array $categoryBucketValues, |
| 138 | + array $categoryIdsRequested, |
| 139 | + ?int $storeId |
| 140 | + ): array { |
| 141 | + $categoryChildIds = []; |
| 142 | + $storeId = $storeId !== null ? $storeId : $this->storeManager->getStore()->getId(); |
| 143 | + $searchCriteria = $this->searchCriteriaBuilder |
| 144 | + ->addFilter('entity_id', $categoryIdsRequested, 'in') |
| 145 | + ->create(); |
| 146 | + $categoriesRequested = $this->categoryList->getList($searchCriteria); |
| 147 | + foreach ($categoriesRequested->getItems() as $category) { |
| 148 | + $category->setStoreId($storeId); |
| 149 | + $childrenIds = $category->getChildren(); |
| 150 | + if ($childrenIds) { |
| 151 | + $categoryChildIds[] = explode(',', $childrenIds); |
| 152 | + } |
| 153 | + } |
| 154 | + $categoryChildIds = array_merge([], ...$categoryChildIds); |
| 155 | + foreach ($categoryBucketValues as $key => $bucketValue) { |
| 156 | + $categoryId = (int)$bucketValue->getValue(); |
| 157 | + if (!in_array($categoryId, $categoryChildIds)) { |
| 158 | + unset($categoryBucketValues[$key]); |
| 159 | + } |
| 160 | + } |
| 161 | + return array_values($categoryBucketValues); |
| 162 | + } |
| 163 | +} |
0 commit comments