vendor/dvdw/platform-choice/src/Framework/Routing/PlatformChoiceResolver.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\PlatformChoice\Framework\Routing;
  3. use Dvdw\Cache\Content\Category\SalesChannel\AbstractCategoryRoute;
  4. use Dvdw\PlatformChoice\Content\Category\CategoryCustomFields;
  5. use Dvdw\PlatformChoice\Framework\Cache\CrawlerDetector;
  6. use Dvdw\PlatformChoice\Storefront\Controller\RedirectController;
  7. use Dvdw\PlatformChoice\System\SalesChannel\Struct\PlatformChoiceExtension;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  10. use Shopware\Core\PlatformRequest;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. class PlatformChoiceResolver implements EventSubscriberInterface
  17. {
  18.     private RequestValidator $requestValidator;
  19.     private RedirectController $redirectController;
  20.     private AbstractCategoryRoute $categoryRoute;
  21.     private CrawlerDetector $crawlerDetector;
  22.     public function __construct(
  23.         RequestValidator      $requestValidator,
  24.         RedirectController    $redirectController,
  25.         AbstractCategoryRoute $categoryRoute,
  26.         CrawlerDetector       $crawlerDetector
  27.     )
  28.     {
  29.         $this->requestValidator $requestValidator;
  30.         $this->redirectController $redirectController;
  31.         $this->categoryRoute $categoryRoute;
  32.         $this->crawlerDetector $crawlerDetector;
  33.     }
  34.     public static function getSubscribedEvents(): iterable
  35.     {
  36.         return [
  37.             KernelEvents::CONTROLLER => [
  38.                 ['setRedirect'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  39.             ],
  40.         ];
  41.     }
  42.     public function setRedirect(ControllerEvent $event): void
  43.     {
  44.         $request $event->getRequest();
  45.         if (!$this->requestValidator->isNormalStorefrontRequestWithScopes($request)) {
  46.             return;
  47.         }
  48.         if ($this->crawlerDetector->isCrawler($request)) {
  49.             $this->crawlerDetector->markCrawler($request);
  50.             return;
  51.         }
  52.         /**@var SalesChannelContext $context */
  53.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  54.         /** @var PlatformChoiceExtension|null $choice */
  55.         if (($choice $context->getExtension(PlatformChoiceExtension::KEY)) !== null) {
  56.             if (($categoryId $request->get('navigationId')) === null) {
  57.                 return;
  58.             }
  59.             $this->validateCurrentCategory($categoryId$event$request$choice$context);
  60.             return;
  61.         }
  62.         if ($request->cookies->get(PlatformChoiceExtension::KEY) !== null) {
  63.             return;
  64.         }
  65.         $event->setController(
  66.             (function() use ($request) {
  67.                 return $this->redirectController->handleRedirect($request'frontend.platform-choice.index');
  68.             })
  69.         );
  70.     }
  71.     private function redirectToHome(ControllerEvent $eventRequest $request): void
  72.     {
  73.         $event->setController(
  74.             (function() use ($request) {
  75.                 return $this->redirectController->handleRedirect($request'frontend.home.page');
  76.             })
  77.         );
  78.     }
  79.     private function validateCurrentCategory(
  80.         string $categoryId,
  81.         ControllerEvent $event,
  82.         Request $request,
  83.         PlatformChoiceExtension $choice,
  84.         SalesChannelContext $context
  85.     ): void
  86.     {
  87.         $category $this->categoryRoute->load($request, new Criteria([$categoryId]), $context)->getCategories()->first();
  88.         if ($category === null || !$category->getActive()) {
  89.             return;
  90.         }
  91.         $customFields $category->getCustomFields();
  92.         if (isset($customFields[CategoryCustomFields::FIELD_NAME_B2B])
  93.             && $customFields[CategoryCustomFields::FIELD_NAME_B2B]
  94.             && $choice->isB2c()
  95.         )
  96.         {
  97.             $this->redirectToHome($event$request);
  98.             return;
  99.         }
  100.         if (isset($customFields[CategoryCustomFields::FIELD_NAME_B2C])
  101.             && $customFields[CategoryCustomFields::FIELD_NAME_B2C]
  102.             && $choice->isB2b()
  103.         )
  104.         {
  105.             $this->redirectToHome($event$request);
  106.         }
  107.     }
  108. }