vendor/lnb/shopware6-lnb-partner-auth/src/Subscriber/SalesChannelResolvedSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace Lnb\Shopware6\LnbPartnerAuth\Subscriber;
  3. use Lnb\Shopware6\LnbPartnerAuth\Config\ConfigServiceAdapter;
  4. use Lnb\Shopware6\LnbPartnerAuth\Service\PartnerSessionResolver;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class SalesChannelResolvedSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private readonly RequestStack $requestStack,
  14.         private readonly PartnerSessionResolver $partnerSessionResolver,
  15.         private readonly ConfigServiceAdapter $configServiceAdapter
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             SalesChannelContextResolvedEvent::class => 'onSalesChannelResolved',
  22.         ];
  23.     }
  24.     public function onSalesChannelResolved(SalesChannelContextResolvedEvent $event): void
  25.     {
  26.         $this->configServiceAdapter->setSalesChannelId($event->getSalesChannelContext()->getSalesChannelId());
  27.         $partnerSalesChannelId $this->configServiceAdapter->getPartnerSalesChannelId();
  28.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  29.         if ($partnerSalesChannelId !== $salesChannelId) {
  30.             return; //wrong sales channel
  31.         }
  32.         $request $this->requestStack->getCurrentRequest();
  33.         if ($request === null) {
  34.             return; // no request probably cli context
  35.         }
  36.         if ($request->attributes->get('_route') === 'frontend.account.recover.page') {
  37.             //link to password recover page
  38.             $redirect $this->configServiceAdapter->getPartnerPasswordRecoverUrl();
  39.             $redirect .= substr($redirect, -1) === '/' '' '/';
  40.             (new RedirectResponse($redirect))->send();
  41.         }
  42.         if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
  43.             return; //customer logged in
  44.         }
  45.         $this->partnerSessionResolver->resolve($request$event->getSalesChannelContext());
  46.     }
  47. }