vendor/crehler/listing-banner/src/Subscriber/NavigationPageLoadSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace Crehler\ListingBanner\Subscriber;
  3. use Crehler\ListingBanner\Core\Content\ListingBanners\SalesChannel\AbstractListingBannersRoute;
  4. use Crehler\ListingBanner\Entity\BannerEntity\BannerEntity;
  5. use Crehler\ListingBanner\Entity\BannerEntity\BannerEntityCollection;
  6. use Crehler\ListingBanner\Event\ProductListingCmsResolverEvent;
  7. use Crehler\ListingBanner\Struct\ProductBannerStruct;
  8. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  9. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class NavigationPageLoadSubscriber implements EventSubscriberInterface
  18. {
  19.     public const DEFAULT_LIMIT 24;
  20.     protected AbstractListingBannersRoute $listingBannersRoute;
  21.     protected EntityRepository $categoryRepository;
  22.     protected ?ProductBannerStruct $productBanner;
  23.     /**
  24.      * @param AbstractListingBannersRoute $listingBannersRoute
  25.      * @param EntityRepository $categoryRepository
  26.      */
  27.     public function __construct(
  28.         AbstractListingBannersRoute $listingBannersRoute,
  29.         EntityRepository            $categoryRepository
  30.     )
  31.     {
  32.         $this->listingBannersRoute $listingBannersRoute;
  33.         $this->categoryRepository $categoryRepository;
  34.         $this->productBanner null;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             ProductListingCriteriaEvent::class => ['onProductListingCriteria'100],
  40.             ProductListingCmsResolverEvent::class => ['onProductProductListingCmsResolver'5000],
  41.             ProductListingResultEvent::class => ['onProductListingResult'100],
  42.             CmsPageLoadedEvent::class => 'onCmsPageLoadedEvent'
  43.         ];
  44.     }
  45.     public function onCmsPageLoadedEvent(CmsPageLoadedEvent $event)
  46.     {
  47.         if(!$this->productBanner) {
  48.             return;
  49.         }
  50.         $event->getResult()->addExtension('crehlerBanner'$this->productBanner);
  51.     }
  52.     public function onProductListingResult(ProductListingResultEvent $event): void
  53.     {
  54.         if(!$this->productBanner) {
  55.             return;
  56.         }
  57.         $event->getResult()->addExtension('crehlerBanner'$this->productBanner);
  58.     }
  59.     public function onProductProductListingCmsResolver(
  60.         ProductListingCmsResolverEvent $event
  61.     ): void
  62.     {
  63.         $page = (int) $event->getRequest()->query->get('p'1);
  64.         if ($page 1) {
  65.             return;
  66.         }
  67.         $categoryId $event->getRequest()->attributes->get('navigationId');
  68.         $salesChannelContext $event->getSalesChannelContext();
  69.         if (!$categoryId || $this->isArrangmentCategory($categoryId$salesChannelContext->getContext())) {
  70.             return;
  71.         }
  72.         $criteria = (new Criteria());
  73.         $banners $this->listingBannersRoute->load($categoryId$criteria$salesChannelContext)->getBanners();
  74.         if ($banners->count() === 0) {
  75.             return;
  76.         }
  77.         $bannerArray $this->buildBanners($banners);
  78.         $this->productBanner $bannerArray;
  79.     }
  80.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  81.     {
  82.         if (!$this->productBanner) {
  83.             return;
  84.         }
  85.         $bannerArray $this->productBanner;
  86.         if ($event->getCriteria()->getOffset() === 0) {
  87.             $event->getCriteria()->setLimit(self::DEFAULT_LIMIT count($bannerArray->getDesktopMedia()));
  88.             $event->getRequest()->query->set('limit'self::DEFAULT_LIMIT count($bannerArray->getDesktopMedia()));
  89.         } else {
  90.             $event->getCriteria()->setOffset($event->getCriteria()->getOffset() - count($bannerArray->getDesktopMedia()));
  91.             $event->getRequest()->query->set('offset'$event->getCriteria()->getOffset() - count($bannerArray->getDesktopMedia()));
  92.             $event->getRequest()->query->set('bannerCount'count($bannerArray->getDesktopMedia()));
  93.         }
  94.         $event->getRequest()->request->set('criteriaGap'count($bannerArray->getDesktopMedia()));
  95.     }
  96.     protected function buildBanners(BannerEntityCollection $banners): ProductBannerStruct
  97.     {
  98.         $productBanner = new ProductBannerStruct();
  99.         foreach ($banners as $banner) {
  100.             $this->buildProductBanner($banner$productBanner);
  101.         }
  102.         return $productBanner;
  103.     }
  104.     protected function buildProductBanner(BannerEntity $bannerProductBannerStruct $productBanner): void
  105.     {
  106.         foreach ($banner->getMedia()->getElements() as $media) {
  107.             if ($media->isDesktop()) {
  108.                 $productBanner->addDesktopMediaItem($media);
  109.             } else {
  110.                 $productBanner->addMobileMediaItem($media);
  111.             }
  112.         }
  113.     }
  114.     protected function isArrangmentCategory(string $categoryIdContext $context): bool
  115.     {
  116.         return $this->categoryRepository->searchIds(
  117.                 (new Criteria([$categoryId]))
  118.                     ->addFilter(new EqualsFilter('customFields.crehler_category_is_arrangements'"1")),
  119.                 $context
  120.             )->getTotal() > 0;
  121.     }
  122. }