|
| 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\Model\Resolver\Products\Query; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\CategoryRepository; |
| 11 | +use Magento\Framework\Api\Search\AggregationValueInterface; |
| 12 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 13 | +use Magento\Framework\Search\Response\Aggregation; |
| 14 | +use Magento\Framework\Search\Response\AggregationFactory; |
| 15 | +use Magento\Framework\Search\Response\Bucket; |
| 16 | +use Magento\Framework\Search\Response\BucketFactory; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Resolve category aggregation. |
| 21 | + */ |
| 22 | +class ResolveCategoryAggregation |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public const CATEGORY_BUCKET = 'category_bucket'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + private const BUCKETS = 'buckets'; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var AggregationFactory |
| 36 | + */ |
| 37 | + private $aggregationFactory; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var BucketFactory |
| 41 | + */ |
| 42 | + private $bucketFactory; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var StoreManagerInterface |
| 46 | + */ |
| 47 | + private $storeManager; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var CategoryRepository |
| 51 | + */ |
| 52 | + private $categoryRepository; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var array |
| 56 | + */ |
| 57 | + private $resolvedChildrenIds = []; |
| 58 | + |
| 59 | + /** |
| 60 | + * @param AggregationFactory $aggregationFactory |
| 61 | + * @param BucketFactory $bucketFactory |
| 62 | + * @param StoreManagerInterface $storeManager |
| 63 | + * @param CategoryRepository $categoryRepository |
| 64 | + */ |
| 65 | + public function __construct( |
| 66 | + AggregationFactory $aggregationFactory, |
| 67 | + BucketFactory $bucketFactory, |
| 68 | + StoreManagerInterface $storeManager, |
| 69 | + CategoryRepository $categoryRepository |
| 70 | + ) { |
| 71 | + $this->aggregationFactory = $aggregationFactory; |
| 72 | + $this->bucketFactory = $bucketFactory; |
| 73 | + $this->storeManager = $storeManager; |
| 74 | + $this->categoryRepository = $categoryRepository; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get resolved category aggregation. |
| 79 | + * |
| 80 | + * @param array $categoryFilter |
| 81 | + * @param Bucket[] $bucketList |
| 82 | + * |
| 83 | + * @return Aggregation |
| 84 | + */ |
| 85 | + public function getResolvedCategoryAggregation(array $categoryFilter, array $bucketList): Aggregation |
| 86 | + { |
| 87 | + $categoryBucket = $bucketList[self::CATEGORY_BUCKET] ?? []; |
| 88 | + $values = $categoryBucket->getValues(); |
| 89 | + $condition = array_key_first($categoryFilter); |
| 90 | + $searchableCategoryValue = $categoryFilter[$condition] ?? 0; |
| 91 | + |
| 92 | + if (!$searchableCategoryValue || empty($values)) { |
| 93 | + return $this->aggregationFactory->create([self::BUCKETS => $bucketList]); |
| 94 | + } |
| 95 | + |
| 96 | + $resolvedBucketList = $bucketList; |
| 97 | + |
| 98 | + try { |
| 99 | + if (!is_array($searchableCategoryValue)) { |
| 100 | + $resolvedValues = $this->getValidCategories((int) $searchableCategoryValue, $values); |
| 101 | + } else { |
| 102 | + $categoryResolvedValues = []; |
| 103 | + |
| 104 | + foreach ($searchableCategoryValue as $categoryId) { |
| 105 | + $categoryResolvedValues[] = $this->getValidCategories((int) $categoryId, $values); |
| 106 | + } |
| 107 | + |
| 108 | + $resolvedValues = call_user_func_array('array_merge', $categoryResolvedValues);; |
| 109 | + } |
| 110 | + } catch (NoSuchEntityException $e) { |
| 111 | + return $this->aggregationFactory->create([self::BUCKETS => $bucketList]); |
| 112 | + } |
| 113 | + |
| 114 | + $resolvedCategoryBucket = $this->bucketFactory->create( |
| 115 | + [ |
| 116 | + 'name' => self::CATEGORY_BUCKET, |
| 117 | + 'values' => $resolvedValues |
| 118 | + ] |
| 119 | + ); |
| 120 | + |
| 121 | + $resolvedBucketList[self::CATEGORY_BUCKET] = $resolvedCategoryBucket; |
| 122 | + |
| 123 | + return $this->aggregationFactory->create([self::BUCKETS => $resolvedBucketList]); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Check is valid searchable category children. |
| 128 | + * |
| 129 | + * @param int $searchableCategoryId |
| 130 | + * @param AggregationValueInterface[] $aggregationValues |
| 131 | + * |
| 132 | + * @return AggregationValueInterface[] |
| 133 | + * |
| 134 | + * @throws NoSuchEntityException |
| 135 | + */ |
| 136 | + private function getValidCategories(int $searchableCategoryId, array $aggregationValues): array |
| 137 | + { |
| 138 | + $stareId = (int) $this->storeManager->getStore()->getId(); |
| 139 | + $searchableCategory = $this->categoryRepository->get($searchableCategoryId, $stareId); |
| 140 | + $childrenList = $searchableCategory->getChildrenCategories(); |
| 141 | + $resolvedList = []; |
| 142 | + $validChildIdList = []; |
| 143 | + |
| 144 | + foreach ($childrenList as $child) { |
| 145 | + if (!$child->getIsActive()) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + $validChildIdList[] = $child->getId(); |
| 150 | + } |
| 151 | + |
| 152 | + foreach ($aggregationValues as $bucketValue) { |
| 153 | + $childCategoryId = (int) $bucketValue->getValue(); |
| 154 | + |
| 155 | + if (!in_array($childCategoryId, $validChildIdList) || |
| 156 | + in_array($childCategoryId, $this->resolvedChildrenIds) |
| 157 | + ) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + $resolvedList[] = $bucketValue; |
| 162 | + $this->resolvedChildrenIds[] = $childCategoryId; |
| 163 | + } |
| 164 | + |
| 165 | + return $resolvedList; |
| 166 | + } |
| 167 | +} |
0 commit comments