<?php declare(strict_types=1);
namespace LoyxxSW6SlideInBox\Core\Subscribers;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ResponseSubscriber implements EventSubscriberInterface
{
private const PREFERENCE_COOKIE_NAME = 'cookie-preference';
private const SLIDE_IN_BOX_COOKIE_NAME = 'slide-in-box';
/**
* @var ContainerInterface
*/
private $container;
/**
* @var CookieProviderInterface
*/
private $cookieService;
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(
ContainerInterface $container,
CookieProviderInterface $cookieService,
SystemConfigService $systemConfigService
)
{
$this->container = $container;
$this->cookieService = $cookieService;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
['setResponseCache', -1800]
]
];
}
public function setResponseCache(ResponseEvent $event)
{
$cookies = $event->getRequest()->cookies;
// slide-in-box cookie is technically required and needed to be in the header all the time
if ($cookies->get(static::SLIDE_IN_BOX_COOKIE_NAME) === null){
$event->getResponse()->headers->setCookie(new Cookie(static::SLIDE_IN_BOX_COOKIE_NAME, "1", time() + (86400 * 30), '/', null, false, false ));
}
}
}