custom/plugins/CompraDisableNewsletterSW6/src/Storefront/Subscriber/Newsletter.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\DisableNewsletterSW6\Storefront\Subscriber;
  3. use Psr\Container\ContainerInterface;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class Newsletter implements EventSubscriberInterface {
  10.     /** @var ContainerInterface  */
  11.     private $container;
  12.     /** @var SystemConfigService */
  13.     private $systemConfig;
  14.     public function __construct(ContainerInterface $containerSystemConfigService $systemConfig)
  15.     {
  16.         $this->container $container;
  17.         $this->systemConfig $systemConfig;
  18.     }
  19.     /**
  20.      * @return array
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             KernelEvents::REQUEST => 'onHandleRequest'
  26.         ];
  27.     }
  28.     /**
  29.      * Handles Kernel requests. Check if request is newsletter registration page and redirect to home page if configured in the plugin.
  30.      *
  31.      * @param RequestEvent $event
  32.      */
  33.     public function onHandleRequest(RequestEvent $event)
  34.     {
  35.         $route $event->getRequest()->attributes->get('_route');
  36.         if ($route === 'frontend.newsletter.subscribe') {
  37.             $salesChannelId $event->getRequest()->attributes->get('sw-sales-channel-id');
  38.             $isRedirectActiveForSalesChannel $this->systemConfig->get('CompraDisableNewsletterSW6.config.activeForSC'$salesChannelId);
  39.             if ($isRedirectActiveForSalesChannel) {
  40.                 $url $this->container->get('router')->generate('frontend.home.page');
  41.                 $event->setResponse(new RedirectResponse($url));
  42.             }
  43.         }
  44.     }
  45. }