<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\EventListener;
use Customize\Service\LandingPageService;
use Eccube\Request\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* @inheritDoc
*/
class LandingPageEventListener implements EventSubscriberInterface
{
/**
* @var Context
*/
private $requestContext;
/**
* @var LandingPageService
*/
private $landingPageService;
/**
* @inheritDoc
*/
public function __construct(
Context $context,
LandingPageService $landingPageService
) {
$this->requestContext = $context;
$this->landingPageService = $landingPageService;
}
public function onKernelRequest(ResponseEvent $event)
{
if ($event->isMasterRequest()) {
if ($this->requestContext->isFront()) {
if ($this->landingPageService->isCompletePage($event->getRequest())) {
// LPデータを削除
$response = $event->getResponse();
$response->headers->clearCookie(LandingPageService::COOKIE_NAME, LandingPageService::COOKIE_PATH);
$event->setResponse($response);
} else if ($this->landingPageService->isLandingPage($event->getRequest())) {
if ($this->landingPageService->isAdvertised($event->getRequest()) || !($lp = $this->landingPageService->get())) {
//
$land_page_data = serialize($this->landingPageService->retrieveLandingPageData());
$cookie = new Cookie(LandingPageService::COOKIE_NAME, $land_page_data, time() + LandingPageService::COOKIE_EXPIRES, LandingPageService::COOKIE_PATH);
//
$response = $event->getResponse();
$response->headers->setCookie($cookie);
$event->setResponse($response);
}
}
}
}
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
// SecurityServiceProviderで、認証処理が完了した後に実行.
['onKernelRequest', 6],
],
];
}
}