custom/plugins/b2bsellerscore/src/Components/Order/Subscriber/DeepLinkOrderSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Order\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use B2bSellersCore\Components\Customer\Service\B2bAccountServiceInterface;
  5. use B2bSellersCore\Components\Employee\EmployeeEntity;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  12. use Shopware\Core\Framework\Routing\RequestContextResolverInterface;
  13. use Shopware\Core\PlatformRequest;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Component\Routing\RouterInterface;
  21. class DeepLinkOrderSubscriber implements EventSubscriberInterface
  22. {
  23.     use B2bContextTrait;
  24.     private EntityRepositoryInterface $orderRepository;
  25.     private RequestContextResolverInterface $requestContextResolver;
  26.     private RouterInterface $router;
  27.     public function __construct(
  28.         EntityRepositoryInterface       $orderRepository,
  29.         RequestContextResolverInterface $requestContextResolver,
  30.         RouterInterface $router
  31.     )
  32.     {
  33.         $this->orderRepository $orderRepository;
  34.         $this->requestContextResolver $requestContextResolver;
  35.         $this->router $router;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             KernelEvents::REQUEST => 'redirectDeepLinkOrders'
  41.         ];
  42.     }
  43.     public function redirectDeepLinkOrders(RequestEvent $event)
  44.     {
  45.         if ($event->isMainRequest() === false) {
  46.             return;
  47.         }
  48.         $request $event->getRequest();
  49.         $route $request->get('_route');
  50.         if ($route !== 'frontend.account.order.single.page') {
  51.             return;
  52.         }
  53.         $deepLinkCode $request->get('deepLinkCode'false);
  54.         if($deepLinkCode === false) {
  55.             return;
  56.         }
  57.         $request->attributes->set(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, new RouteScope(['scopes' => ['storefront']]));
  58.         $this->requestContextResolver->resolve($request);
  59.         /** @var SalesChannelContext|null $salesChannelContext */
  60.         $salesChannelContext $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  61.         if (!$salesChannelContext instanceof SalesChannelContext) {
  62.             return;
  63.         }
  64.         if(!$salesChannelContext->getCustomer() || !$this->hasB2bPlatformContext($salesChannelContext)) {
  65.             return;
  66.         }
  67.         $order $this->getOrderByDeepLinkCode($deepLinkCode$salesChannelContext->getContext());
  68.         if (!$order || !$order->getOrderCustomer()->getCustomer() || $order->getOrderCustomer()->getCustomer()->getGuest()) {
  69.             return;
  70.         }
  71.         $customer $order->getOrderCustomer()->getCustomer();
  72.         if($customer->getId() !== $salesChannelContext->getCustomerId()) {
  73.             return;
  74.         }
  75.         $url $this->router->generate('frontend.b2b_platform.path', [
  76.             'path' => 'order',
  77.             'id' => $order->getId()
  78.         ], UrlGeneratorInterface::ABSOLUTE_PATH);
  79.         $event->setResponse(new RedirectResponse($url));
  80.     }
  81.     private function getOrderByDeepLinkCode(string $deepLinkCodeContext $context): ?OrderEntity
  82.     {
  83.         $criteria = new Criteria();
  84.         $criteria->setTitle('b2b-deeplink-order-search');
  85.         $criteria->addAssociation('orderCustomer.customer');
  86.         $criteria->addFilter(new EqualsFilter('deepLinkCode'$deepLinkCode));
  87.         return $this->orderRepository->search($criteria$context)->first();
  88.     }
  89. }