vendor/crehler/mojebambino-engine/src/Subscriber/ProductListingResultSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Service\FilterService;
  4. use Crehler\MojeBambinoEngine\Struct\FilterOrientationStruct;
  5. use Crehler\MojeBambinoEngine\Struct\FilterSettingsStruct;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Property\PropertyGroupCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\EntityResult;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductListingResultSubscriber implements EventSubscriberInterface
  15. {
  16.     private FilterService $filterService;
  17.     private string $colorFilterOrientation 'vertical';
  18.     public function __construct(FilterService $filterServiceSystemConfigService $systemConfigService)
  19.     {
  20.         $this->filterService $filterService;
  21.         $this->colorFilterOrientation $systemConfigService->get('CrehlerMojeBambinoEngine.config.colorFilterOrientation') ?? $this->colorFilterOrientation;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductListingResultEvent::class => 'handleProductListingResult',
  27.         ];
  28.     }
  29.     public function handleProductListingResult(ProductListingResultEvent $event): void
  30.     {
  31.         try {
  32.             $result $event->getResult();
  33.             $context $event->getContext();
  34.             $navigationId $result->getCurrentFilter('navigationId');
  35.             $aggregations $result->getAggregations();
  36.             $categorySettings $this->filterService->getCategorySettings($navigationId$context);
  37.             if (!is_null($categorySettings->getFilterSettings()) && !is_null($categorySettings->getPropertyGroupSettings())) {
  38.                 $this->sortFilters($aggregations$categorySettings->getFilterSettings(), $categorySettings->getPropertyGroupSettings());
  39.             } else {
  40.                 $this->addExtensions($aggregations);
  41.             }
  42.         } catch (\Throwable $exception) {
  43.             dd($exception);
  44.         }
  45.     }
  46.     private function sortFilters(AggregationResultCollection $aggregationsEntityCollection $filterSettingsEntityCollection $propertyGroupSettings): void
  47.     {
  48.         $aggregationsFilters $aggregations->getElements();
  49.         $aggregations->clear();
  50.         foreach ($filterSettings as $filterSetting) {
  51.             $filterName $filterSetting->get('name');
  52.             if (array_key_exists($filterName$aggregationsFilters)) {
  53.                 $filter $aggregationsFilters[$filterName];
  54.                 if ($filterName === 'properties') {
  55.                     $filter $this->sortProperties($filter$propertyGroupSettings);
  56.                 }
  57.                 if ($filter !== null) {
  58.                     $filter->addExtension(
  59.                         'filterSettings',
  60.                         new FilterSettingsStruct($filterSetting->get('show'), $filterSetting->get('expanded'))
  61.                     );
  62.                     $aggregations->add($filter);
  63.                 }
  64.                 unset($aggregationsFilters[$filterName]);
  65.             }
  66.         }
  67.         if (!empty($aggregationsFilters)) {
  68.             foreach ($aggregationsFilters as $filter) {
  69.                 $aggregations->add($filter);
  70.             }
  71.         }
  72.     }
  73.     private function sortProperties(AggregationResult $filterEntityCollection $propertyGroupSettings): ?EntityResult
  74.     {
  75.         if ($filter instanceof EntityResult) {
  76.             /** @var PropertyGroupCollection $propertyGroups */
  77.             $propertyGroups $filter->getEntities();
  78.             $propertyGroupsIds $propertyGroups->getIds();
  79.             $propertyGroupCollection = new PropertyGroupCollection();
  80.             foreach ($propertyGroupSettings as $propertyGroupSetting) {
  81.                 $propertyGroupId $propertyGroupSetting->get('propertyGroupId');
  82.                 $key array_search($propertyGroupId$propertyGroupsIds);
  83.                 if ($key !== false) {
  84.                     $property $propertyGroups->get($key);
  85.                     $property->addExtension(
  86.                         'orientation',
  87.                         new FilterOrientationStruct(
  88.                             $property->getDisplayType() === 'color' $this->colorFilterOrientation null
  89.                         )
  90.                     );
  91.                     $property->addExtension(
  92.                         'filterSettings',
  93.                         new FilterSettingsStruct($propertyGroupSetting->get('show'), $propertyGroupSetting->get('expanded'))
  94.                     );
  95.                     $propertyGroupCollection->add($property);
  96.                     $propertyGroups->remove($propertyGroupId);
  97.                 }
  98.             }
  99.             if ($propertyGroups->count() > 0) {
  100.                 foreach ($propertyGroups as $propertyGroup) {
  101.                     $propertyGroupCollection->add($propertyGroup);
  102.                 }
  103.             }
  104.             return new EntityResult('properties'$propertyGroupCollection);
  105.         }
  106.         return null;
  107.     }
  108.     private function addExtensions(AggregationResultCollection $aggregations)
  109.     {
  110.         foreach ($aggregations->getElements() as $name => $filterSetting) {
  111.             if ($name === 'properties') {
  112.                 if ($filterSetting instanceof EntityResult) {
  113.                     $propertyGroups $filterSetting->getEntities();
  114.                     foreach ($propertyGroups->getElements() as $property) {
  115.                         $property->addExtension(
  116.                             'orientation',
  117.                             new FilterOrientationStruct(
  118.                                 $property->getDisplayType() === 'color' $this->colorFilterOrientation null
  119.                             )
  120.                         );
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.     }
  126. }