vendor/store.shopware.com/acrisfiltercs/src/Storefront/Subscriber/PropertyOptionCountSubscriber.php line 63

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Filter\Storefront\Subscriber;
  3. use Acris\Filter\Components\FilterCount\Struct\TermCount;
  4. use Acris\Filter\Components\FilterCount\Struct\TermCountCollection;
  5. use Acris\Filter\Components\FilterCount\Struct\AggregationDocCount;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  8. use Shopware\Core\Content\Property\PropertyGroupEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\TermsResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\EntityResult;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class PropertyOptionCountSubscriber implements EventSubscriberInterface
  14. {
  15.     private SystemConfigService $configService;
  16.     public function __construct(SystemConfigService $configService)
  17.     {
  18.         $this->configService $configService;
  19.     }
  20.     /**
  21.      * @var TermCountCollection
  22.      */
  23.     private TermCountCollection $termCountCollection;
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             ProductListingResultEvent::class => [
  28.                 ['handleSearchResultBefore'150],
  29.                 ['handleSearchResultAfter', -150]
  30.             ],
  31.             ProductSearchResultEvent::class => [
  32.                 ['handleSearchResultBefore'150],
  33.                 ['handleSearchResultAfter', -150]
  34.             ]
  35.         ];
  36.     }
  37.     public function handleSearchResultBefore(ProductListingResultEvent $event): void
  38.     {
  39.         if(!$this->configService->get('AcrisFilterCS.config.filterCount')) {
  40.             return;
  41.         }
  42.         $aggregations $event->getResult()->getAggregations();
  43.         $this->termCountCollection = new TermCountCollection();
  44.         /** @var TermsResult|null $properties */
  45.         $properties $aggregations->get('properties');
  46.         $this->collectTermCount($this->termCountCollection$properties);
  47.         /** @var TermsResult|null $options */
  48.         $options $aggregations->get('options');
  49.         $this->collectTermCount($this->termCountCollection$options);
  50.     }
  51.     public function handleSearchResultAfter(ProductListingResultEvent $event): void
  52.     {
  53.         if(!$this->configService->get('AcrisFilterCS.config.filterCount')) {
  54.             return;
  55.         }
  56.         $aggregations $event->getResult()->getAggregations();
  57.         /** @var EntityResult|null $propertyGroups */
  58.         $propertyGroups $aggregations->get('properties');
  59.         if(!$propertyGroups instanceof EntityResult) {
  60.             return;
  61.         }
  62.         /** @var PropertyGroupEntity $propertyGroup */
  63.         foreach ($propertyGroups->getEntities() as $propertyGroup) {
  64.             foreach ($propertyGroup->getOptions() as $option) {
  65.                 if($this->termCountCollection->has($option->getId()) === true) {
  66.                     $option->addExtension(AggregationDocCount::AGGREGATION_DOC_COUNT, new AggregationDocCount($this->termCountCollection->get($option->getId())->getCount()));
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     private function collectTermCount(TermCountCollection $termCountCollection, ?TermsResult $termsResult): void
  72.     {
  73.         if(!$termsResult instanceof TermsResult) {
  74.             return;
  75.         }
  76.         foreach ($termsResult->getBuckets() as $bucket) {
  77.             $bucketCount $bucket->getCount();
  78.             if ($bucket->hasExtension('aggregationDocCount')) {
  79.                 /** @var AggregationDocCount $docCount */
  80.                 $docCount $bucket->getExtension('aggregationDocCount');
  81.                 $bucketCount $docCount->getDocCount();
  82.             }
  83.             if($termCountCollection->has($bucket->getKey()) === true) {
  84.                 continue;
  85.             } else {
  86.                 $termCount = new TermCount($bucket->getKey(), 0);
  87.                 $termCountCollection->set($bucket->getKey(), $termCount);
  88.             }
  89.             $termCount->setCount($termCount->getCount() + $bucketCount);
  90.         }
  91.     }
  92. }