<?php declare(strict_types=1);
namespace Compra\DisableNewsletterSW6\Storefront\Subscriber;
use Psr\Container\ContainerInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class Newsletter implements EventSubscriberInterface {
/** @var ContainerInterface */
private $container;
/** @var SystemConfigService */
private $systemConfig;
public function __construct(ContainerInterface $container, SystemConfigService $systemConfig)
{
$this->container = $container;
$this->systemConfig = $systemConfig;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onHandleRequest'
];
}
/**
* Handles Kernel requests. Check if request is newsletter registration page and redirect to home page if configured in the plugin.
*
* @param RequestEvent $event
*/
public function onHandleRequest(RequestEvent $event)
{
$route = $event->getRequest()->attributes->get('_route');
if ($route === 'frontend.newsletter.subscribe') {
$salesChannelId = $event->getRequest()->attributes->get('sw-sales-channel-id');
$isRedirectActiveForSalesChannel = $this->systemConfig->get('CompraDisableNewsletterSW6.config.activeForSC', $salesChannelId);
if ($isRedirectActiveForSalesChannel) {
$url = $this->container->get('router')->generate('frontend.home.page');
$event->setResponse(new RedirectResponse($url));
}
}
}
}