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

  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\Log\Package;
  13. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  14. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. #[Route(defaults: ['_routeScope' => ['store-api']])]
  19. #[Package('content')]
  20. class LandingPageRoute extends AbstractLandingPageRoute
  21. {
  22.     /**
  23.      * @internal
  24.      */
  25.     public function __construct(
  26.         private readonly SalesChannelRepository $landingPageRepository,
  27.         private readonly SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  28.         private readonly LandingPageDefinition $landingPageDefinition
  29.     ) {
  30.     }
  31.     public function getDecorated(): AbstractLandingPageRoute
  32.     {
  33.         throw new DecorationPatternException(self::class);
  34.     }
  35.     #[Route(path'/store-api/landing-page/{landingPageId}'name'store-api.landing-page.detail'methods: ['POST'])]
  36.     public function load(string $landingPageIdRequest $requestSalesChannelContext $context): LandingPageRouteResponse
  37.     {
  38.         $landingPage $this->loadLandingPage($landingPageId$context);
  39.         $pageId $landingPage->getCmsPageId();
  40.         if (!$pageId) {
  41.             return new LandingPageRouteResponse($landingPage);
  42.         }
  43.         $resolverContext = new EntityResolverContext($context$request$this->landingPageDefinition$landingPage);
  44.         $pages $this->cmsPageLoader->load(
  45.             $request,
  46.             $this->createCriteria($pageId$request),
  47.             $context,
  48.             $landingPage->getTranslation('slotConfig'),
  49.             $resolverContext
  50.         );
  51.         if (!$pages->has($pageId)) {
  52.             throw new PageNotFoundException($pageId);
  53.         }
  54.         $landingPage->setCmsPage($pages->get($pageId));
  55.         return new LandingPageRouteResponse($landingPage);
  56.     }
  57.     private function loadLandingPage(string $landingPageIdSalesChannelContext $context): LandingPageEntity
  58.     {
  59.         $criteria = new Criteria([$landingPageId]);
  60.         $criteria->setTitle('landing-page::data');
  61.         $criteria->addFilter(new EqualsFilter('active'true));
  62.         $criteria->addFilter(new EqualsFilter('salesChannels.id'$context->getSalesChannel()->getId()));
  63.         $landingPage $this->landingPageRepository
  64.             ->search($criteria$context)
  65.             ->get($landingPageId);
  66.         if (!$landingPage) {
  67.             throw new LandingPageNotFoundException($landingPageId);
  68.         }
  69.         return $landingPage;
  70.     }
  71.     private function createCriteria(string $pageIdRequest $request): Criteria
  72.     {
  73.         $criteria = new Criteria([$pageId]);
  74.         $criteria->setTitle('landing-page::cms-page');
  75.         $slots $request->get('slots');
  76.         if (\is_string($slots)) {
  77.             $slots explode('|'$slots);
  78.         }
  79.         if (!empty($slots) && \is_array($slots)) {
  80.             $criteria
  81.                 ->getAssociation('sections.blocks')
  82.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  83.         }
  84.         return $criteria;
  85.     }
  86. }