<?php
namespace B2bSellersCore\Components\Order\Subscriber;
use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
use B2bSellersCore\Components\Customer\Service\B2bAccountServiceInterface;
use B2bSellersCore\Components\Employee\EmployeeEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\RequestContextResolverInterface;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class DeepLinkOrderSubscriber implements EventSubscriberInterface
{
use B2bContextTrait;
private EntityRepositoryInterface $orderRepository;
private RequestContextResolverInterface $requestContextResolver;
private RouterInterface $router;
public function __construct(
EntityRepositoryInterface $orderRepository,
RequestContextResolverInterface $requestContextResolver,
RouterInterface $router
)
{
$this->orderRepository = $orderRepository;
$this->requestContextResolver = $requestContextResolver;
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'redirectDeepLinkOrders'
];
}
public function redirectDeepLinkOrders(RequestEvent $event)
{
if ($event->isMainRequest() === false) {
return;
}
$request = $event->getRequest();
$route = $request->get('_route');
if ($route !== 'frontend.account.order.single.page') {
return;
}
$deepLinkCode = $request->get('deepLinkCode', false);
if($deepLinkCode === false) {
return;
}
$request->attributes->set(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, new RouteScope(['scopes' => ['storefront']]));
$this->requestContextResolver->resolve($request);
/** @var SalesChannelContext|null $salesChannelContext */
$salesChannelContext = $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
if (!$salesChannelContext instanceof SalesChannelContext) {
return;
}
if(!$salesChannelContext->getCustomer() || !$this->hasB2bPlatformContext($salesChannelContext)) {
return;
}
$order = $this->getOrderByDeepLinkCode($deepLinkCode, $salesChannelContext->getContext());
if (!$order || !$order->getOrderCustomer()->getCustomer() || $order->getOrderCustomer()->getCustomer()->getGuest()) {
return;
}
$customer = $order->getOrderCustomer()->getCustomer();
if($customer->getId() !== $salesChannelContext->getCustomerId()) {
return;
}
$url = $this->router->generate('frontend.b2b_platform.path', [
'path' => 'order',
'id' => $order->getId()
], UrlGeneratorInterface::ABSOLUTE_PATH);
$event->setResponse(new RedirectResponse($url));
}
private function getOrderByDeepLinkCode(string $deepLinkCode, Context $context): ?OrderEntity
{
$criteria = new Criteria();
$criteria->setTitle('b2b-deeplink-order-search');
$criteria->addAssociation('orderCustomer.customer');
$criteria->addFilter(new EqualsFilter('deepLinkCode', $deepLinkCode));
return $this->orderRepository->search($criteria, $context)->first();
}
}