vendor/shopware/core/Content/LandingPage/SalesChannel/LandingPageRoute.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\LandingPage\SalesChannel;
  3. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  4. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  5. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  6. use Shopware\Core\Content\LandingPage\Exception\LandingPageNotFoundException;
  7. use Shopware\Core\Content\LandingPage\LandingPageDefinition;
  8. use Shopware\Core\Content\LandingPage\LandingPageEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  13. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  14. use Shopware\Core\Framework\Routing\Annotation\Since;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route(defaults={"_routeScope"={"store-api"}})
  21.  */
  22. class LandingPageRoute extends AbstractLandingPageRoute
  23. {
  24.     /**
  25.      * @var SalesChannelRepositoryInterface
  26.      */
  27.     private $landingPageRepository;
  28.     /**
  29.      * @var SalesChannelCmsPageLoaderInterface
  30.      */
  31.     private $cmsPageLoader;
  32.     /**
  33.      * @var LandingPageDefinition
  34.      */
  35.     private $landingPageDefinition;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(
  40.         SalesChannelRepositoryInterface $landingPageRepository,
  41.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  42.         LandingPageDefinition $landingPageDefinition
  43.     ) {
  44.         $this->landingPageRepository $landingPageRepository;
  45.         $this->cmsPageLoader $cmsPageLoader;
  46.         $this->landingPageDefinition $landingPageDefinition;
  47.     }
  48.     public function getDecorated(): AbstractLandingPageRoute
  49.     {
  50.         throw new DecorationPatternException(self::class);
  51.     }
  52.     /**
  53.      * @Since("6.4.0.0")
  54.      * @Route("/store-api/landing-page/{landingPageId}", name="store-api.landing-page.detail", methods={"POST"})
  55.      */
  56.     public function load(string $landingPageIdRequest $requestSalesChannelContext $context): LandingPageRouteResponse
  57.     {
  58.         $landingPage $this->loadLandingPage($landingPageId$context);
  59.         $pageId $landingPage->getCmsPageId();
  60.         if (!$pageId) {
  61.             return new LandingPageRouteResponse($landingPage);
  62.         }
  63.         $resolverContext = new EntityResolverContext($context$request$this->landingPageDefinition$landingPage);
  64.         $pages $this->cmsPageLoader->load(
  65.             $request,
  66.             $this->createCriteria($pageId$request),
  67.             $context,
  68.             $landingPage->getTranslation('slotConfig'),
  69.             $resolverContext
  70.         );
  71.         if (!$pages->has($pageId)) {
  72.             throw new PageNotFoundException($pageId);
  73.         }
  74.         $landingPage->setCmsPage($pages->get($pageId));
  75.         return new LandingPageRouteResponse($landingPage);
  76.     }
  77.     private function loadLandingPage(string $landingPageIdSalesChannelContext $context): LandingPageEntity
  78.     {
  79.         $criteria = new Criteria([$landingPageId]);
  80.         $criteria->setTitle('landing-page::data');
  81.         $criteria->addFilter(new EqualsFilter('active'true));
  82.         $criteria->addFilter(new EqualsFilter('salesChannels.id'$context->getSalesChannel()->getId()));
  83.         $landingPage $this->landingPageRepository
  84.             ->search($criteria$context)
  85.             ->get($landingPageId);
  86.         if (!$landingPage) {
  87.             throw new LandingPageNotFoundException($landingPageId);
  88.         }
  89.         return $landingPage;
  90.     }
  91.     private function createCriteria(string $pageIdRequest $request): Criteria
  92.     {
  93.         $criteria = new Criteria([$pageId]);
  94.         $criteria->setTitle('landing-page::cms-page');
  95.         $slots $request->get('slots');
  96.         if (\is_string($slots)) {
  97.             $slots explode('|'$slots);
  98.         }
  99.         if (!empty($slots) && \is_array($slots)) {
  100.             $criteria
  101.                 ->getAssociation('sections.blocks')
  102.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  103.         }
  104.         return $criteria;
  105.     }
  106. }