vendor/crehler/listing-banner/src/Core/Content/ListingBanners/SalesChannel/ListingBannersRoute.php line 49

Open in your IDE?
  1. <?php
  2. namespace Crehler\ListingBanner\Core\Content\ListingBanners\SalesChannel;
  3. use DateTime;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  14. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  15. use Shopware\Core\Framework\Routing\Annotation\Entity;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. /**
  19.  * @Route(defaults={"_routeScope"={"store-api"}})
  20.  */
  21. class ListingBannersRoute extends AbstractListingBannersRoute
  22. {
  23.     protected EntityRepositoryInterface $categoryRepository;
  24.     protected EntityRepositoryInterface $bannerRepository;
  25.     /**
  26.      * @param EntityRepositoryInterface $categoryRepository
  27.      * @param EntityRepositoryInterface $bannerRepository
  28.      */
  29.     public function __construct(EntityRepositoryInterface $categoryRepositoryEntityRepositoryInterface $bannerRepository)
  30.     {
  31.         $this->categoryRepository $categoryRepository;
  32.         $this->bannerRepository $bannerRepository;
  33.     }
  34.     public function getDecorated(): AbstractListingBannersRoute
  35.     {
  36.         throw new  DecorationPatternException(self::class);
  37.     }
  38.     /**
  39.      * @Entity("crehler_banner")
  40.      * @Route("/store-api/crehler/listing-banners/{categoryId}", name="store-api.crehler.listing-banners", methods={"GET"})
  41.      */
  42.     public function load(string $categoryIdCriteria $criteriaSalesChannelContext $context): ListingBannerRouteResponse
  43.     {
  44.         $path explode('|'$this->getPath($categoryId$context->getContext()));
  45.         $path array_filter($path, static function ($el) {
  46.            return $el !== "";
  47.         });
  48.         $date = (new DateTime())
  49.             ->format(Defaults::STORAGE_DATE_TIME_FORMAT);
  50.         $criteria
  51.             ->getAssociation('media')
  52.             ->addSorting(new FieldSorting('position'FieldSorting::ASCENDING));
  53.         $criteria
  54.             ->addFilter(new EqualsFilter('active'true))
  55.             ->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR,
  56.                 [
  57.                     new MultiFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('dateFrom'null), new EqualsFilter('dateTo'null)]),
  58.                     new MultiFilter(MultiFilter::CONNECTION_AND, [new RangeFilter('dateFrom', [RangeFilter::LT => $date]), new RangeFilter('dateTo', [RangeFilter::GT => $date])])
  59.                 ]
  60.             ))
  61.             ->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR,
  62.                 [
  63.                     new EqualsFilter('categories.id'$categoryId),
  64.                     new MultiFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('inheritCategory'true), new EqualsAnyFilter('categories.id'$path)])
  65.                 ]
  66.             ))
  67.             ->addSorting(new FieldSorting('priority'FieldSorting::DESCENDING));
  68.         return new ListingBannerRouteResponse($this->bannerRepository->search($criteria$context->getContext()));
  69.     }
  70.     protected function getPath(string $idContext $context): ?string
  71.     {
  72.         return $this->categoryRepository->search(new Criteria([$id]), $context)->first()->getPath();
  73.     }
  74. }